X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilter.java;h=3494c2186d44e261f8255c37edd0e94dd4e5f128;hb=5e30dc1ca805f1a2d87b6c752da68a57275cb159;hp=477e97275724cf99462d698b3cc9340405308ccf;hpb=58eeba9b521b0a0094ac90a37fd88811c6a27376;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 477e972..3494c21 100644
--- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java
+++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java
@@ -19,18 +19,31 @@ package net.pterodactylus.sone.template;
import java.io.IOException;
import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
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.FreenetLinkPart;
+import net.pterodactylus.sone.text.LinkPart;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+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.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
/**
- * Filter that filters a given text through a {@link FreenetLinkParser}.
+ * Filter that filters a given text through a {@link SoneTextParser}.
*
* @author David âBombeâ Roden
*/
@@ -40,20 +53,32 @@ public class ParserFilter implements Filter {
private final Core core;
/** The link parser. */
- private final FreenetLinkParser linkParser;
+ private final SoneTextParser soneTextParser;
+
+ /** The template context factory. */
+ private final TemplateContextFactory templateContextFactory;
+
+ /** The template for {@link PlainTextPart}s. */
+ private final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
+
+ /** The template for {@link FreenetLinkPart}s. */
+ private final Template linkTemplate = TemplateParser.parse(new StringReader("\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html>"));
/**
- * Creates a new filter that runs its input through a
- * {@link FreenetLinkParser}.
+ * Creates a new filter that runs its input through a {@link SoneTextParser}
+ * .
*
* @param core
* The core
* @param templateContextFactory
* The context factory for rendering the parts
+ * @param soneTextParser
+ * The Sone text parser
*/
- public ParserFilter(Core core, TemplateContextFactory templateContextFactory) {
+ public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
this.core = core;
- linkParser = new FreenetLinkParser(core, templateContextFactory);
+ this.templateContextFactory = templateContextFactory;
+ this.soneTextParser = soneTextParser;
}
/**
@@ -62,6 +87,19 @@ public class ParserFilter implements Filter {
@Override
public Object format(TemplateContext templateContext, Object data, Map parameters) {
String text = String.valueOf(data);
+ int length = -1;
+ try {
+ length = Integer.parseInt(parameters.get("length"));
+ } catch (NumberFormatException nfe1) {
+ /* ignore. */
+ }
+ if ((length == -1) && (parameters.get("length") != null)) {
+ try {
+ length = Integer.parseInt(String.valueOf(templateContext.get(parameters.get("length"))));
+ } catch (NumberFormatException nfe1) {
+ /* ignore. */
+ }
+ }
String soneKey = parameters.get("sone");
if (soneKey == null) {
soneKey = "sone";
@@ -70,13 +108,189 @@ public class ParserFilter implements Filter {
if (sone == null) {
sone = core.getSone(soneKey, false);
}
- FreenetLinkParserContext context = new FreenetLinkParserContext(sone);
+ FreenetRequest request = (FreenetRequest) templateContext.get("request");
+ SoneTextParserContext context = new SoneTextParserContext(request, sone);
+ StringWriter parsedTextWriter = new StringWriter();
try {
- return linkParser.parse(context, new StringReader(text));
+ Iterable parts = soneTextParser.parse(context, new StringReader(text));
+ if (length > -1) {
+ List shortenedParts = new ArrayList();
+ for (Part part : parts) {
+ if (part instanceof PlainTextPart) {
+ String longText = ((PlainTextPart) part).getText();
+ if (length >= longText.length()) {
+ shortenedParts.add(part);
+ } else {
+ shortenedParts.add(new PlainTextPart(longText.substring(0, length) + "â¦"));
+ }
+ length -= longText.length();
+ } else if (part instanceof LinkPart) {
+ shortenedParts.add(part);
+ length -= ((LinkPart) part).getText().length();
+ } else {
+ shortenedParts.add(part);
+ }
+ if (length <= 0) {
+ break;
+ }
+ }
+ parts = shortenedParts;
+ }
+ render(parsedTextWriter, parts);
} catch (IOException ioe1) {
- /* no exceptions in a StringReader, ignore. */
+ /* no exceptions in a StringReader or StringWriter, ignore. */
+ }
+ return parsedTextWriter.toString();
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Renders the given parts.
+ *
+ * @param writer
+ * The writer to render the parts to
+ * @param parts
+ * The parts to render
+ */
+ private void render(Writer writer, Iterable parts) {
+ for (Part part : parts) {
+ render(writer, part);
+ }
+ }
+
+ /**
+ * Renders the given part.
+ *
+ * @param writer
+ * The writer to render the part to
+ * @param part
+ * The part to render
+ */
+ @SuppressWarnings("unchecked")
+ private void render(Writer writer, Part part) {
+ if (part instanceof PlainTextPart) {
+ render(writer, (PlainTextPart) part);
+ } else if (part instanceof FreenetLinkPart) {
+ render(writer, (FreenetLinkPart) part);
+ } else if (part instanceof LinkPart) {
+ render(writer, (LinkPart) part);
+ } else if (part instanceof SonePart) {
+ render(writer, (SonePart) part);
+ } else if (part instanceof PostPart) {
+ render(writer, (PostPart) part);
+ } else if (part instanceof Iterable>) {
+ render(writer, (Iterable) part);
+ }
+ }
+
+ /**
+ * Renders the given plain-text part.
+ *
+ * @param writer
+ * The writer to render the part to
+ * @param plainTextPart
+ * The part to render
+ */
+ private void render(Writer writer, PlainTextPart plainTextPart) {
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("text", plainTextPart.getText());
+ plainTextTemplate.render(templateContext, writer);
+ }
+
+ /**
+ * Renders the given freenet link part.
+ *
+ * @param writer
+ * The writer to render the part to
+ * @param freenetLinkPart
+ * The part to render
+ */
+ private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
+ renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
+ }
+
+ /**
+ * Renders the given link part.
+ *
+ * @param writer
+ * The writer to render the part to
+ * @param linkPart
+ * The part to render
+ */
+ private void render(Writer writer, LinkPart linkPart) {
+ renderLink(writer, "/?_CHECKED_HTTP_=" + linkPart.getLink(), linkPart.getText(), linkPart.getTitle(), "internet");
+ }
+
+ /**
+ * Renders the given Sone part.
+ *
+ * @param writer
+ * The writer to render the part to
+ * @param sonePart
+ * The part to render
+ */
+ private void render(Writer writer, SonePart sonePart) {
+ renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
+ }
+
+ /**
+ * Renders the given post part.
+ *
+ * @param writer
+ * The writer to render the part to
+ * @param postPart
+ * The part to render
+ */
+ private void render(Writer writer, PostPart postPart) {
+ renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), getExcerpt(postPart.getPost().getText(), 20), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
+ }
+
+ /**
+ * Renders the given link.
+ *
+ * @param writer
+ * The writer to render the link to
+ * @param link
+ * The link to render
+ * @param text
+ * The text of the link
+ * @param title
+ * The title of the link
+ * @param cssClass
+ * The CSS class of the link
+ */
+ private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("cssClass", cssClass);
+ templateContext.set("link", link);
+ templateContext.set("text", text);
+ templateContext.set("title", title);
+ linkTemplate.render(templateContext, writer);
+ }
+
+ //
+ // STATIC METHODS
+ //
+
+ /**
+ * Returns up to {@code length} characters from the given text, appending
+ * ââ¦â if the text is longer.
+ *
+ * @param text
+ * The text to get an excerpt from
+ * @param length
+ * The maximum length of the excerpt (without the ellipsis)
+ * @return The excerpt of the text
+ */
+ private static String getExcerpt(String text, int length) {
+ String filteredText = text.replaceAll("(\r\n)+", "\r\n").replaceAll("\n+", "\n").replace("\r\n", " ").replace('\n', ' ');
+ if (filteredText.length() > length) {
+ return filteredText.substring(0, length) + "â¦";
}
- return null;
+ return filteredText;
}
}