From ec71e06d5c77825301236fff06558c9c9e92ad23 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 1 Apr 2011 11:25:58 +0200 Subject: [PATCH] Collapse multiple empty lines to one empty line at maximum. This also strips empty lines at the beginning and the end of texts. --- .../pterodactylus/sone/text/FreenetLinkParser.java | 41 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java index 704792c..ad9381d 100644 --- a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java +++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java @@ -97,8 +97,21 @@ public class FreenetLinkParser implements Parser { PartContainer parts = new PartContainer(); BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source); String line; + boolean lastLineEmpty = true; + int emptyLines = 0; while ((line = bufferedReader.readLine()) != null) { - line = line.trim() + "\n"; + line = line.trim(); + if (line.length() == 0) { + if (lastLineEmpty) { + continue; + } + parts.add(createPlainTextPart("\n")); + ++emptyLines; + lastLineEmpty = emptyLines == 2; + continue; + } + emptyLines = 0; + boolean lineComplete = true; while (line.length() > 0) { int nextKsk = line.indexOf("KSK@"); int nextChk = line.indexOf("CHK@"); @@ -107,7 +120,11 @@ public class FreenetLinkParser implements Parser { int nextHttp = line.indexOf("http://"); int nextHttps = line.indexOf("https://"); if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1) && (nextHttp == -1) && (nextHttps == -1)) { - parts.add(createPlainTextPart(line)); + if (lineComplete && !lastLineEmpty) { + parts.add(createPlainTextPart("\n" + line)); + } else { + parts.add(createPlainTextPart(line)); + } break; } int next = Integer.MAX_VALUE; @@ -143,7 +160,11 @@ public class FreenetLinkParser implements Parser { Matcher matcher = whitespacePattern.matcher(line); int nextSpace = matcher.find(next) ? matcher.start() : line.length(); if (nextSpace > (next + 4)) { - parts.add(createPlainTextPart(line.substring(0, next))); + if (!lastLineEmpty && lineComplete) { + parts.add(createPlainTextPart("\n" + line.substring(0, next))); + } else { + parts.add(createPlainTextPart(line.substring(0, next))); + } String link = line.substring(next, nextSpace); String name = link; logger.log(Level.FINER, "Found link: %s", link); @@ -199,10 +220,22 @@ public class FreenetLinkParser implements Parser { } line = line.substring(nextSpace); } else { - parts.add(createPlainTextPart(line.substring(0, next + 4))); + if (!lastLineEmpty && lineComplete) { + parts.add(createPlainTextPart("\n" + line.substring(0, next + 4))); + } else { + parts.add(createPlainTextPart(line.substring(0, next + 4))); + } line = line.substring(next + 4); } + lineComplete = false; + } + lastLineEmpty = false; + } + for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) { + if (!parts.getPart(partIndex).toString().equals("\n")) { + break; } + parts.removePart(partIndex); } return parts; } -- 2.7.4