X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FSoneTextParser.java;h=dbfa0f3188838fb52389cf85ec8033d5d1fbc4eb;hp=fb4e7cd33fccfefffa52b1af0270d934d931206b;hb=72413a87b9ac2eb1ea7ec8cfa37cd3ca5d3e5f8b;hpb=3d6cffe82270a1faacf1f0d39c34b11ab316e0db diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index fb4e7cd..dbfa0f3 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -175,9 +175,8 @@ public class SoneTextParser implements Parser { } 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)); @@ -271,7 +270,7 @@ public class SoneTextParser implements Parser { } parts.add(new LinkPart(link, name)); } - line = line.substring(nextSpace); + line = line.substring(endOfLink); } lastLineEmpty = false; } @@ -291,6 +290,40 @@ public class SoneTextParser implements Parser { return parts; } + private int findEndOfLink(String line) { + Matcher matcher = whitespacePattern.matcher(line); + if (!matcher.find(0)) { + return line.length(); + } + int nextWhitespace = matcher.start(); + int lastPunctuation = nextWhitespace; + while (isPunctuation(line.charAt(lastPunctuation - 1))) { + lastPunctuation -= 1; + } + if (lastPunctuation < nextWhitespace) { + return lastPunctuation; + } + 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 boolean isPunctuation(char character) { + return (character == '.') || (character == ','); + } + private static class NextLink { private final int position;