Last active
November 30, 2023 17:25
-
-
Save SalomonBrys/f1c21beefd9150bfd659ff8c59030c48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.JsonObject | |
import com.google.gson.JsonParser | |
import com.google.gson.JsonPrimitive | |
import java.net.HttpURLConnection | |
import java.net.URL | |
import kotlin.io.path.Path | |
import kotlin.io.path.writer | |
val prefix = "https://github.githubassets.com/images/icons/emoji/unicode/" | |
val suffix = ".png?v8" | |
fun main() { | |
val json = (URL("https://api.github.com/emojis").openConnection() as HttpURLConnection) | |
.inputStream.use { it.reader().readText() } | |
val obj = JsonParser.parseString(json) as JsonObject | |
Path("Emoji.kt").writer().use { writer -> | |
writer.appendLine("package org.kodein.pres.ui") | |
writer.appendLine() | |
writer.appendLine("// Generated from https://api.github.com/emojis") | |
writer.appendLine("// with https://gist.github.com/SalomonBrys/f1c21beefd9150bfd659ff8c59030c48") | |
writer.appendLine("public object Emoji {") | |
val map = LinkedHashMap<String, String>() | |
obj.keySet().forEach { id -> | |
val url = (obj[id] as JsonPrimitive).asString | |
if (!url.startsWith(prefix)) return@forEach | |
require(url.endsWith(suffix)) { url } | |
val codePointsStr = url.removeSurrounding(prefix, suffix) | |
val name = when { | |
id[0] == '+' -> "plus${id.substring(1)}" | |
id[0] == '-' -> "minus${id.substring(1)}" | |
id[0] in "0123456789" -> "_$id" | |
id == "package" -> "_package" | |
else -> id | |
} .replace('-', '_') | |
map[id] = name | |
val utf16Str = codePointsStr.split("-") | |
.map { codePointStr -> | |
val codePoint = codePointStr.toIntOrNull(16) ?: error("Not Int: $codePointStr") | |
Character.toChars(codePoint).joinToString("") { "\\u${it.code.toString(16).padStart(4, '0')}" } | |
} | |
.joinToString("\\u200d") | |
writer.appendLine(" public val $name: String = \"$utf16Str\" // U+$codePointsStr") | |
} | |
writer.appendLine(" private val map: Map<String, String> by lazy {") | |
writer.appendLine(" mapOf(") | |
map.forEach { id, name -> | |
writer.appendLine(" \"$id\" to $name,") | |
} | |
writer.appendLine(" )") | |
writer.appendLine(" }") | |
writer.appendLine(" public operator fun get(id: String): String? = map[id]") | |
writer.appendLine("}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment