Skip to content

Commit ab0a179

Browse files
committed
fix: Implement more templates
Closes #228 Closes #232 Closes #242 #230 (partially fixed)
1 parent 298a104 commit ab0a179

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

app/src/main/java/org/nsh07/wikireader/data/misc.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.nsh07.wikireader.data
22

33
import androidx.compose.ui.graphics.Color
4+
import java.util.Locale
45

56
/**
67
* Extension function for [String] to convert it to a [Color]
@@ -79,6 +80,36 @@ fun bytesToHumanReadableSize(bytes: Double) = when {
7980
else -> "$bytes bytes"
8081
}
8182

83+
fun Double.formatToHumanReadable(): String {
84+
return when {
85+
this >= 1000000000000000 -> String.format(
86+
Locale.US,
87+
"%.2f quadrillion",
88+
this / 1000000000000000.0
89+
)
90+
91+
this >= 1000000000000 -> String.format(Locale.US, "%.2f trillion", this / 1000000000000.0)
92+
this >= 1000000000 -> String.format(Locale.US, "%.2f billion", this / 1000000000.0)
93+
this >= 1000000 -> String.format(Locale.US, "%.2f million", this / 1000000.0)
94+
else -> this.toString()
95+
}
96+
}
97+
98+
fun Long.formatToHumanReadable(): String {
99+
return when {
100+
this >= 1000000000000000 -> String.format(
101+
Locale.US,
102+
"%.2f quadrillion",
103+
this / 1000000000000000.0
104+
)
105+
106+
this >= 1000000000000 -> String.format(Locale.US, "%.2f trillion", this / 1000000000000.0)
107+
this >= 1000000000 -> String.format(Locale.US, "%.2f billion", this / 1000000000.0)
108+
this >= 1000000 -> String.format(Locale.US, "%.2f million", this / 1000000.0)
109+
else -> this.toString()
110+
}
111+
}
112+
82113
/**
83114
* Converts a Wikipedia URL language code (e.g. "en" in en.wikipedia.org for the English Wikipedia)
84115
* into its corresponding language name (e.g. "English" for en)

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.nsh07.wikireader.data.LanguageData.alpha3CodeToAlpha2CodeMap
2929
import org.nsh07.wikireader.data.LanguageData.countryNameToAlpha2CodeMap
3030
import org.nsh07.wikireader.data.LanguageData.fifaCodeToAlpha3CodeMap
3131
import org.nsh07.wikireader.data.countryFlag
32+
import org.nsh07.wikireader.data.formatToHumanReadable
3233
import org.nsh07.wikireader.data.langCodeToName
3334
import org.nsh07.wikireader.parser.ReferenceData.refCount
3435
import org.nsh07.wikireader.parser.ReferenceData.refList
@@ -704,6 +705,15 @@ fun String.toWikitextAnnotatedString(
704705
append('.')
705706
}
706707

708+
currSubstring.startsWith("{{format price", true) -> {
709+
val curr = currSubstring.substringAfter('|').substringBefore('|')
710+
append(
711+
if (curr.contains('.')) curr.toDoubleOrNull()
712+
?.formatToHumanReadable() ?: curr.twas()
713+
else curr.toLongOrNull()?.formatToHumanReadable() ?: curr.twas()
714+
)
715+
}
716+
707717
currSubstring.startsWith("{{for", true) -> {
708718
val splitList = currSubstring.substringAfter('|').split('|')
709719
if (splitList.size > 1) {
@@ -727,6 +737,13 @@ fun String.toWikitextAnnotatedString(
727737
}
728738
}
729739

740+
currSubstring.startsWith("{{US$", true) -> {
741+
if (currSubstring.contains('|')) {
742+
val curr = currSubstring.substringAfter('|')
743+
append("US$${curr.twas()}")
744+
} else append("US$")
745+
}
746+
730747
currSubstring.startsWith("{{hatnote", ignoreCase = true) -> {
731748
val curr = currSubstring.substringAfter('|').replace('\n', ' ')
732749
append("''$curr''".twas())
@@ -971,7 +988,7 @@ fun String.toWikitextAnnotatedString(
971988
)
972989
}
973990

974-
currSubstring.startsWith("{{unbulleted list") -> {
991+
currSubstring.startsWith("{{unbulleted list", true) -> {
975992
val splitList = currSubstring
976993
.substringAfter('|', "")
977994
.splitNotInBraces('|')
@@ -982,6 +999,30 @@ fun String.toWikitextAnnotatedString(
982999
append(splitList.twas())
9831000
}
9841001

1002+
listOf(
1003+
"{{bulleted list",
1004+
"{{ulist",
1005+
"{{blist",
1006+
"{{bulleted",
1007+
"{{unordered list"
1008+
).fastAny {
1009+
currSubstring.startsWith(it, true)
1010+
} -> {
1011+
val splitList = currSubstring
1012+
.substringAfter('|', "")
1013+
.splitNotInBraces('|')
1014+
.fastMap {
1015+
if (it.trim()
1016+
.matches("\\d+=.+".toRegex(RegexOption.DOT_MATCHES_ALL))
1017+
)
1018+
it.substringAfter('=').trim()
1019+
else it.trim()
1020+
}
1021+
.joinToString("\n* ")
1022+
1023+
append("* $splitList".twas())
1024+
}
1025+
9851026
currSubstring.startsWith("{{Transcluded section", true) -> {
9861027
val title =
9871028
currSubstring.split('|').filter { it.contains("source=") }
@@ -1043,13 +1084,21 @@ fun String.toWikitextAnnotatedString(
10431084
)
10441085
}
10451086

1046-
currSubstring.startsWith("{{siglo", true) -> {
1087+
listOf("{{siglo", "{{segle").fastAny {
1088+
currSubstring.startsWith(it, true)
1089+
} -> {
10471090
// Spanish/Catalan template for century
10481091
val first = currSubstring.substringAfter("{{").substringBefore('|')
10491092
val second = currSubstring.substringAfter('|').substringBefore('|')
10501093
append("$first <small>$second</small>".twas())
10511094
}
10521095

1096+
currSubstring.startsWith("{{romanes", true) -> {
1097+
// Spanish/Catalan template for roman numerals
1098+
val second = currSubstring.substringAfter('|').substringBefore('|')
1099+
append("<small>$second</small>".twas())
1100+
}
1101+
10531102
currSubstring.startsWith("{{tracce", true) -> {
10541103
// Italian template for music track lists
10551104
val splitList = currSubstring.substringAfter('|')
@@ -1304,5 +1353,5 @@ object ReferenceData {
13041353
val refListCount = mutableMapOf<String, Int>()
13051354
var refTemplate = "{{cite"
13061355
val refTemplates = listOf("{{cite", "{{lien", "{{cita|")
1307-
val infoboxTemplates = listOf("{{infobox", "{{taxobox", "{{Automatic taxobox")
1356+
val infoboxTemplates = listOf("{{infobox", "{{taxobox", "{{Automatic taxobox", "{{Картка")
13081357
}

0 commit comments

Comments
 (0)