feat: added QrScript support for templates
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user