Add cut-off length for the snippet.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Sep 2011 17:29:26 +0000 (19:29 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Sep 2011 17:29:26 +0000 (19:29 +0200)
src/main/java/net/pterodactylus/sone/template/ParserFilter.java

index c5f7f6b..b53e969 100644 (file)
@@ -36,6 +36,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;
@@ -88,6 +89,7 @@ public class ParserFilter implements Filter {
        public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
                String text = String.valueOf(data);
                int length = Numbers.safeParseInteger(parameters.get("length"), Numbers.safeParseInteger(templateContext.get(parameters.get("length")), -1));
+               int cutOffLength = Numbers.safeParseInteger(parameters.get("cut-off-length"), Numbers.safeParseInteger(templateContext.get(parameters.get("cut-off-length")), length));
                String soneKey = parameters.get("sone");
                if (soneKey == null) {
                        soneKey = "sone";
@@ -102,27 +104,33 @@ public class ParserFilter implements Filter {
                try {
                        Iterable<Part> parts = soneTextParser.parse(context, new StringReader(text));
                        if (length > -1) {
+                               int allPartsLength = 0;
                                List<Part> shortenedParts = new ArrayList<Part>();
                                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) {