Include templates for rendering linked elements
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 22 Nov 2016 20:55:44 +0000 (21:55 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Nov 2016 05:55:22 +0000 (06:55 +0100)
src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt
src/main/resources/templates/linked/image.html [new file with mode: 0644]
src/main/resources/templates/linked/notLoaded.html [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilterTest.kt

index 6644ad8..a356028 100644 (file)
@@ -14,8 +14,8 @@ import java.io.StringWriter
 class LinkedElementRenderFilter(private val templateContextFactory: TemplateContextFactory) : Filter {
 
        companion object {
-               private val loadedImageTemplate = """<a href="/<% link|html>"><span class="linked-element" title="<% link|html>" style="background-image: url('/<% link|html>')"></span></a>""".parse()
-               private val notLoadedImageTemplate = """<span class="linked-element" title="<% link|html>" style="background-image: url('images/loading-animation.gif')"></span>""".parse()
+               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) }
        }
diff --git a/src/main/resources/templates/linked/image.html b/src/main/resources/templates/linked/image.html
new file mode 100644 (file)
index 0000000..17fa28a
--- /dev/null
@@ -0,0 +1 @@
+<a href="/<% link|html>"><span class="linked-element" title="<% link|html>" style="background-image: url('/<% link|html>')"></span></a>
diff --git a/src/main/resources/templates/linked/notLoaded.html b/src/main/resources/templates/linked/notLoaded.html
new file mode 100644 (file)
index 0000000..87ee516
--- /dev/null
@@ -0,0 +1 @@
+<span class="linked-element" title="<% link|html>" style="background-image: url('images/loading-animation.gif')"></span>
index c0c83b3..9127f58 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.sone.template
 
 import net.pterodactylus.sone.core.LinkedElement
+import net.pterodactylus.util.template.ClassPathTemplateProvider
 import net.pterodactylus.util.template.HtmlFilter
 import net.pterodactylus.util.template.TemplateContextFactory
 import org.hamcrest.MatcherAssert.assertThat
@@ -18,6 +19,7 @@ class LinkedElementRenderFilterTest {
 
        init {
                templateContextFactory.addFilter("html", HtmlFilter())
+               templateContextFactory.addProvider(ClassPathTemplateProvider(LinkedElementRenderFilter::class.java, "/templates/"))
        }
 
        private val filter = LinkedElementRenderFilter(templateContextFactory)