Refactor
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package starter.modules
|
||||
|
||||
import com.mitchellbosecke.pebble.extension.escaper.SafeString
|
||||
import com.mitchellbosecke.pebble.template.EvaluationContext
|
||||
import com.mitchellbosecke.pebble.template.PebbleTemplate
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
import starter.Dependency
|
||||
import starter.utils.PebbleEngineBuilder
|
||||
import starter.utils.PebbleEngineBuilder.CacheType.ConcurrentMap
|
||||
import starter.utils.PebbleFunction
|
||||
|
||||
class DepAsXmlPebbleFunction : PebbleFunction {
|
||||
override val name = "depAsXml"
|
||||
override fun getArgumentNames() = listOf("dependency")
|
||||
override fun execute(args: Map<String, Any>, self: PebbleTemplate, context: EvaluationContext, lineNumber: Int): Any {
|
||||
val dep = args["dependency"] as Dependency
|
||||
|
||||
fun startTag(name: String): String {
|
||||
@Language("html") @Suppress("UnnecessaryVariable")
|
||||
val result = """<span class="text-gray-700"><</span><span class="text-red-700">$name</span><span class="text-gray-700">></span>"""
|
||||
return result
|
||||
}
|
||||
|
||||
fun endTag(name: String): String {
|
||||
@Language("html") @Suppress("UnnecessaryVariable")
|
||||
val result = """<span class="text-gray-700"></</span><span class="text-red-700">$name</span><span class="text-gray-700">></span>"""
|
||||
return result
|
||||
}
|
||||
|
||||
fun tag(name: String, content: String): String {
|
||||
return """${startTag(name)}$content${endTag(name)}"""
|
||||
}
|
||||
|
||||
val result = """
|
||||
|${startTag("dependency")}
|
||||
| ${tag("groupId", dep.groupId)}
|
||||
| ${tag("artifactId", dep.artifactId)}
|
||||
| ${tag("version", dep.version)}
|
||||
|${endTag("dependency")}
|
||||
""".trimMargin()
|
||||
|
||||
return SafeString(result)
|
||||
}
|
||||
}
|
||||
|
||||
val pebbleModule = module {
|
||||
single { DepAsXmlPebbleFunction() } bind PebbleFunction::class
|
||||
|
||||
single {
|
||||
PebbleEngineBuilder {
|
||||
cache = ConcurrentMap
|
||||
functions(getAll())
|
||||
classPath { suffix = ".twig" }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user