import java.util.regex.Pattern;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.template.TemplateFactory;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
import freenet.keys.FreenetURI;
/**
}
/** The template factory. */
- private final TemplateFactory templateFactory;
+ private final TemplateContextFactory templateContextFactory;
/**
* Creates a new freenet link parser.
*
- * @param templateFactory
- * The template factory
+ * @param templateContextFactory
+ * The template context factory
*/
- public FreenetLinkParser(TemplateFactory templateFactory) {
- this.templateFactory = templateFactory;
+ public FreenetLinkParser(TemplateContextFactory templateContextFactory) {
+ this.templateContextFactory = templateContextFactory;
}
//
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";
+ if (line.trim().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@");
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;
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);
}
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;
}
* @return The part that displays the given text
*/
private Part createPlainTextPart(String text) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<% text|html>"))).set("text", text);
}
/**
* @return The part that displays the link
*/
private Part createInternetLinkPart(String link, String name) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("<a class=\"internet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"internet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
}
/**
* @return The part that displays the link
*/
private Part createFreenetLinkPart(String link, String name) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("<a class=\"freenet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"freenet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
}
/**
* @return The part that displays the link
*/
private Part createTrustedFreenetLinkPart(String link, String name) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("<a class=\"freenet-trusted\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"freenet-trusted\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
}
}