X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FLinkedElementRenderFilter.kt;h=b11dc79aca2946fd66c283ef714470fd98a3f627;hb=0dd4bb42d2aa7e67c0750415a31e75bf9f4284d7;hp=e15f68ba2edeb0c44e86ea3cc9ab518f1129085c;hpb=19e49c0a2463b90c3468903672d8ba600da217d2;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt index e15f68b..b11dc79 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt @@ -1,26 +1,15 @@ package net.pterodactylus.sone.template -import net.pterodactylus.sone.core.LinkedElement -import net.pterodactylus.util.template.Filter -import net.pterodactylus.util.template.TemplateContext -import net.pterodactylus.util.template.TemplateContextFactory -import net.pterodactylus.util.template.TemplateParser -import java.io.StringReader -import java.io.StringWriter -import javax.inject.Inject +import net.pterodactylus.sone.core.* +import net.pterodactylus.sone.utils.* +import net.pterodactylus.util.template.* +import java.io.* +import javax.inject.* /** * Renders all kinds of [LinkedElement]s. */ -class LinkedElementRenderFilter @Inject constructor(private val templateContextFactory: TemplateContextFactory): Filter { - - companion object { - private val loadedImageTemplate = """<%include linked/image.html>""".parse() - private val loadedHtmlPageTemplate = """<%include linked/html-page.html>""".parse() - private val notLoadedImageTemplate = """<%include linked/notLoaded.html>""".parse() - - private fun String.parse() = StringReader(this).use { TemplateParser.parse(it) }!! - } +class LinkedElementRenderFilter @Inject constructor(private val templateContextFactory: TemplateContextFactory) : Filter { override fun format(templateContext: TemplateContext?, data: Any?, parameters: Map?) = when { @@ -34,8 +23,7 @@ class LinkedElementRenderFilter @Inject constructor(private val templateContextF StringWriter().use { val templateContext = templateContextFactory.createTemplateContext() templateContext["link"] = linkedElement.link - loadedImageTemplate.render(templateContext, it) - it + it.also { loadedImageTemplate.render(templateContext, it) } }.toString() private fun renderHtmlPage(linkedElement: LinkedElement) = @@ -51,8 +39,11 @@ class LinkedElementRenderFilter @Inject constructor(private val templateContextF StringWriter().use { val templateContext = templateContextFactory.createTemplateContext() templateContext["link"] = linkedElement.link - notLoadedImageTemplate.render(templateContext, it) - it + it.also { notLoadedImageTemplate.render(templateContext, it) } }.toString() } + +private val loadedImageTemplate = """<%include linked/image.html>""".asTemplate() +private val loadedHtmlPageTemplate = """<%include linked/html-page.html>""".asTemplate() +private val notLoadedImageTemplate = """<%include linked/notLoaded.html>""".asTemplate()