aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorDocile-Alligator <chineseperson5@gmail.com>2021-12-07 13:10:41 +0000
committerGitHub <noreply@github.com>2021-12-07 13:10:41 +0000
commite3ac6d07718e40f3828bd5da3652b0eb4c0a68b9 (patch)
tree5397b22359fc3c0d65197e0dd2a3b0a3d1b3cf57 /app/src/main
parent0d80bee501e5feb9e0e1781a61c9bc771189ede4 (diff)
parent1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219 (diff)
downloadinfinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.tar
infinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.tar.gz
infinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.tar.bz2
infinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.tar.lz
infinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.tar.xz
infinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.tar.zst
infinity-for-reddit-e3ac6d07718e40f3828bd5da3652b0eb4c0a68b9.zip
Merge pull request #570 from scria1000/patch-1
Fix gif not found for downsized Giphy gifs
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java49
1 files changed, 32 insertions, 17 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 e7ee8d6a..41bb5e98 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
@@ -49,7 +49,7 @@ import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.UploadedImage;
import retrofit2.Retrofit;
-public class Utils {
+public final class Utils {
public static final int NETWORK_TYPE_OTHER = -1;
public static final int NETWORK_TYPE_WIFI = 0;
public static final int NETWORK_TYPE_CELLULAR = 1;
@@ -60,21 +60,36 @@ public class Utils {
private static final long MONTH_MILLIS = 30 * DAY_MILLIS;
private static final long YEAR_MILLIS = 12 * MONTH_MILLIS;
+ public static final Pattern[] REGEX_PATTERN = {
+ Pattern.compile("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}"),
+ Pattern.compile("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}"),
+ Pattern.compile("\\^{2,}"),
+ Pattern.compile("(^|^ *|\\n *)#(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)##(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)###(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)####(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)#####(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)######(?!($|\\s|#))"),
+ Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)"),
+ Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)"),
+ Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)"),
+ Pattern.compile("!\\[img]\\(emote\\|\\w+\\|\\w+\\)")
+ };
+
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)")
- .replaceAll("\\^{2,}", "^")
- .replaceAll("(^|^ *|\\n *)#(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)##(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)###(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)####(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)#####(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)######(?!($|\\s|#))", "$0 "));
+ String regexed = REGEX_PATTERN[0].matcher(markdown).replaceAll("[$0](https://www.reddit.com$0)");
+ regexed = REGEX_PATTERN[1].matcher(regexed).replaceAll("[$0](https://www.reddit.com/$0)");
+ regexed = REGEX_PATTERN[2].matcher(regexed).replaceAll("^");
+ regexed = REGEX_PATTERN[3].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[4].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[5].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[6].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[7].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[8].matcher(regexed).replaceAll("$0");
//return fixSuperScript(regexed);
// We don't want to fix super scripts here because we need the original markdown later for editing posts
- return regexed.toString();
+ return regexed;
}
public static String fixSuperScript(String regexedMarkdown) {
@@ -121,21 +136,21 @@ public class Utils {
public static String parseInlineGifInComments(String markdown) {
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
- Pattern inlineGifPattern = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)");
+ Pattern inlineGifPattern = REGEX_PATTERN[9];
Matcher matcher = inlineGifPattern.matcher(markdownStringBuilder);
while (matcher.find()) {
- markdownStringBuilder.replace(matcher.start(), matcher.end(), "[gif](https://media3.giphy.com/media/" + markdownStringBuilder.substring(matcher.start() + "![gif](giphy|".length(), matcher.end() - 1) + "/giphy.mp4)");
+ markdownStringBuilder.replace(matcher.start(), matcher.end(), "[gif](https://i.giphy.com/media/" + markdownStringBuilder.substring(matcher.start() + "![gif](giphy|".length(), matcher.end() - 1) + "/giphy.mp4)");
matcher = inlineGifPattern.matcher(markdownStringBuilder);
}
- Pattern inlineGifPattern2 = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)");
+ Pattern inlineGifPattern2 = REGEX_PATTERN[10];
Matcher matcher2 = inlineGifPattern2.matcher(markdownStringBuilder);
while (matcher2.find()) {
- markdownStringBuilder.replace(matcher2.start(), matcher2.end(), "[gif](https://media3.giphy.com/media/" + markdownStringBuilder.substring(matcher2.start() + "![gif](giphy|".length(), matcher2.end() - "|downsized\\)".length()) + "/giphy.mp4)");
+ markdownStringBuilder.replace(matcher2.start(), matcher2.end(), "[gif](https://i.giphy.com/media/" + markdownStringBuilder.substring(matcher2.start() + "![gif](giphy|".length(), matcher2.end() - "|downsized\\)".length() + 1) + "/giphy.mp4)");
matcher2 = inlineGifPattern2.matcher(markdownStringBuilder);
}
- Pattern inlineGifPattern3 = Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)");
+ Pattern inlineGifPattern3 = REGEX_PATTERN[11];
Matcher matcher3 = inlineGifPattern3.matcher(markdownStringBuilder);
while (matcher3.find()) {
markdownStringBuilder.replace(matcher3.start(), matcher3.end(),