X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilter.java;h=26254412eebe335ef703dce2382230b2b96be6d5;hb=5777a17f983636923bc2231daec8d0383955d1f5;hp=1e274ccbc2c9d11c15135da58ff9a763cc2a08e9;hpb=33f333b35a73d3d4a4e79f41e9dd7b342db87b1a;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index 1e274cc..2625441 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -1,5 +1,5 @@ /* - * Sone - ParserFilter.java - Copyright © 2011 David Roden + * Sone - ParserFilter.java - Copyright © 2011–2013 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 @@ -17,66 +17,55 @@ package net.pterodactylus.sone.template; +import static java.lang.String.valueOf; + import java.io.IOException; import java.io.StringReader; +import java.util.Collections; import java.util.Map; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.text.FreenetLinkParser; -import net.pterodactylus.sone.text.FreenetLinkParserContext; +import net.pterodactylus.sone.text.Part; +import net.pterodactylus.sone.text.SoneTextParser; +import net.pterodactylus.sone.text.SoneTextParserContext; +import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Filter; import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.template.TemplateContextFactory; /** - * Filter that filters a given text through a {@link FreenetLinkParser}. + * Filter that filters a given text through a {@link SoneTextParser} and returns the parsed {@link Part}s. * * @author David ‘Bombe’ Roden */ public class ParserFilter implements Filter { - /** The core. */ private final Core core; + private final SoneTextParser soneTextParser; - /** The link parser. */ - private final FreenetLinkParser linkParser; - - /** - * Creates a new filter that runs its input through a - * {@link FreenetLinkParser}. - * - * @param core - * The core - * @param templateContextFactory - * The context factory for rendering the parts - */ - public ParserFilter(Core core, TemplateContextFactory templateContextFactory) { + public ParserFilter(Core core, SoneTextParser soneTextParser) { this.core = core; - linkParser = new FreenetLinkParser(templateContextFactory); + this.soneTextParser = soneTextParser; } /** * {@inheritDoc} */ @Override - public Object format(TemplateContext templateContext, Object data, Map parameters) { - String text = String.valueOf(data); - String soneKey = parameters.get("sone"); - if (soneKey == null) { - soneKey = "sone"; - } - Sone sone = (Sone) templateContext.get(soneKey); - if (sone == null) { - sone = core.getSone(soneKey, false); + public Object format(TemplateContext templateContext, Object data, Map parameters) { + String text = valueOf(data); + Object sone = parameters.get("sone"); + if (sone instanceof String) { + sone = core.getSone((String) sone).orNull(); } - FreenetLinkParserContext context = new FreenetLinkParserContext(sone); + FreenetRequest request = (FreenetRequest) templateContext.get("request"); + SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone); try { - return linkParser.parse(context, new StringReader(text)); + return soneTextParser.parse(context, new StringReader(text)); } catch (IOException ioe1) { /* no exceptions in a StringReader, ignore. */ + return Collections.emptyList(); } - return null; } }