FreenetRequest request = (FreenetRequest) templateContext.get("request");
SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
StringWriter parsedTextWriter = new StringWriter();
- try {
- Iterable<Part> parts = soneTextParser.parse(context, text);
- if (length > -1) {
- int allPartsLength = 0;
- List<Part> shortenedParts = new ArrayList<Part>();
- for (Part part : parts) {
- if (part instanceof PlainTextPart) {
- String longText = part.getText();
- if (allPartsLength < cutOffLength) {
- if ((allPartsLength + longText.length()) > cutOffLength) {
- shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
- } else {
- shortenedParts.add(part);
- }
- }
- allPartsLength += longText.length();
- } else if (part instanceof LinkPart) {
- if (allPartsLength < cutOffLength) {
- shortenedParts.add(part);
- }
- allPartsLength += part.getText().length();
- } else {
- if (allPartsLength < cutOffLength) {
+ Iterable<Part> parts = soneTextParser.parse(context, text);
+ if (length > -1) {
+ int allPartsLength = 0;
+ List<Part> shortenedParts = new ArrayList<Part>();
+ for (Part part : parts) {
+ if (part instanceof PlainTextPart) {
+ String longText = part.getText();
+ if (allPartsLength < cutOffLength) {
+ if ((allPartsLength + longText.length()) > cutOffLength) {
+ shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
+ } else {
shortenedParts.add(part);
}
}
+ allPartsLength += longText.length();
+ } else if (part instanceof LinkPart) {
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
+ allPartsLength += part.getText().length();
+ } else {
+ if (allPartsLength < cutOffLength) {
+ shortenedParts.add(part);
+ }
}
- if (allPartsLength >= length) {
- parts = shortenedParts;
- }
}
- render(parsedTextWriter, parts);
- } catch (IOException ioe1) {
- /* no exceptions in a StringReader or StringWriter, ignore. */
+ if (allPartsLength >= length) {
+ parts = shortenedParts;
+ }
}
+ render(parsedTextWriter, parts);
return parsedTextWriter.toString();
}
private void render(Writer writer, PostPart postPart) {
SoneTextParser parser = new SoneTextParser(core, core);
SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
- try {
- Iterable<Part> parts = parser.parse(parserContext, 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;
+ Iterable<Part> parts = parser.parse(parserContext, 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. */
}
+ renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
}
/**
import static java.util.logging.Logger.getLogger;
import static net.pterodactylus.util.template.TemplateParser.parse;
-import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
-import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
private Collection<Sone> getMentionedSones(String text) {
/* we need no context to find mentioned Sones. */
Set<Sone> mentionedSones = new HashSet<Sone>();
- try {
- for (Part part : soneTextParser.parse(null, text)) {
- if (part instanceof SonePart) {
- mentionedSones.add(((SonePart) part).getSone());
- }
+ for (Part part : soneTextParser.parse(null, text)) {
+ if (part instanceof SonePart) {
+ mentionedSones.add(((SonePart) part).getSone());
}
- } catch (IOException ioe1) {
- logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
}
return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
}