From 10e65e7290209c04d6dd9ede70205e33f4ce66c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 16 Jan 2011 15:36:39 +0100 Subject: [PATCH] Add filter to pipe arbitrary text through the link parser. --- .../pterodactylus/sone/template/ParserFilter.java | 71 ++++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 2 + 2 files changed, 73 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/template/ParserFilter.java diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java new file mode 100644 index 0000000..6099ee7 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -0,0 +1,71 @@ +/* + * Sone - ParserFilter.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.text.FreenetLinkParser; +import net.pterodactylus.sone.text.FreenetLinkParserContext; +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.Filter; +import net.pterodactylus.util.template.TemplateFactory; + +/** + * Filter that filters a given text through a {@link FreenetLinkParser}. + * + * @author David ‘Bombe’ Roden + */ +public class ParserFilter implements Filter { + + /** The link parser. */ + private final FreenetLinkParser linkParser; + + /** + * Creates a new parser filter. + * + * @param templateFactory + * The template factory for the link parser + */ + public ParserFilter(TemplateFactory templateFactory) { + this.linkParser = new FreenetLinkParser(templateFactory); + } + + /** + * {@inheritDoc} + */ + @Override + public Object format(DataProvider dataProvider, Object data, Map parameters) { + String text = String.valueOf(data); + String soneKey = parameters.get("sone"); + if (soneKey == null) { + soneKey = "sone"; + } + Sone sone = (Sone) dataProvider.get(soneKey); + FreenetLinkParserContext context = new FreenetLinkParserContext(sone); + try { + return linkParser.parse(context, new StringReader(text)); + } catch (IOException ioe1) { + /* no exceptions in a StringReader, ignore. */ + } + return null; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index adcc17e..92aa998 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -50,6 +50,7 @@ import net.pterodactylus.sone.template.GetPagePlugin; import net.pterodactylus.sone.template.IdentityAccessor; import net.pterodactylus.sone.template.JavascriptFilter; import net.pterodactylus.sone.template.NotificationManagerAccessor; +import net.pterodactylus.sone.template.ParserFilter; import net.pterodactylus.sone.template.PostAccessor; import net.pterodactylus.sone.template.ReplyAccessor; import net.pterodactylus.sone.template.RequestChangeFilter; @@ -182,6 +183,7 @@ public class WebInterface implements CoreListener { templateFactory.addFilter("match", new MatchFilter()); templateFactory.addFilter("css", new CssClassNameFilter()); templateFactory.addFilter("js", new JavascriptFilter()); + templateFactory.addFilter("parse", new ParserFilter(templateFactory)); templateFactory.addPlugin("getpage", new GetPagePlugin()); templateFactory.addPlugin("paginate", new PaginationPlugin()); templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory)); -- 2.7.4