✨ Show audio player for links to audio files
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 9 Sep 2019 19:54:53 +0000 (21:54 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 9 Sep 2019 19:54:53 +0000 (21:54 +0200)
src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt
src/main/resources/templates/linked/audio.html [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilterTest.kt

index 200bd83..5cc602c 100644 (file)
@@ -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 (file)
index 0000000..cd04c5b
--- /dev/null
@@ -0,0 +1 @@
+<span class="linked-element loaded" title="<%link|html>"><audio controls src="/<% link|html>"></audio></span>
index 8363f6b..e93bfeb 100644 (file)
@@ -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<LinkedElementRenderFilter>(), notNullValue())