data is LinkedElement && data.loading -> renderNotLoadedLinkedElement(data)
data is LinkedElement && data.properties["type"] == "image" -> renderLinkedImage(data)
data is LinkedElement && data.properties["type"] == "html" -> renderHtmlPage(data)
+ data is LinkedElement && data.properties["type"] == "audio" -> renderAudioPlayer(data)
else -> null
}
private fun renderLinkedImage(linkedElement: LinkedElement) =
- StringWriter().use {
- val templateContext = templateContextFactory.createTemplateContext()
+ renderTemplate(loadedImageTemplate) { templateContext ->
templateContext["link"] = linkedElement.link
- it.also { loadedImageTemplate.render(templateContext, it) }
- }.toString()
+ }
private fun renderHtmlPage(linkedElement: LinkedElement) =
- StringWriter().use {
- val templateContext = templateContextFactory.createTemplateContext()
+ renderTemplate(loadedHtmlPageTemplate) { templateContext ->
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 renderAudioPlayer(linkedElement: LinkedElement) =
+ renderTemplate(loadedAudioTemplate) { templateContext ->
+ templateContext["link"] = linkedElement.link
+ }
private fun renderNotLoadedLinkedElement(linkedElement: LinkedElement) =
- StringWriter().use {
- val templateContext = templateContextFactory.createTemplateContext()
+ renderTemplate(notLoadedImageTemplate) { templateContext ->
templateContext["link"] = linkedElement.link
- it.also { notLoadedImageTemplate.render(templateContext, it) }
+ }
+
+ private fun renderTemplate(template: Template, fillTemplateContext: (TemplateContext) -> Unit) =
+ StringWriter().use { stringWriter ->
+ stringWriter.also {
+ templateContextFactory.createTemplateContext().also { templateContext ->
+ fillTemplateContext(templateContext)
+ template.render(templateContext, stringWriter)
+ }
+ }
}.toString()
}
private val loadedImageTemplate = """<%include linked/image.html>""".asTemplate()
private val loadedHtmlPageTemplate = """<%include linked/html-page.html>""".asTemplate()
+private val loadedAudioTemplate = """<%include linked/audio.html>""".asTemplate()
private val notLoadedImageTemplate = """<%include linked/notLoaded.html>""".asTemplate()
import org.hamcrest.Matchers.*
import org.jsoup.*
import org.jsoup.nodes.*
-import org.junit.*
+import kotlin.test.Test
/**
* Unit test for [LinkedElementRenderFilter].
}
@Test
+ fun `filter can render audio player`() {
+ val html = filter.format(null, LinkedElement("KSK@gpl.mp3", properties = mapOf("type" to "audio")), emptyMap()) as String
+ val outerSpanNode = Jsoup.parseBodyFragment(html).body().child(0)
+ assertThat(outerSpanNode.nodeName(), equalTo("span"))
+ assertThat(outerSpanNode.attr("class"), equalTo("linked-element loaded"))
+ assertThat(outerSpanNode.attr("title"), equalTo("KSK@gpl.mp3"))
+ val linkNode = outerSpanNode.child(0)
+ assertThat(linkNode.nodeName(), equalTo("audio"))
+ assertThat(linkNode.attr("controls"), equalTo(""))
+ assertThat(linkNode.attr("src"), equalTo("/KSK@gpl.mp3"))
+ }
+
+ @Test
fun `render filter can be created by guice`() {
val injector = Guice.createInjector(TemplateContextFactory::class.isProvidedByMock())
assertThat(injector.getInstance<LinkedElementRenderFilter>(), notNullValue())