Add reparse filter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Jul 2015 15:29:17 +0000 (17:29 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Jul 2015 15:29:17 +0000 (17:29 +0200)
src/main/java/net/pterodactylus/sone/main/ReparseFilter.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/test/java/net/pterodactylus/sone/main/ReparseFilterTest.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sone/main/ReparseFilter.java b/src/main/java/net/pterodactylus/sone/main/ReparseFilter.java
new file mode 100644 (file)
index 0000000..acad00b
--- /dev/null
@@ -0,0 +1,33 @@
+package net.pterodactylus.sone.main;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Map;
+
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateException;
+import net.pterodactylus.util.template.TemplateParser;
+
+/**
+ * Takes the input and parses it as a new {@link Template}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ReparseFilter implements Filter {
+
+       @Override
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               Template template = TemplateParser.parse(new StringReader(String.valueOf(data)));
+               StringWriter stringWriter = new StringWriter();
+               try {
+                       template.render(templateContext, stringWriter);
+               } catch (TemplateException e) {
+                       throw new RuntimeException(e);
+               } finally {
+                       return stringWriter.toString();
+               }
+       }
+
+}
index 1ce8f8e..e813179 100644 (file)
@@ -72,6 +72,7 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.Trust;
+import net.pterodactylus.sone.main.ReparseFilter;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.sone.notify.ListNotification;
 import net.pterodactylus.sone.template.AlbumAccessor;
@@ -274,6 +275,7 @@ public class WebInterface {
                templateContextFactory.addFilter("css", new CssClassNameFilter());
                templateContextFactory.addFilter("js", new JavascriptFilter());
                templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
+               templateContextFactory.addFilter("reparse", new ReparseFilter());
                templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
                templateContextFactory.addFilter("format", new FormatFilter());
                templateContextFactory.addFilter("sort", new CollectionSortFilter());
diff --git a/src/test/java/net/pterodactylus/sone/main/ReparseFilterTest.java b/src/test/java/net/pterodactylus/sone/main/ReparseFilterTest.java
new file mode 100644 (file)
index 0000000..879fe36
--- /dev/null
@@ -0,0 +1,29 @@
+package net.pterodactylus.sone.main;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import java.util.Collections;
+
+import net.pterodactylus.util.template.TemplateContext;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ReparseFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ReparseFilterTest {
+
+       private final ReparseFilter reparseFilter = new ReparseFilter();
+
+       @Test
+       public void filterParsesTemplateCorrectly() {
+               TemplateContext templateContext = new TemplateContext();
+               templateContext.set("bar", "baz");
+               assertThat(reparseFilter.format(templateContext, "foo <% bar>", Collections.<String, Object>emptyMap()),
+                               is((Object) "foo baz"));
+       }
+
+}