aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit
diff options
context:
space:
mode:
authorDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2023-11-26 05:12:15 +0000
committerDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2023-11-26 05:12:15 +0000
commite612170931cbe9ea66458c4176465b2358e3c378 (patch)
treeea14bacb61b24067c6d196c719ab673cff1efa30 /app/src/main/java/ml/docilealligator/infinityforreddit
parentc8e527d326e4e64b373d363e8f6032de6f73ae4c (diff)
downloadinfinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.tar
infinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.tar.gz
infinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.tar.bz2
infinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.tar.lz
infinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.tar.xz
infinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.tar.zst
infinity-for-reddit-e612170931cbe9ea66458c4176465b2358e3c378.zip
Directly show i.redd.it images and gifs.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java5
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java13
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java37
4 files changed, 51 insertions, 12 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java
index e37bf304..754ae6e7 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java
@@ -269,11 +269,12 @@ public class ParseComment {
String parentId = singleCommentData.getString(JSONUtils.PARENT_ID_KEY);
boolean isSubmitter = singleCommentData.getBoolean(JSONUtils.IS_SUBMITTER_KEY);
String distinguished = singleCommentData.getString(JSONUtils.DISTINGUISHED_KEY);
+ Map<String, MediaMetadata> mediaMetadataMap = parseMediaMetadata(singleCommentData);
String commentMarkdown = "";
if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) {
commentMarkdown = Utils.parseInlineRedditImages(
Utils.modifyMarkdown(
- Utils.trimTrailingWhitespace(singleCommentData.getString(JSONUtils.BODY_KEY))));
+ Utils.trimTrailingWhitespace(singleCommentData.getString(JSONUtils.BODY_KEY))), mediaMetadataMap);
}
String commentRawText = Utils.trimTrailingWhitespace(
Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString();
@@ -300,8 +301,6 @@ public class ParseComment {
// this key can either be a bool (false) or a long (edited timestamp)
long edited = singleCommentData.optLong(JSONUtils.EDITED_KEY) * 1000;
- Map<String, MediaMetadata> mediaMetadataMap = parseMediaMetadata(singleCommentData);
-
return new Comment(id, fullName, author, authorFlair, authorFlairHTMLBuilder.toString(),
linkAuthor, submitTime, commentMarkdown, commentRawText,
linkId, subredditName, parentId, score, voteType, isSubmitter, distinguished,
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 49d58b60..ca71d4bd 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/ImageAndGifBlockParser.java
@@ -36,10 +36,12 @@ 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 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();
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
@@ -59,6 +61,17 @@ public class ImageAndGifBlockParser extends AbstractBlockParser {
}
}
+ matcher = iRedditPattern.matcher(line);
+ if (matcher.find()) {
+ if (matcher.end() == line.length()) {
+ int endIndex = line.indexOf('.', fromIndexIReddit);
+ if (endIndex > 0) {
+ String id = line.substring(fromIndexIReddit, endIndex);
+ return mediaMetadataMap.containsKey(id) ? BlockStart.of(new ImageAndGifBlockParser(mediaMetadataMap.get(id))) : BlockStart.none();
+ }
+ }
+ }
+
matcher = gifPattern.matcher(line);
if (matcher.find()) {
if (matcher.end() == line.length()) {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java
index 31a1f139..88d60dfa 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java
@@ -350,7 +350,7 @@ public class ParsePost {
if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText("");
} else {
- post.setSelfText(Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY)))));
+ post.setSelfText(Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY))), mediaMetadataMap));
}
String authority = uri.getAuthority();
@@ -536,7 +536,7 @@ public class ParsePost {
if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText("");
} else {
- post.setSelfText(Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY)))));
+ post.setSelfText(Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY))), mediaMetadataMap));
}
post.setPreviews(previews);
@@ -611,7 +611,7 @@ public class ParsePost {
if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText("");
} else {
- post.setSelfText(Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY)))));
+ post.setSelfText(Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY))), mediaMetadataMap));
}
String authority = uri.getAuthority();
@@ -755,7 +755,7 @@ public class ParsePost {
if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText("");
} else {
- String selfText = Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY))));
+ String selfText = Utils.parseInlineRedditImages(Utils.modifyMarkdown(Utils.trimTrailingWhitespace(data.getString(JSONUtils.SELFTEXT_KEY))), mediaMetadataMap);
post.setSelfText(selfText);
if (data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
post.setSelfTextPlainTrimmed("");
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 2fb4cfb0..01ec20ac 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
@@ -46,12 +46,14 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
+import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.noties.markwon.core.spans.CustomTypefaceSpan;
+import ml.docilealligator.infinityforreddit.MediaMetadata;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.UploadedImage;
@@ -74,7 +76,8 @@ public final class Utils {
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://preview.redd.it/\\w+.(jpg|png|jpeg)((\\?+[-a-zA-Z0-9()@:%_+.~#?&/=]*)|)"),
+ Pattern.compile("https://i.redd.it/\\w+.(jpg|png|jpeg|gif)")
};
public static String modifyMarkdown(String markdown) {
@@ -85,16 +88,40 @@ public final class Utils {
return regexed;
}
- public static String parseInlineRedditImages(String markdown) {
+ public static String parseInlineRedditImages(String markdown, @Nullable Map<String, MediaMetadata> mediaMetadataMap) {
+ if (mediaMetadataMap == null) {
+ return markdown;
+ }
+
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
- Pattern inlineRedditImagePattern = REGEX_PATTERNS[6];
- Matcher matcher = inlineRedditImagePattern.matcher(markdownStringBuilder);
+ Pattern previewReddItImagePattern = REGEX_PATTERNS[6];
+ Matcher matcher = previewReddItImagePattern.matcher(markdownStringBuilder);
int start = 0;
+ int previewReddItLength = "https://preview.redd.it/".length();
+ while (matcher.find(start)) {
+ String id = markdownStringBuilder.substring(previewReddItLength, markdownStringBuilder.indexOf(".", previewReddItLength));
+ if (!mediaMetadataMap.containsKey(id)) {
+ continue;
+ }
+ 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];
+ matcher = iReddItImagePattern.matcher(markdownStringBuilder);
+ int iReddItLength = "https://i.redd.it/".length();
while (matcher.find(start)) {
+ String id = markdownStringBuilder.substring(iReddItLength, markdownStringBuilder.indexOf(".", iReddItLength));
+ if (!mediaMetadataMap.containsKey(id)) {
+ continue;
+ }
String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";
markdownStringBuilder.replace(matcher.start(), matcher.end(), replacingText);
start = replacingText.length() + matcher.start();
- matcher = inlineRedditImagePattern.matcher(markdownStringBuilder);
+ matcher = iReddItImagePattern.matcher(markdownStringBuilder);
}
return markdownStringBuilder.toString();