From 2241b13275a0dec86461aba67db92825424b9f1b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 15 Aug 2016 20:50:57 +0200 Subject: [PATCH] Ignore punctuation at end of links --- .../java/net/pterodactylus/sone/text/SoneTextParser.java | 11 +++++++++++ .../net/pterodactylus/sone/text/SoneTextParserTest.java | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index b675c12..6752c12 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -296,6 +296,13 @@ public class SoneTextParser implements Parser { 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)) { @@ -313,6 +320,10 @@ public class SoneTextParser implements Parser { return nextWhitespace; } + private boolean isPunctuation(char character) { + return character == '.'; + } + private static class NextLink { private final int position; diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 8d83c95..fb66f86 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -124,6 +124,22 @@ public class SoneTextParserTest { 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))); } + @Test + public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() { + SoneTextParser soneTextParser = new SoneTextParser(null, null); + Iterable 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 multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() { + SoneTextParser soneTextParser = new SoneTextParser(null, null); + Iterable 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))); + } + /** * Converts all given {@link Part}s into a string, validating that the * part’s classes match only the expected classes. -- 2.7.4