X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FLinkedElementRenderFilter.kt;h=b11dc79aca2946fd66c283ef714470fd98a3f627;hp=8d328be99df8939871252a63d2f1626426a16c57;hb=0dd4bb42d2aa7e67c0750415a31e75bf9f4284d7;hpb=550309db774fb837ba2172571b04c24ee417e4e8 diff --git a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt index 8d328be..b11dc79 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt @@ -1,30 +1,21 @@ 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 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 { data is LinkedElement && data.loading -> renderNotLoadedLinkedElement(data) - data is LinkedElement -> renderLinkedImage(data) + data is LinkedElement && data.properties["type"] == "image" -> renderLinkedImage(data) + data is LinkedElement && data.properties["type"] == "html" -> renderHtmlPage(data) else -> null } @@ -32,16 +23,27 @@ 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) = + StringWriter().use { + val templateContext = templateContextFactory.createTemplateContext() + templateContext["link"] = linkedElement.link + templateContext["title"] = linkedElement.properties["title"] ?: "No title" + templateContext["description"] = linkedElement.properties["description"] ?: "No description" + it.also { loadedHtmlPageTemplate.render(templateContext, it) } }.toString() private fun renderNotLoadedLinkedElement(linkedElement: LinkedElement) = 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()