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=f1190e054fafa8c54e647fbb8bc304d78614453e;hpb=9e4db46b86d084eba9029906e779ec1d96f78ac4;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 f1190e0..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,56 +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 link parser. */ - private final FreenetLinkParser linkParser; + private final Core core; + private final SoneTextParser soneTextParser; - /** - * Creates a new filter that runs its input through a - * {@link FreenetLinkParser}. - * - * @param templateContextFactory - * The context factory for rendering the parts - */ - public ParserFilter(TemplateContextFactory templateContextFactory) { - linkParser = new FreenetLinkParser(templateContextFactory); + public ParserFilter(Core core, SoneTextParser soneTextParser) { + this.core = core; + 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"; + 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(); } - Sone sone = (Sone) templateContext.get(soneKey); - 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; } }