X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilter.java;h=a53184369cdd78d493a2a1ffe89447bd34cdc411;hb=15b175ec7ed0423bc5a58d1306062458ab0b2a04;hp=2030807174a2b85b0c68e4466ba44433baacb4ee;hpb=38c1d04aefb25fa4418cfd9e9ce3474b6cd2696a;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 2030807..a531843 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
@@ -20,7 +20,11 @@ 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;
@@ -33,7 +37,8 @@ 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.Page.Request;
+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;
@@ -51,16 +56,16 @@ public class ParserFilter implements Filter {
private final Core core;
/** The link parser. */
- private final SoneTextParser textParser;
+ 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>"));
+ private static 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>"));
+ 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 SoneTextParser}
@@ -70,32 +75,59 @@ public class ParserFilter implements Filter {
* 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;
this.templateContextFactory = templateContextFactory;
- textParser = new SoneTextParser(core);
+ 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";
- }
- Sone sone = (Sone) templateContext.get(soneKey);
- if (sone == null) {
- sone = core.getSone(soneKey, false);
+ 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();
}
- Request request = (Request) templateContext.get("request");
- SoneTextParserContext context = new SoneTextParserContext(request, sone);
+ FreenetRequest request = (FreenetRequest) templateContext.get("request");
+ SoneTextParserContext context = new SoneTextParserContext((Sone) sone);
StringWriter parsedTextWriter = new StringWriter();
try {
- render(parsedTextWriter, textParser.parse(context, new StringReader(text)));
+ 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. */
}
@@ -106,12 +138,29 @@ public class ParserFilter implements Filter {
// 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);
@@ -128,28 +177,112 @@ public class ParserFilter implements Filter {
}
}
+ /**
+ * 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");
+ 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) {
- renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
+ 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) {
- renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), getExcerpt(postPart.getPost().getText(), 20), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
+ SoneTextParser parser = new SoneTextParser(core.getDatabase());
+ SoneTextParserContext parserContext = new SoneTextParserContext(postPart.getPost().getSone());
+ try {
+ 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) {
+ /* StringReader shouldnât throw. */
+ }
}
+ /**
+ * 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);
@@ -159,15 +292,4 @@ public class ParserFilter implements Filter {
linkTemplate.render(templateContext, writer);
}
- //
- // STATIC METHODS
- //
-
- private static String getExcerpt(String text, int length) {
- if (text.length() > length) {
- return text.substring(0, length) + "â¦";
- }
- return text;
- }
-
}