From bedcf6d0b626a447d4fec40e3e33b4fe0339ea38 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 6 Apr 2021 23:43:32 +0800 Subject: [PATCH] Support inline gif in comments. --- .../comment/ParseComment.java | 2 +- .../infinityforreddit/utils/Utils.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 0b38c530..d5a41dd1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -140,7 +140,7 @@ public class ParseComment { String distinguished = singleCommentData.getString(JSONUtils.DISTINGUISHED_KEY); String commentMarkdown = ""; if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) { - commentMarkdown = Utils.modifyMarkdown(singleCommentData.getString(JSONUtils.BODY_KEY).trim()); + commentMarkdown = Utils.parseInlineGifInComments(Utils.modifyMarkdown(singleCommentData.getString(JSONUtils.BODY_KEY).trim())); } String commentRawText = Utils.trimTrailingWhitespace( Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString(); 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 f7a7d247..4cea425b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -22,6 +22,8 @@ import androidx.core.text.HtmlCompat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SortType; @@ -93,6 +95,23 @@ public class Utils { return regexed.toString(); } + public static String parseInlineGifInComments(String markdown) { + StringBuilder markdownStringBuilder = new StringBuilder(markdown); + Pattern inlineGifPattern = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)"); + 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)"); + } + + Pattern inlineGifPattern2 = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)"); + 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)"); + } + + return markdownStringBuilder.toString(); + } + public static CharSequence trimTrailingWhitespace(CharSequence source) { if(source == null)