}
lineComplete = false;
- Matcher matcher = whitespacePattern.matcher(line);
- int nextSpace = matcher.find(0) ? matcher.start() : line.length();
- String link = line.substring(0, nextSpace);
+ int endOfLink = findEndOfLink(line);
+ String link = line.substring(0, endOfLink);
String name = link;
logger.log(Level.FINER, String.format("Found link: %s", link));
}
parts.add(new LinkPart(link, name));
}
- line = line.substring(nextSpace);
+ line = line.substring(endOfLink);
}
lastLineEmpty = false;
}
return parts;
}
+ private int findEndOfLink(String line) {
+ Matcher matcher = whitespacePattern.matcher(line);
+ if (!matcher.find(0)) {
+ return line.length();
+ }
+ int nextWhitespace = matcher.start();
+ int openParens = 0;
+ for (int i = 0; i < nextWhitespace; i++) {
+ switch (line.charAt(i)) {
+ case '(':
+ openParens++;
+ break;
+ case ')':
+ openParens--;
+ if (openParens < 0) {
+ return i;
+ }
+ default:
+ }
+ }
+ return nextWhitespace;
+ }
+
private static class NextLink {
private final int position;
assertThat("Part Text", "Some text. Empty link: http:// – nice!", is(convertText(parts, PlainTextPart.class)));
}
+ @Test
+ public void httpLinkWithoutParensEndsAtNextClosingParen() {
+ SoneTextParser soneTextParser = new SoneTextParser(null, null);
+ Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", "Some text (and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]) – nice!", is(convertText(parts, PlainTextPart.class, LinkPart.class)));
+ }
+
+ @Test
+ public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
+ SoneTextParser soneTextParser = new SoneTextParser(null, null);
+ Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", "Some text (and a link: [http://example.sone/abc_(def)|example.sone/abc_(def)|example.sone/abc_(def)]) – nice!", is(convertText(parts, PlainTextPart.class, LinkPart.class)));
+ }
/**
* Converts all given {@link Part}s into a string, validating that the