Skip to content

Commit 298a104

Browse files
committed
fix: Implement more templates
Closes #234 Closes #233
1 parent b795f8b commit 298a104

File tree

2 files changed

+73
-20
lines changed

2 files changed

+73
-20
lines changed

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

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ fun String.toWikitextAnnotatedString(
6161
var i = 0
6262
var number = 1 // Count for numbered lists
6363

64+
var italic = false
65+
var bold = false
66+
6467
val twas: String.() -> AnnotatedString = {
6568
this.toWikitextAnnotatedString(
6669
colorScheme,
@@ -680,8 +683,52 @@ fun String.toWikitextAnnotatedString(
680683
}
681684
}
682685

686+
currSubstring.startsWith("{{redirect-distinguish", true) -> {
687+
val splitList = currSubstring.substringAfter('|').split('|')
688+
append("\"${splitList.getOrNull(0)}\" redirects here; not to be confused with ")
689+
splitList.subList(1, splitList.size)
690+
.fastForEachIndexed { index: Int, it: String ->
691+
append(
692+
"[[${it.substringBefore(MAGIC_SEP)}|${
693+
it.substringAfter(
694+
MAGIC_SEP
695+
)
696+
}]]".twas()
697+
)
698+
699+
if (index == splitList.size - 2 && splitList.size > 2) append(
700+
", or "
701+
)
702+
else if (index < splitList.size - 2) append(", ")
703+
}
704+
append('.')
705+
}
706+
707+
currSubstring.startsWith("{{for", true) -> {
708+
val splitList = currSubstring.substringAfter('|').split('|')
709+
if (splitList.size > 1) {
710+
append("For ${splitList[0]}, see ")
711+
splitList.subList(1, splitList.size)
712+
.fastForEachIndexed { index: Int, it: String ->
713+
append(
714+
"[[${it.substringBefore(MAGIC_SEP)}|${
715+
it.substringAfter(
716+
MAGIC_SEP
717+
)
718+
}]]".twas()
719+
)
720+
721+
if (index == splitList.size - 2 && splitList.size > 2) append(
722+
", and "
723+
)
724+
else if (index < splitList.size - 2) append(", ")
725+
}
726+
append('.')
727+
}
728+
}
729+
683730
currSubstring.startsWith("{{hatnote", ignoreCase = true) -> {
684-
val curr = currSubstring.substringAfter('|')
731+
val curr = currSubstring.substringAfter('|').replace('\n', ' ')
685732
append("''$curr''".twas())
686733
}
687734

@@ -965,6 +1012,13 @@ fun String.toWikitextAnnotatedString(
9651012
append("<sup>[citation needed]</sup>".twas())
9661013
}
9671014

1015+
currSubstring.startsWith("{{url", true) -> {
1016+
val curr = currSubstring.substringAfter('|', "")
1017+
if (curr.matches(".+://.+".toRegex()))
1018+
append("[$curr ${curr.substringAfter("//")}]".twas())
1019+
else append("[https://$curr $curr]".twas())
1020+
}
1021+
9681022
currSubstring.startsWith("{{Starbox begin", ignoreCase = true) -> {
9691023
val templateLength = currSubstring.length
9701024
i = input.indexOf(
@@ -1118,24 +1172,23 @@ fun String.toWikitextAnnotatedString(
11181172

11191173
'\'' ->
11201174
if (input.getOrNull(i + 1) == '\'' && input.getOrNull(i + 2) == '\'') {
1121-
val subs = input.substring(i + 3)
1122-
val curr = subs.substring(
1123-
0,
1124-
min(
1125-
subs.length,
1126-
("'''(?!')".toRegex().find(subs)?.range?.start ?: subs.length) + 2
1127-
)
1128-
)
1129-
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
1130-
append(curr.twas())
1175+
if (!bold) {
1176+
pushStyle(SpanStyle(fontWeight = FontWeight.Bold))
1177+
bold = true
1178+
} else {
1179+
pop()
1180+
bold = false
11311181
}
1132-
i += curr.length + 3
1182+
i += 2
11331183
} else if (input.getOrNull(i + 1) == '\'') {
1134-
val curr = input.substring(i + 2).substringBefore("''")
1135-
withStyle(SpanStyle(fontStyle = FontStyle.Italic)) {
1136-
append(curr.twas())
1184+
if (!italic) {
1185+
pushStyle(SpanStyle(fontStyle = FontStyle.Italic))
1186+
italic = true
1187+
} else {
1188+
pop()
1189+
italic = false
11371190
}
1138-
i += 1 + curr.length + 2
1191+
i += 1
11391192
} else append(input[i])
11401193

11411194
'[' ->
@@ -1170,7 +1223,7 @@ fun String.toWikitextAnnotatedString(
11701223
) {
11711224
append(
11721225
(linkText.substringAfter(' ').removeSuffix("]")
1173-
.trim() + "\uD83D\uDD17")
1226+
.trim() + " \uD83D\uDD17")
11741227
)
11751228
}
11761229
i += linkText.length - 1

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import androidx.compose.runtime.getValue
3232
import androidx.compose.runtime.mutableStateOf
3333
import androidx.compose.runtime.remember
3434
import androidx.compose.runtime.rememberCoroutineScope
35+
import androidx.compose.runtime.saveable.rememberSaveable
3536
import androidx.compose.runtime.setValue
3637
import androidx.compose.ui.Alignment
3738
import androidx.compose.ui.Modifier
@@ -81,7 +82,7 @@ fun AsyncInfobox(
8182
}
8283
}
8384

84-
var expanded by remember { mutableStateOf(false) }
85+
var expanded by rememberSaveable { mutableStateOf(false) }
8586

8687
SharedTransitionLayout {
8788
Card(
@@ -180,8 +181,7 @@ fun AsyncInfobox(
180181
onLinkClick = onLinkClick,
181182
onClick = onImageClick,
182183
showCaption = false,
183-
modifier = Modifier
184-
.weight(2f)
184+
modifier = Modifier.weight(2f)
185185
)
186186
} else {
187187
Text(

0 commit comments

Comments
 (0)