From 97fe04482ebb8a08e43294acde041c2975cbd8ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 9 Sep 2019 21:54:53 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Show=20audio=20player=20for=20links?= =?utf8?q?=20to=20audio=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/template/LinkedElementRenderFilter.kt | 33 ++++++++++++++-------- src/main/resources/templates/linked/audio.html | 1 + .../sone/template/LinkedElementRenderFilterTest.kt | 15 +++++++++- 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/main/resources/templates/linked/audio.html diff --git a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt index 200bd83..5cc602c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt @@ -20,34 +20,45 @@ class LinkedElementRenderFilter : Filter { 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() diff --git a/src/main/resources/templates/linked/audio.html b/src/main/resources/templates/linked/audio.html new file mode 100644 index 0000000..cd04c5b --- /dev/null +++ b/src/main/resources/templates/linked/audio.html @@ -0,0 +1 @@ + diff --git a/src/test/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilterTest.kt index 8363f6b..e93bfeb 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilterTest.kt @@ -8,7 +8,7 @@ import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.jsoup.* import org.jsoup.nodes.* -import org.junit.* +import kotlin.test.Test /** * Unit test for [LinkedElementRenderFilter]. @@ -64,6 +64,19 @@ class LinkedElementRenderFilterTest { } @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(), notNullValue()) -- 2.7.4