X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilter.java;h=3900ad8144d814cf68eb7331109b8dc4f4a424c4;hb=d35ab97a494fd187d3b8fe6b3253b65e3427a077;hp=1e274ccbc2c9d11c15135da58ff9a763cc2a08e9;hpb=47d2becfd53af8314accaee3b1aaa3c7a7ef401e;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..3900ad8 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
@@ -19,18 +19,34 @@ package net.pterodactylus.sone.template;
import java.io.IOException;
import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.net.URLEncoder;
+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.number.Numbers;
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,43 +56,240 @@ 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 static final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
+
+ /** The template for {@link FreenetLinkPart}s. */
+ private static 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(templateContextFactory);
+ this.templateContextFactory = templateContextFactory;
+ this.soneTextParser = soneTextParser;
}
- /**
- * {@inheritDoc}
- */
@Override
- public Object format(TemplateContext templateContext, Object data, Map parameters) {
+ public Object format(TemplateContext templateContext, Object data, Map parameters) {
String text = String.valueOf(data);
- String soneKey = parameters.get("sone");
- if (soneKey == null) {
- soneKey = "sone";
+ int length = Numbers.safeParseInteger(parameters.get("length"), Numbers.safeParseInteger(templateContext.get(String.valueOf(parameters.get("length"))), -1));
+ int cutOffLength = Numbers.safeParseInteger(parameters.get("cut-off-length"), Numbers.safeParseInteger(templateContext.get(String.valueOf(parameters.get("cut-off-length"))), length));
+ Object sone = parameters.get("sone");
+ if (sone instanceof String) {
+ sone = core.getSone((String) sone).orNull();
+ }
+ FreenetRequest request = (FreenetRequest) templateContext.get("request");
+ SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
+ StringWriter parsedTextWriter = new StringWriter();
+ try {
+ Iterable parts = soneTextParser.parse(context, new StringReader(text));
+ if (length > -1) {
+ int allPartsLength = 0;
+ List shortenedParts = new ArrayList();
+ for (Part part : parts) {
+ if (part instanceof PlainTextPart) {
+ String longText = ((PlainTextPart) part).getText();
+ if (allPartsLength < cutOffLength) {
+ if ((allPartsLength + longText.length()) > cutOffLength) {
+ shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "â¦"));
+ } else {
+ shortenedParts.add(part);
+ }
+ }
+ allPartsLength += longText.length();
+ } else if (part instanceof LinkPart) {
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
+ allPartsLength += ((LinkPart) part).getText().length();
+ } else {
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
+ }
+ }
+ if (allPartsLength >= length) {
+ parts = shortenedParts;
+ }
+ }
+ render(parsedTextWriter, parts);
+ } catch (IOException ioe1) {
+ /* no exceptions in a StringReader or StringWriter, ignore. */
}
- Sone sone = (Sone) templateContext.get(soneKey);
- if (sone == null) {
- sone = core.getSone(soneKey, false);
+ 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);
}
- FreenetLinkParserContext context = new FreenetLinkParserContext(sone);
+ }
+
+ /**
+ * 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) {
+ try {
+ renderLink(writer, "/external-link/?_CHECKED_HTTP_=" + URLEncoder.encode(linkPart.getLink(), "UTF-8"), linkPart.getText(), linkPart.getTitle(), "internet");
+ } catch (UnsupportedEncodingException uee1) {
+ /* not possible for UTF-8. */
+ throw new RuntimeException("The JVM does not support UTF-8 encoding!", uee1);
+ }
+ }
+
+ /**
+ * 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) {
+ if ((sonePart.getSone() != null) && (sonePart.getSone().getName() != null)) {
+ renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
+ } else {
+ renderLink(writer, "/WebOfTrust/ShowIdentity?id=" + sonePart.getSone().getId(), sonePart.getSone().getId(), sonePart.getSone().getId(), "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) {
+ SoneTextParser parser = new SoneTextParser(core.getDatabase());
+ SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
try {
- return linkParser.parse(context, new StringReader(text));
+ Iterable parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
+ StringBuilder excerpt = new StringBuilder();
+ for (Part part : parts) {
+ excerpt.append(part.getText());
+ if (excerpt.length() > 20) {
+ int lastSpace = excerpt.lastIndexOf(" ", 20);
+ if (lastSpace > -1) {
+ excerpt.setLength(lastSpace);
+ } else {
+ excerpt.setLength(20);
+ }
+ excerpt.append("â¦");
+ break;
+ }
+ }
+ renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
} catch (IOException ioe1) {
- /* no exceptions in a StringReader, ignore. */
+ /* StringReader shouldnât throw. */
}
- return null;
+ }
+
+ /**
+ * 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);
}
}