Skip to content

Commit 45d2880

Browse files
committed
fix: Improve section heading handling
1 parent 78e2aa1 commit 45d2880

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

app/src/main/java/org/nsh07/wikireader/parser/cleanUpWikitext.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.nsh07.wikireader.parser
88
fun cleanUpWikitext(wikitext: String): String {
99
return wikitext
1010
.replace("<!--.+?-->".toRegex(), "")
11+
.replace("== \n", "==\n")
1112
.replace(
1213
"\\{\\{nobility table header.*?\\}\\}"
1314
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.DOT_MATCHES_ALL)),

app/src/main/java/org/nsh07/wikireader/ui/homeScreen/viewModel/HomeScreenViewModel.kt

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class HomeScreenViewModel(
401401
lastQuery = Pair(apiResponse.title, setLang)
402402

403403
val extractText = if (apiResponse != null)
404-
wikipediaRepository.getPageContent(apiResponse.title)
404+
cleanUpWikitext(wikipediaRepository.getPageContent(apiResponse.title))
405405
else ""
406406
val extract: List<String> = parseSections(extractText)
407407

@@ -713,7 +713,7 @@ class HomeScreenViewModel(
713713
jsonInst.decodeFromString<WikiApiPageData>(savedArticle.apiResponse)
714714
.query?.pages?.get(0)
715715

716-
val extractText = savedArticle.pageContent
716+
val extractText = cleanUpWikitext(savedArticle.pageContent)
717717
val extract: List<String> = parseSections(extractText)
718718

719719
sections = extract.size
@@ -786,22 +786,21 @@ class HomeScreenViewModel(
786786
*/
787787
private suspend fun parseWikitext(wikitext: String): List<AnnotatedString> =
788788
withContext(Dispatchers.IO) {
789-
val parsed = cleanUpWikitext(wikitext)
790789
var curr = ""
791790
var i = 0
792791
var stack = 0
793792
val out = mutableListOf<AnnotatedString>()
794793

795-
while (i < parsed.length) {
796-
if (parsed[i] == '{')
794+
while (i < wikitext.length) {
795+
if (wikitext[i] == '{')
797796
stack++
798-
else if (parsed[i] == '}')
797+
else if (wikitext[i] == '}')
799798
stack--
800799

801-
if (parsed[i] == '<') {
802-
var currSubstring = parsed.substring(i, min(i + 16, parsed.length))
800+
if (wikitext[i] == '<') {
801+
var currSubstring = wikitext.substring(i, min(i + 16, wikitext.length))
803802
if (currSubstring.startsWith("<math display")) {
804-
currSubstring = parsed.substring(i).substringBefore("</math>")
803+
currSubstring = wikitext.substring(i).substringBefore("</math>")
805804
out.add(
806805
curr.toWikitextAnnotatedString(
807806
colorScheme = colorScheme,
@@ -817,7 +816,7 @@ class HomeScreenViewModel(
817816
i += currSubstring.length + 7
818817
curr = ""
819818
} else if (currSubstring.startsWith("<gallery")) {
820-
currSubstring = parsed.substring(i).substringBefore("</gallery>")
819+
currSubstring = wikitext.substring(i).substringBefore("</gallery>")
821820
out.add(
822821
curr.toWikitextAnnotatedString(
823822
colorScheme = colorScheme,
@@ -830,9 +829,9 @@ class HomeScreenViewModel(
830829
out.add(AnnotatedString(currSubstring))
831830
i += currSubstring.length + 10
832831
curr = ""
833-
} else curr += parsed[i]
834-
} else if (stack == 0 && parsed[i] == '[' && parsed.getOrNull(i + 1) == '[') {
835-
val currSubstring = parsed.substringMatchingParen('[', ']', i)
832+
} else curr += wikitext[i]
833+
} else if (stack == 0 && wikitext[i] == '[' && wikitext.getOrNull(i + 1) == '[') {
834+
val currSubstring = wikitext.substringMatchingParen('[', ']', i)
836835
if (currSubstring.contains(':')) {
837836
if (currSubstring
838837
.matches(
@@ -868,13 +867,13 @@ class HomeScreenViewModel(
868867
curr = ""
869868
i += currSubstring.length - 1
870869
} else
871-
curr += parsed[i]
870+
curr += wikitext[i]
872871
} else {
873-
curr += parsed[i]
872+
curr += wikitext[i]
874873
}
875-
} else if (parsed[i] == '{') {
876-
if (parsed.getOrNull(i + 1) == '|') {
877-
val currSubstring = parsed.substringMatchingParen('{', '}', i)
874+
} else if (wikitext[i] == '{') {
875+
if (wikitext.getOrNull(i + 1) == '|') {
876+
val currSubstring = wikitext.substringMatchingParen('{', '}', i)
878877
if (!currSubstring.substring(min(i + 2, currSubstring.lastIndex))
879878
.contains("{|")
880879
) {
@@ -892,7 +891,11 @@ class HomeScreenViewModel(
892891
i += currSubstring.length
893892
} else {
894893
val currSubstringNestedTable =
895-
parsed.substringMatchingParen('{', '}', parsed.indexOf("{|", i + 2))
894+
wikitext.substringMatchingParen(
895+
'{',
896+
'}',
897+
wikitext.indexOf("{|", i + 2)
898+
)
896899
out.add(
897900
curr.toWikitextAnnotatedString(
898901
colorScheme = colorScheme,
@@ -907,15 +910,15 @@ class HomeScreenViewModel(
907910
i += currSubstring.length
908911
}
909912
} else if (
910-
stack < 2 && parsed.getOrNull(i + 1) == '{' &&
911-
parsed.substring(i, min(i + 24, parsed.length))
913+
stack < 2 && wikitext.getOrNull(i + 1) == '{' &&
914+
wikitext.substring(i, min(i + 24, wikitext.length))
912915
.let { subStr ->
913916
infoboxTemplates.fastAny {
914917
subStr.startsWith(it, true)
915918
}
916919
}
917920
) {
918-
val currSubstring = parsed.substringMatchingParen('{', '}', i)
921+
val currSubstring = wikitext.substringMatchingParen('{', '}', i)
919922
out.add(
920923
curr.toWikitextAnnotatedString(
921924
colorScheme = colorScheme,
@@ -928,8 +931,8 @@ class HomeScreenViewModel(
928931
out.add(AnnotatedString(currSubstring))
929932
curr = ""
930933
i += currSubstring.length - 1
931-
} else curr += parsed[i]
932-
} else curr += parsed[i]
934+
} else curr += wikitext[i]
935+
} else curr += wikitext[i]
933936
i++
934937
}
935938
out.add(

0 commit comments

Comments
 (0)