diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-12-14 21:20:34 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-12-14 21:20:34 +0100 |
commit | 6c6d3caea00810df9fb89de599386a93e8dbc4d4 (patch) | |
tree | 231aafe1f2a2c7b5d08d9eb641df29117aeee62e /src/main/java | |
parent | b9604621ad1e10a593889d307e390e40c7e83f5d (diff) |
change message styling rules
Message styling purposly doesn’t require a whitespace after a closing
tag to make something like ~un~believable work. However it also breaks
_Programmierer_innen_ and other example where the tag is repeated as a
non tag in the word.
Therefor we change the rules that if a closing tag is followed by a higher
order closing tag (a closing tag followed by an end block or white space)
we ignore the first closing tag. But only if we don’t read another tag open.
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/ImStyleParser.java | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/ImStyleParser.java b/src/main/java/de/pixart/messenger/utils/ImStyleParser.java index 9f008d76c..118f3179f 100644 --- a/src/main/java/de/pixart/messenger/utils/ImStyleParser.java +++ b/src/main/java/de/pixart/messenger/utils/ImStyleParser.java @@ -34,10 +34,12 @@ import java.util.Arrays; import java.util.List; public class ImStyleParser { + private final static List<Character> KEYWORDS = Arrays.asList('*', '_', '~', '`'); private final static List<Character> NO_SUB_PARSING_KEYWORDS = Arrays.asList('`'); private final static List<Character> BLOCK_KEYWORDS = Arrays.asList('`'); private final static boolean ALLOW_EMPTY = false; + private final static boolean PARSE_HIGHER_ORDER_END = true; public static List<Style> parse(CharSequence text) { return parse(text, 0, text.length() - 1); @@ -86,6 +88,28 @@ public class ImStyleParser { for (int i = start; i <= end; ++i) { char c = text.charAt(i); if (c == needle && !Character.isWhitespace(text.charAt(i - 1))) { + if (!PARSE_HIGHER_ORDER_END || followedByWhitespace(text, i, end)) { + return i; + } else { + int higherOrder = seekHigherOrderEndWithoutNewBeginning(text, needle, i + 1, end); + if (higherOrder != -1) { + return higherOrder; + } + return i; + } + } else if (c == '\n') { + return -1; + } + } + return -1; + } + + private static int seekHigherOrderEndWithoutNewBeginning(CharSequence text, char needle, int start, int end) { + for (int i = start; i <= end; ++i) { + char c = text.charAt(i); + if (c == needle && precededByWhiteSpace(text, i, start) && !followedByWhitespace(text, i, end)) { + return -1; // new beginning + } else if (c == needle && !Character.isWhitespace(text.charAt(i - 1)) && followedByWhitespace(text, i, end)) { return i; } else if (c == '\n') { return -1; |