From a444838bf9532a8c2b61aebb4d8dfe9fb41a4226 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 19 Aug 2017 16:42:04 -0400 Subject: [PATCH] Add support for imports --- src/main/kotlin/net/shadowfacts/ekt/EKT.kt | 8 +++++++- src/test/resources/templates/template.ekt | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/net/shadowfacts/ekt/EKT.kt b/src/main/kotlin/net/shadowfacts/ekt/EKT.kt index beed18e..3c26843 100644 --- a/src/main/kotlin/net/shadowfacts/ekt/EKT.kt +++ b/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 { diff --git a/src/test/resources/templates/template.ekt b/src/test/resources/templates/template.ekt index a2d56e3..b00e1f7 100644 --- a/src/test/resources/templates/template.ekt +++ b/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") =]