diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-01-07 16:38:03 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-01-07 16:38:03 +0000 |
commit | 7e5edf92b14c90ac22de17810dc6ee3444e11f06 (patch) | |
tree | 7b9295470f38eee2452871f1632c17f9e2a0aa03 /app/src | |
parent | ce503e242a335639e0c8644d4e2a2319be463b28 (diff) | |
download | infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.tar infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.tar.gz infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.tar.bz2 infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.tar.lz infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.tar.xz infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.tar.zst infinity-for-reddit-7e5edf92b14c90ac22de17810dc6ee3444e11f06.zip |
Show superscript correctly.
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java | 51 |
1 files changed, 20 insertions, 31 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java index 2a59f2a5..16fc6b0e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -37,39 +37,28 @@ public class Utils { private static final long YEAR_MILLIS = 12 * MONTH_MILLIS; public static String modifyMarkdown(String markdown) { - StringBuilder regexed = new StringBuilder(markdown.replaceAll("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com$0)").replaceAll("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com/$0)")); - - /*int startIndex = 0; - - Pattern pattern = Pattern.compile("\\^.+"); - Matcher matcher = pattern.matcher(regexed); - // Check all occurrences - while (matcher.find(startIndex)) { - int count = 0; - Pattern pattern2 = Pattern.compile("(\\^\\([^)]+\\))"); - Matcher matcher2 = pattern2.matcher(matcher.group()); - if (matcher2.find()) { - regexed.setCharAt(matcher2.end() + matcher.start() - 1, '^'); - regexed.deleteCharAt(matcher2.start() + matcher.start() + 1); - - startIndex = matcher.start() + matcher2.end(); - String substring = regexed.substring(matcher.start() + matcher2.start() + 1, matcher.start() + matcher2.end() - 2); - String trimmedSubstring = substring.trim(); - regexed.replace(matcher.start() + matcher2.start() + 1, matcher.start() + matcher2.end() - 2, trimmedSubstring); - startIndex -= (substring.length() - trimmedSubstring.length()); - continue; - } - - regexed.insert(matcher.end(), '^'); - - for (int i = matcher.end() - 1; i >= matcher.start() + 1; i--) { - if (regexed.charAt(i) == '^') { - regexed.deleteCharAt(i); - count++; + StringBuilder regexed = new StringBuilder(markdown + .replaceAll("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com$0)") + .replaceAll("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com/$0)") + .replaceAll("\\^{2,}", "^")); + + int startIndex = regexed.indexOf("^"); + while (startIndex >= 0 && startIndex + 1 < regexed.length()) { + char currentChar = regexed.charAt(startIndex + 1); + if (currentChar == '^') { + regexed.insert(startIndex, '^'); + startIndex = regexed.indexOf("^", startIndex + 1); + } else if (currentChar == ' ' || currentChar == '\n') { + regexed.insert(startIndex + 1, '^'); + startIndex = regexed.indexOf("^", startIndex + 2); + } else { + if (startIndex + 1 == regexed.length() - 1) { + regexed.append('^'); + startIndex++; } + startIndex++; } - startIndex = matcher.end() - count; - }*/ + } return regexed.toString(); } |