aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java20
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java64
2 files changed, 61 insertions, 23 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java
index ca71d4bd..4cdd3fd6 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java
@@ -35,13 +35,13 @@ public class ImageAndGifBlockParser extends AbstractBlockParser {
}
public static class Factory extends AbstractBlockParserFactory {
- private final Pattern redditPreviewPattern = Pattern.compile("!\\[img]\\(https://preview.redd.it/\\w+.(jpg|png|jpeg)((\\?+[-a-zA-Z0-9()@:%_+.~#?&/=]*)|)\\)");
- private final Pattern iRedditPattern = Pattern.compile("!\\[img]\\(https://i.redd.it/\\w+.(jpg|png|jpeg|gif)\\)");
+ private final Pattern redditPreviewPattern = Pattern.compile("!\\[.*]\\(https://preview.redd.it/\\w+.(jpg|png|jpeg)((\\?+[-a-zA-Z0-9()@:%_+.~#?&/=]*)|)\\)");
+ private final Pattern iRedditPattern = Pattern.compile("!\\[.*]\\(https://i.redd.it/\\w+.(jpg|png|jpeg|gif)\\)");
private final Pattern gifPattern = Pattern.compile("!\\[gif]\\(giphy\\|\\w+(\\|downsized)?\\)");
@Nullable
private Map<String, MediaMetadata> mediaMetadataMap;
- private final int fromIndex = "![img](https://preview.redd.it/".length();
- private final int fromIndexIReddit = "![img](https://i.redd.it/".length();
+ private final int previewReddItLength = "https://preview.redd.it/".length();
+ private final int iReddItLength = "https://i.redd.it/".length();
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
@@ -53,9 +53,11 @@ public class ImageAndGifBlockParser extends AbstractBlockParser {
Matcher matcher = redditPreviewPattern.matcher(line);
if (matcher.find()) {
if (matcher.end() == line.length()) {
- int endIndex = line.indexOf('.', fromIndex);
+ int endIndex = line.indexOf('.', previewReddItLength);
if (endIndex > 0) {
- String id = line.substring(fromIndex, endIndex);
+ int urlStartIndex = line.lastIndexOf("https://preview.redd.it/", matcher.end());
+ String id = line.substring(previewReddItLength + urlStartIndex,
+ line.indexOf(".", previewReddItLength + urlStartIndex));
return mediaMetadataMap.containsKey(id) ? BlockStart.of(new ImageAndGifBlockParser(mediaMetadataMap.get(id))) : BlockStart.none();
}
}
@@ -64,9 +66,11 @@ public class ImageAndGifBlockParser extends AbstractBlockParser {
matcher = iRedditPattern.matcher(line);
if (matcher.find()) {
if (matcher.end() == line.length()) {
- int endIndex = line.indexOf('.', fromIndexIReddit);
+ int endIndex = line.indexOf('.', iReddItLength);
if (endIndex > 0) {
- String id = line.substring(fromIndexIReddit, endIndex);
+ int urlStartIndex = line.lastIndexOf("https://i.redd.it/", matcher.end());
+ String id = line.substring(iReddItLength + urlStartIndex,
+ line.indexOf(".", iReddItLength + urlStartIndex));
return mediaMetadataMap.containsKey(id) ? BlockStart.of(new ImageAndGifBlockParser(mediaMetadataMap.get(id))) : BlockStart.none();
}
}
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 c47c879a..9f3167b9 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
@@ -73,11 +73,10 @@ public final class Utils {
Pattern.compile("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}"),
Pattern.compile("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}"),
Pattern.compile("\\^{2,}"),
- Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)"),
- Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)"),
- Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)"),
- Pattern.compile("https://preview.redd.it/\\w+.(jpg|png|jpeg)((\\?+[-a-zA-Z0-9()@:%_+.~#?&/=]*)|)"),
- Pattern.compile("https://i.redd.it/\\w+.(jpg|png|jpeg|gif)")
+ //Sometimes the reddit preview images and gifs have a caption and the markdown will become [caption](image_link)
+ Pattern.compile("(\\[(?:(?!((?<!\\\\)\\[)).)*?]\\()?https://preview.redd.it/\\w+.(jpg|png|jpeg)((\\?+[-a-zA-Z0-9()@:%_+.~#?&/=]*)|)"),
+ //Same reason as above. But this time it only matches [caption](image-link. Notice there is no ) at the end.
+ Pattern.compile("(\\[(?:(?!((?<!\\\\)\\[)).)*?]\\()?https://i.redd.it/\\w+.(jpg|png|jpeg|gif)")
};
public static String modifyMarkdown(String markdown) {
@@ -94,35 +93,70 @@ public final class Utils {
}
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
- Pattern previewReddItImagePattern = REGEX_PATTERNS[6];
+ Pattern previewReddItImagePattern = REGEX_PATTERNS[3];
Matcher matcher = previewReddItImagePattern.matcher(markdownStringBuilder);
int start = 0;
int previewReddItLength = "https://preview.redd.it/".length();
while (matcher.find(start)) {
- String id = markdownStringBuilder.substring(matcher.start() + previewReddItLength, markdownStringBuilder.indexOf(".", matcher.start() + previewReddItLength));
+ String id;
+ if (markdownStringBuilder.charAt(matcher.start()) == '[') {
+ //Has caption
+ int urlStartIndex = markdownStringBuilder.lastIndexOf("https://preview.redd.it/", matcher.end());
+ id = markdownStringBuilder.substring(previewReddItLength + urlStartIndex,
+ markdownStringBuilder.indexOf(".", previewReddItLength + urlStartIndex));
+ } else {
+ id = markdownStringBuilder.substring(matcher.start() + previewReddItLength,
+ markdownStringBuilder.indexOf(".", matcher.start() + previewReddItLength));
+ }
+
if (!mediaMetadataMap.containsKey(id)) {
start = matcher.end();
continue;
}
- String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";
- markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText);
- start = replacingText.length() + matcher.start();
+
+ if (markdownStringBuilder.charAt(matcher.start()) == '[') {
+ //Has caption
+ markdownStringBuilder.insert(matcher.start(), '!');
+ start = matcher.end() + 1;
+ } else {
+ String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";
+ markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText);
+ start = replacingText.length() + matcher.start();
+ }
+
matcher = previewReddItImagePattern.matcher(markdownStringBuilder);
}
start = 0;
- Pattern iReddItImagePattern = REGEX_PATTERNS[7];
+ Pattern iReddItImagePattern = REGEX_PATTERNS[4];
matcher = iReddItImagePattern.matcher(markdownStringBuilder);
int iReddItLength = "https://i.redd.it/".length();
while (matcher.find(start)) {
- String id = markdownStringBuilder.substring(matcher.start() + iReddItLength, matcher.start() + markdownStringBuilder.indexOf(".", iReddItLength));
+ String id;
+ if (markdownStringBuilder.charAt(matcher.start()) == '[') {
+ //Has caption
+ int urlStartIndex = markdownStringBuilder.lastIndexOf("https://i.redd.it/", matcher.end());
+ id = markdownStringBuilder.substring(iReddItLength + urlStartIndex,
+ markdownStringBuilder.indexOf(".", iReddItLength + urlStartIndex));
+ } else {
+ id = markdownStringBuilder.substring(matcher.start() + iReddItLength, matcher.start() + markdownStringBuilder.indexOf(".", iReddItLength));
+ }
+
if (!mediaMetadataMap.containsKey(id)) {
start = matcher.end();
continue;
}
- String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";
- markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText);
- start = replacingText.length() + matcher.start();
+
+ if (markdownStringBuilder.charAt(matcher.start()) == '[') {
+ //Has caption
+ markdownStringBuilder.insert(matcher.start(), '!');
+ start = matcher.end() + 1;
+ } else {
+ String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";
+ markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText);
+ start = replacingText.length() + matcher.start();
+ }
+
matcher = iReddItImagePattern.matcher(markdownStringBuilder);
}