From: David ‘Bombe’ Roden Date: Mon, 9 Sep 2019 19:54:53 +0000 (+0200) Subject: ✨ Show audio player for links to audio files X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=97fe04482ebb8a08e43294acde041c2975cbd8ee;p=Sone.git ✨ Show audio player for links to audio files --- 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())