feat: added QrScript support for templates

This commit is contained in:
2026-06-23 20:54:58 +03:00
parent d702928c28
commit e8b572bf9a
8 changed files with 70 additions and 8 deletions
@@ -121,3 +121,41 @@ fun base64ToImageBitmap(base64String: String): ImageBitmap {
return bitmap.asImageBitmap()
}
fun processQrScript(input: String, fills: Map<String, String>): String {
val outerRegex = Regex("""\$(\w+)((?:\([^)]*\))*)\$""")
val innerRegex = Regex("""\(([^)]*)\)""")
var result = input
outerRegex.findAll(input).forEach { match ->
val fullBlock = match.value
val name = match.groupValues[1]
val contents = innerRegex.findAll(match.groupValues[2]).map { it.groupValues[1] }.toList()
var blockResult = fills[name] ?: return@forEach
contents.forEach { content ->
val args = content.split(" ")
val action = args[0]
when (action) {
"first" -> {
val count = args[1].toInt()
blockResult = blockResult.take(count)
}
"last" -> {
val count = args[1].toInt()
blockResult = blockResult.takeLast(count)
}
}
}
result = result.replace(fullBlock, blockResult)
}
return result
}