From b5ad0f8bfb68524d16e103438a32024016352bd5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 27 Sep 2011 19:29:26 +0200 Subject: [PATCH] Add cut-off length for the snippet. --- .../pterodactylus/sone/template/ParserFilter.java | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index c5f7f6b..b53e969 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -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 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 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) { -- 2.7.4