From 4bcd60aafdfdc28c764c8239c82dab0886e40781 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 26 Nov 2023 14:52:18 -0500 Subject: Handle preview images or gifs with captions. --- .../markdown/ImageAndGifBlockParser.java | 20 ++++--- .../infinityforreddit/utils/Utils.java | 64 +++++++++++++++++----- 2 files changed, 61 insertions(+), 23 deletions(-) (limited to 'app') 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 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("(\\[(?:(?!((?