Basic quote handling

Handling quotation marks and spacing are going to be interesting
This commit is contained in:
Bradlee Speice 2015-04-05 22:33:19 -04:00
parent 3bb9d61ff9
commit 7bcb676532
8 changed files with 90 additions and 49 deletions

View File

@ -16,16 +16,22 @@ import dagger.ObjectGraph;
* Set up the application! * Set up the application!
*/ */
public class MinimalBible extends Application implements Injector { public class MinimalBible extends Application implements Injector {
private String TAG = "MinimalBible"; private static Context mContext;
private ObjectGraph mObjectGraph; private ObjectGraph mObjectGraph;
public static MinimalBible get(Context ctx) { public static MinimalBible get(Context ctx) {
return (MinimalBible) ctx.getApplicationContext(); return (MinimalBible) ctx.getApplicationContext();
} }
public static Context getAppContext() {
Logger.w("Statically accessing context, please refactor that.");
return mContext;
}
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
mContext = this;
Logger.init().setLogLevel(LogLevel.NONE); Logger.init().setLogLevel(LogLevel.NONE);
buildObjGraph(); buildObjGraph();
setJswordHome(); setJswordHome();
@ -52,10 +58,10 @@ public class MinimalBible extends Application implements Injector {
// We need to set the download directory for jSword to stick with // We need to set the download directory for jSword to stick with
// Android. // Android.
String home = this.getFilesDir().toString(); String home = this.getFilesDir().toString();
Logger.d(TAG, "Setting jsword.home to: " + home); Logger.d("Setting jsword.home to: " + home);
System.setProperty("jsword.home", home); System.setProperty("jsword.home", home);
System.setProperty("sword.home", home); System.setProperty("sword.home", home);
SwordBookPath.setDownloadDir(new File(home)); SwordBookPath.setDownloadDir(new File(home));
Logger.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir()); Logger.d("Sword download path: " + SwordBookPath.getSwordDownloadDir());
} }
} }

View File

@ -1,18 +1,17 @@
package org.bspeice.minimalbible.service.format.osisparser package org.bspeice.minimalbible.service.format.osisparser
import org.xml.sax.helpers.DefaultHandler
import org.crosswire.jsword.passage.Verse
import java.util.ArrayDeque
import org.xml.sax.Attributes
import org.crosswire.jsword.book.OSISUtil
import org.crosswire.jsword.book.BookData
import org.crosswire.jsword.book.Book
import kotlin.properties.Delegates
import org.bspeice.minimalbible.service.format.osisparser.handler.TagHandler
import org.bspeice.minimalbible.service.format.osisparser.handler.VerseHandler
import org.bspeice.minimalbible.service.format.osisparser.handler.UnknownHandler
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import org.bspeice.minimalbible.service.format.osisparser.handler.DivineHandler import org.bspeice.minimalbible.MinimalBible
import org.bspeice.minimalbible.R
import org.bspeice.minimalbible.service.format.osisparser.handler.*
import org.crosswire.jsword.book.Book
import org.crosswire.jsword.book.BookData
import org.crosswire.jsword.book.OSISUtil
import org.crosswire.jsword.passage.Verse
import org.xml.sax.Attributes
import org.xml.sax.helpers.DefaultHandler
import java.util.ArrayDeque
import kotlin.properties.Delegates
/** /**
* Parse out the OSIS XML into whatever we want! * Parse out the OSIS XML into whatever we want!
@ -56,12 +55,17 @@ class OsisParser() : DefaultHandler() {
when (localName) { when (localName) {
OSISUtil.OSIS_ELEMENT_VERSE -> handlerStack push VerseHandler() OSISUtil.OSIS_ELEMENT_VERSE -> handlerStack push VerseHandler()
"divineName" -> handlerStack push DivineHandler() "divineName" -> handlerStack push DivineHandler()
"q" -> handlerStack push QHandler(MinimalBible.getAppContext()
.getResources().getColor(R.color.divineSpeech))
else -> handlerStack push UnknownHandler(localName) else -> handlerStack push UnknownHandler(localName)
} }
handlerStack.peek().start(attributes, verseContent, builder)
} }
override fun endElement(uri: String, localName: String, qName: String) { override fun endElement(uri: String, localName: String, qName: String) {
handlerStack.pop() val tagHandler = handlerStack.pop()
tagHandler.end(verseContent, builder)
} }
override fun characters(ch: CharArray, start: Int, length: Int) { override fun characters(ch: CharArray, start: Int, length: Int) {

View File

@ -1,13 +1,18 @@
package org.bspeice.minimalbible.service.format.osisparser.handler package org.bspeice.minimalbible.service.format.osisparser.handler
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import android.text.style.RelativeSizeSpan import android.text.style.RelativeSizeSpan
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import org.xml.sax.Attributes
/**
* Created by bspeice on 12/1/14.
*/
class DivineHandler() : TagHandler { class DivineHandler() : TagHandler {
override fun end(info: VerseContent, builder: SpannableStringBuilder) {
}
override fun start(attrs: Attributes, info: VerseContent,
builder: SpannableStringBuilder) {
}
override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) {
this buildDivineName chars forEach { it apply builder } this buildDivineName chars forEach { it apply builder }
} }

View File

@ -0,0 +1,20 @@
package org.bspeice.minimalbible.service.format.osisparser.handler
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import org.xml.sax.Attributes
class QHandler(val color: Int) : TagHandler {
override fun start(attrs: Attributes, info: VerseContent, builder: SpannableStringBuilder) {
}
override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) {
AppendArgs(chars, ForegroundColorSpan(color)) apply builder
}
override fun end(info: VerseContent, builder: SpannableStringBuilder) {
AppendArgs(" ", null) apply builder
}
}

View File

@ -1,15 +1,14 @@
package org.bspeice.minimalbible.service.format.osisparser.handler package org.bspeice.minimalbible.service.format.osisparser.handler
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import android.text.style.CharacterStyle import android.text.style.CharacterStyle
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
/** import org.xml.sax.Attributes
* Created by bspeice on 12/1/14.
*/
trait TagHandler { trait TagHandler {
fun start(attrs: Attributes, info: VerseContent, builder: SpannableStringBuilder)
fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String)
fun end(info: VerseContent, builder: SpannableStringBuilder)
} }
data class AppendArgs(val text: String, val span: Any?) { data class AppendArgs(val text: String, val span: Any?) {
@ -17,9 +16,9 @@ data class AppendArgs(val text: String, val span: Any?) {
val offset = builder.length() val offset = builder.length()
builder.append(text) builder.append(text)
when (span) { when (span) {
is List<*> -> span.forEach { builder.setSpan(it, offset, offset + text.length, 0) } is List<*> -> span.forEach { builder.setSpan(it, offset, offset + text.length(), 0) }
is CharacterStyle -> builder.setSpan(span, offset, offset + text.length, 0) is CharacterStyle -> builder.setSpan(span, offset, offset + text.length(), 0)
} }
builder.setSpan(span, offset, offset + text.length, 0) builder.setSpan(span, offset, offset + text.length(), 0)
} }
} }

View File

@ -3,11 +3,16 @@ package org.bspeice.minimalbible.service.format.osisparser.handler
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import com.orhanobut.logger.Logger import com.orhanobut.logger.Logger
import org.bspeice.minimalbible.service.format.osisparser.VerseContent import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import org.xml.sax.Attributes
/**
* Created by bspeice on 12/1/14.
*/
class UnknownHandler(val tagName: String) : TagHandler { class UnknownHandler(val tagName: String) : TagHandler {
override fun end(info: VerseContent, builder: SpannableStringBuilder) {
}
override fun start(attrs: Attributes, info: VerseContent,
builder: SpannableStringBuilder) {
}
override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) {
Logger.w("Unknown tag '$tagName' received text: '$chars'") Logger.w("Unknown tag '$tagName' received text: '$chars'")
} }

View File

@ -1,28 +1,28 @@
package org.bspeice.minimalbible.service.format.osisparser.handler package org.bspeice.minimalbible.service.format.osisparser.handler
import android.text.SpannableStringBuilder
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import android.text.style.StyleSpan
import android.graphics.Typeface import android.graphics.Typeface
import android.text.style.SuperscriptSpan import android.text.SpannableStringBuilder
import android.text.style.RelativeSizeSpan import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.SuperscriptSpan
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
import org.xml.sax.Attributes
/**
* Created by bspeice on 12/1/14.
*/
class VerseHandler() : TagHandler { class VerseHandler() : TagHandler {
var isVerseStart = true
override fun end(info: VerseContent, builder: SpannableStringBuilder) {
}
override fun start(attrs: Attributes, info: VerseContent,
builder: SpannableStringBuilder) {
when {
info.verseNum == 1 -> AppendArgs("${info.chapter} ", StyleSpan(Typeface.BOLD))
else -> AppendArgs("${info.verseNum}",
listOf(SuperscriptSpan(), RelativeSizeSpan(.75f)))
} apply builder
}
override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) { override fun render(builder: SpannableStringBuilder, info: VerseContent, chars: String) {
buildVerseHeader(info.chapter, info.verseNum, isVerseStart) apply builder
builder append chars builder append chars
isVerseStart = false
}
fun buildVerseHeader(chapter: Int, verseNum: Int, verseStart: Boolean): AppendArgs =
when {
!verseStart -> AppendArgs("", null)
verseNum == 1 -> AppendArgs("$chapter ", StyleSpan(Typeface.BOLD))
else -> AppendArgs("${verseNum}", listOf(SuperscriptSpan(), RelativeSizeSpan(.75f)))
} }
} }

View File

@ -13,4 +13,6 @@
<color name="settingsTextColor">@color/textColor</color> <color name="settingsTextColor">@color/textColor</color>
<color name="settingsTextColorSecondary">@color/textColorSecondary</color> <color name="settingsTextColorSecondary">@color/textColorSecondary</color>
<color name="divineSpeech">@color/colorPrimary</color>
</resources> </resources>