Browse Source

Add support for imports

master
Shadowfacts 4 years ago
parent
commit
a444838bf9
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
  1. 8
      src/main/kotlin/net/shadowfacts/ekt/EKT.kt
  2. 4
      src/test/resources/templates/template.ekt

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

@ -24,6 +24,8 @@ object EKT {
private val startStringRegex = Regex("([:=#])]")
private val endStringRegex = Regex("\\[([:=#])")
private val lastImportRegex = Regex("(import (.*?)\\n)+")
private val scriptPrefix = """
val _env = bindings["_env"] as net.shadowfacts.ekt.EKT.TemplateEnvironment
val _result = StringBuilder()
@ -67,7 +69,11 @@ _result.toString()
}
})
val script = scriptPrefix + template + scriptSuffix
val lines = template.split("\n")
val imports = lines.filter { it.trim().startsWith("import") }.joinToString("\n")
template = lines.filterNot { it.trim().startsWith("import") }.joinToString("\n")
val script = imports + scriptPrefix + template + scriptSuffix
if (env.cacheDir != null) {
env.cacheFile.apply {

4
src/test/resources/templates/template.ekt

@ -1,3 +1,7 @@
[: import java.util.ArrayList :]
[= ArrayList::class.simpleName =]
Environment type: [= _env::class.simpleName =]
[= list.joinToString("\n") =]

Loading…
Cancel
Save