From 89fe86e5fa64ad57b90b9b898e4fc03d29da5a63 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 18 Jul 2015 17:29:17 +0200 Subject: [PATCH] Add reparse filter --- .../net/pterodactylus/sone/main/ReparseFilter.java | 33 ++++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 2 ++ .../pterodactylus/sone/main/ReparseFilterTest.java | 29 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/main/ReparseFilter.java create mode 100644 src/test/java/net/pterodactylus/sone/main/ReparseFilterTest.java 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 index 0000000..acad00b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/main/ReparseFilter.java @@ -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 David ‘Bombe’ Roden + */ +public class ReparseFilter implements Filter { + + @Override + public Object format(TemplateContext templateContext, Object data, Map 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(); + } + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 1ce8f8e..e813179 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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 index 0000000..879fe36 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/main/ReparseFilterTest.java @@ -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 David ‘Bombe’ Roden + */ +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.emptyMap()), + is((Object) "foo baz")); + } + +} -- 2.7.4