aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt42
1 files changed, 42 insertions, 0 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
new file mode 100644
index 0000000..dedfbbf
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt
@@ -0,0 +1,42 @@
+package ca.allanwang.kau.xml
+
+import android.content.Context
+import android.content.res.XmlResourceParser
+import android.support.annotation.XmlRes
+import android.text.Html
+import android.text.Spanned
+import ca.allanwang.kau.utils.use
+import org.xmlpull.v1.XmlPullParser
+
+/**
+ * Created by Allan Wang on 2017-07-30.
+ */
+
+/**
+ * Parse an xml with two tags, <question>Text</question> and <answer>Text</answer>,
+ * and return them as a list of string pairs
+ */
+fun Context.kauParseFaq(@XmlRes xmlRes: Int, withNumbering: Boolean = true): List<Pair<Spanned, Spanned>> {
+ val items = mutableListOf<Pair<Spanned, Spanned>>()
+ resources.getXml(xmlRes).use {
+ parser: XmlResourceParser ->
+ var eventType = parser.eventType
+ var question: Spanned? = null
+ while (eventType != XmlPullParser.END_DOCUMENT) {
+ if (eventType == XmlPullParser.START_TAG) {
+ if (parser.name == "question") {
+ var q = parser.text.replace("\n", "<br/>")
+ if (withNumbering) q = "${items.size + 1}. $q"
+ question = Html.fromHtml(q)
+ } else if (parser.name == "answer") {
+ items.add(Pair(question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"),
+ Html.fromHtml(parser.text.replace("\n", "<br/>"))))
+ question = null
+ }
+ }
+
+ eventType = parser.next()
+ }
+ }
+ return items
+} \ No newline at end of file