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=42e40356dcbb0d22fbc762971dd2f710dbeb623f;hpb=66a45de8c5f247d03382a570e960ac3e8cee8c79;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 42e4035..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
@@ -20,7 +20,9 @@ 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;
@@ -36,6 +38,7 @@ 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;
@@ -59,10 +62,10 @@ public class ParserFilter implements Filter {
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}
@@ -81,60 +84,48 @@ public class ParserFilter implements Filter {
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);
- 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";
- }
- 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();
}
FreenetRequest request = (FreenetRequest) templateContext.get("request");
- SoneTextParserContext context = new SoneTextParserContext(request, sone);
+ 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 (length >= longText.length()) {
- shortenedParts.add(part);
- } else {
- shortenedParts.add(new PlainTextPart(longText.substring(0, length) + "â¦"));
+ if (allPartsLength < cutOffLength) {
+ if ((allPartsLength + longText.length()) > cutOffLength) {
+ shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "â¦"));
+ } else {
+ shortenedParts.add(part);
+ }
}
- length -= longText.length();
+ allPartsLength += longText.length();
} else if (part instanceof LinkPart) {
- shortenedParts.add(part);
- length -= ((LinkPart) part).getText().length();
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
+ allPartsLength += ((LinkPart) part).getText().length();
} else {
- shortenedParts.add(part);
- }
- if (length <= 0) {
- break;
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
}
}
- parts = shortenedParts;
+ if (allPartsLength >= length) {
+ parts = shortenedParts;
+ }
}
render(parsedTextWriter, parts);
} catch (IOException ioe1) {
@@ -221,7 +212,12 @@ public class ParserFilter implements Filter {
* 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);
+ }
}
/**
@@ -249,7 +245,28 @@ public class ParserFilter implements Filter {
* 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(null, 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. */
+ }
}
/**
@@ -275,26 +292,4 @@ public class ParserFilter implements Filter {
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 filteredText;
- }
-
}