mirror of https://github.com/shadowfacts/ekt.git
Add dumpGeneratedScript option
This commit is contained in:
parent
08c84c1e67
commit
6378d6cdc7
|
@ -26,7 +26,7 @@ _result.toString()
|
||||||
ScriptEngineManager()
|
ScriptEngineManager()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun render(template: String, data: Map<String, Any>): String {
|
fun render(template: String, data: Map<String, Any>, dumpGeneratedScript: Boolean = false): String {
|
||||||
@Suppress("NAME_SHADOWING")
|
@Suppress("NAME_SHADOWING")
|
||||||
var template = template
|
var template = template
|
||||||
template = template.replace("\"", "\\\"")
|
template = template.replace("\"", "\\\"")
|
||||||
|
@ -44,16 +44,18 @@ _result.toString()
|
||||||
|
|
||||||
val script = scriptPrefix + template + scriptSuffix
|
val script = scriptPrefix + template + scriptSuffix
|
||||||
|
|
||||||
File("script.kts").apply {
|
if (dumpGeneratedScript) {
|
||||||
if (!exists()) createNewFile()
|
File("script.kts").apply {
|
||||||
writeText(script)
|
if (!exists()) createNewFile()
|
||||||
|
writeText(script)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return eval(script, data) as String
|
return eval(script, data) as String
|
||||||
}
|
}
|
||||||
|
|
||||||
fun render(template: File, data: Map<String, Any>): String {
|
fun render(template: File, data: Map<String, Any>, dumpGeneratedScript: Boolean = false): String {
|
||||||
return render(template.readText(), data)
|
return render(template.readText(), data, dumpGeneratedScript)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun eval(script: String, data: Map<String, Any> = mapOf()): Any? {
|
internal fun eval(script: String, data: Map<String, Any> = mapOf()): Any? {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.io.File
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val res = EKT.render(File("template.ekt"), mapOf(
|
val res = EKT.render(File("template.ekt"), mapOf(
|
||||||
"value" to 11
|
"value" to 11
|
||||||
))
|
), dumpGeneratedScript = true)
|
||||||
|
|
||||||
File("result.txt").apply {
|
File("result.txt").apply {
|
||||||
if (!exists()) createNewFile()
|
if (!exists()) createNewFile()
|
||||||
|
|
Loading…
Reference in New Issue