add list of known dictionaries for more convenient links

This commit is contained in:
Helium314 2024-02-19 14:10:49 +01:00
parent b1eb33f6e2
commit fd95b4dc87
10 changed files with 215 additions and 3 deletions

View file

@ -0,0 +1,5 @@
# make-dict-list
This module takes care of generating a list of dictionaries available in the [dictionaries repository](https://codeberg.org/Helium314/aosp-dictionaries) for convenient linking when adding dictionaries in HeliBoard.
To use it, simply run `./gradlew tools:make-dict-list:makeDictList`

View file

@ -0,0 +1,18 @@
apply plugin: "java"
apply plugin: 'kotlin'
ext {
javaMainClass = "tools.dict.MakeDictList"
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.register('makeDictList', JavaExec) {
args project.rootProject.project('app').projectDir.path + File.separator + 'src' +
File.separator + 'main' + File.separator + 'assets'
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
}

View file

@ -0,0 +1,63 @@
package tools.dict
import java.io.File
import java.net.URL
class MakeDictList {
companion object {
@JvmStatic fun main(args: Array<String>) {
val readmeUrl = "https://codeberg.org/Helium314/aosp-dictionaries/raw/branch/main/README.md"
val readmeText = URL(readmeUrl).readText()
val fileText = doIt(readmeText)
val targetDir = args[0]
File(targetDir).mkdirs()
File("$targetDir/dictionaries_in_dict_repo.csv").writeText(fileText)
}
}
}
/**
* extract dictionary list from README.md
* output format: <localeString>,<type>,<experimental>
* <experimental> is empty if dictionary is not experimental, no other check done
* requires README.md to have dicts in correct "# Dictionaries" or "# Experimental dictionaries" sections
*/
private fun doIt(readme: String): String {
// output format: <localeString>,<type>,<experimental>
// experimental is empty if dictionary is not experimental, no other check done
var mode = MODE_NOTHING
val outLines = mutableListOf<String>()
readme.split("\n").forEach { line ->
if (line.startsWith("#")) {
mode = if (line.trim() == "# Dictionaries")
MODE_NORMAL
else if (line.trim() == "# Experimental dictionaries")
MODE_EXPERIMENTAL
else
MODE_NOTHING
return@forEach
}
if (mode == MODE_NOTHING || !line.startsWith("*")) return@forEach
val dictName = line.substringAfter("]").substringAfter("(").substringBefore(")")
.substringAfterLast("/").substringBefore(".dict")
val type = dictName.substringBefore("_")
val rawLocale = dictName.substringAfter("_")
val locale = if ("_" !in rawLocale) rawLocale
else {
val split = rawLocale.split("_").toMutableList()
if (!split[1].startsWith("#"))
split[1] = split[1].uppercase()
split.joinToString("_")
}
outLines.add("$type,$locale,${if (mode == MODE_EXPERIMENTAL) "exp" else ""}")
}
return outLines.joinToString("\n") + "\n"
}
private const val MODE_NOTHING = 0
private const val MODE_NORMAL = 1
private const val MODE_EXPERIMENTAL = 2