Browse Source

Add dumpGeneratedScript option

master
Shadowfacts 5 years ago
parent
commit
6378d6cdc7
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
  1. 14
      src/main/kotlin/net/shadowfacts/ekt/EKT.kt
  2. 2
      src/test/kotlin/net/shadowfacts/ekt/Test.kt

14
src/main/kotlin/net/shadowfacts/ekt/EKT.kt

@ -26,7 +26,7 @@ _result.toString()
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")
var template = template
template = template.replace("\"", "\\\"")
@ -44,16 +44,18 @@ _result.toString()
val script = scriptPrefix + template + scriptSuffix
File("script.kts").apply {
if (!exists()) createNewFile()
writeText(script)
if (dumpGeneratedScript) {
File("script.kts").apply {
if (!exists()) createNewFile()
writeText(script)
}
}
return eval(script, data) as String
}
fun render(template: File, data: Map<String, Any>): String {
return render(template.readText(), data)
fun render(template: File, data: Map<String, Any>, dumpGeneratedScript: Boolean = false): String {
return render(template.readText(), data, dumpGeneratedScript)
}
internal fun eval(script: String, data: Map<String, Any> = mapOf()): Any? {

2
src/test/kotlin/net/shadowfacts/ekt/Test.kt

@ -8,7 +8,7 @@ import java.io.File
fun main(args: Array<String>) {
val res = EKT.render(File("template.ekt"), mapOf(
"value" to 11
))
), dumpGeneratedScript = true)
File("result.txt").apply {
if (!exists()) createNewFile()

Loading…
Cancel
Save