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=83023699b05c8e0a880294b62077a832654d33a9;hpb=0f7a91f4fa689a4084d7db5987a534b4b4fc3866;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 8302369..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â2012 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;
@@ -60,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}
@@ -82,9 +84,6 @@ public class ParserFilter implements Filter {
this.soneTextParser = soneTextParser;
}
- /**
- * {@inheritDoc}
- */
@Override
public Object format(TemplateContext templateContext, Object data, Map parameters) {
String text = String.valueOf(data);
@@ -92,7 +91,7 @@ public class ParserFilter implements Filter {
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, false);
+ sone = core.getSone((String) sone).orNull();
}
FreenetRequest request = (FreenetRequest) templateContext.get("request");
SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
@@ -213,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);
+ }
}
/**
@@ -241,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. */
+ }
}
/**
@@ -267,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;
- }
-
}