From debb3e04dfb7399e820371145fced99641416fc1 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Sun, 30 Jul 2023 11:02:11 +0200 Subject: [PATCH] Parse comments to replace empty image captions Closes #33 --- .../comment/ParseComment.java | 6 ++- .../markdown/MarkdownUtils.java | 42 ++++++++++++++++++- .../infinityforlemmy/post/ParsePost.java | 9 ++-- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java index ad343859..f99a0842 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java @@ -21,6 +21,7 @@ import java.util.TimeZone; import java.util.concurrent.Executor; import java.util.regex.Pattern; +import eu.toldi.infinityforlemmy.markdown.MarkdownUtils; import eu.toldi.infinityforlemmy.utils.JSONUtils; import eu.toldi.infinityforlemmy.utils.LemmyUtils; @@ -296,8 +297,9 @@ public class ParseComment { } catch (ParseException e) { e.printStackTrace(); } - String commentMarkdown = commentObj.getString("content"); - String commentRawText = commentObj.getString("content"); + String content = MarkdownUtils.processImageCaptions(commentObj.getString("content"), "Image"); + String commentMarkdown = content; + String commentRawText = content; String linkId = postObj.getString("id"); String communityName = communityObj.getString("name"); String communityQualifiedName = LemmyUtils.actorID2FullName(communityObj.getString("actor_id")); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/markdown/MarkdownUtils.java b/app/src/main/java/eu/toldi/infinityforlemmy/markdown/MarkdownUtils.java index 75ce3ba6..8e573fe3 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/markdown/MarkdownUtils.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/markdown/MarkdownUtils.java @@ -8,6 +8,11 @@ import androidx.annotation.Nullable; import org.commonmark.ext.gfm.tables.TableBlock; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import eu.toldi.infinityforlemmy.R; +import eu.toldi.infinityforlemmy.customviews.CustomMarkwonAdapter; import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonPlugin; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; @@ -20,8 +25,6 @@ import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; import me.saket.bettermovementmethod.BetterLinkMovementMethod; -import eu.toldi.infinityforlemmy.R; -import eu.toldi.infinityforlemmy.customviews.CustomMarkwonAdapter; public class MarkdownUtils { /** @@ -112,4 +115,39 @@ public class MarkdownUtils { .textLayoutIsRoot(R.layout.view_table_entry_cell))) .build(); } + + private static final Pattern emptyPattern = Pattern.compile("!\\[\\]\\((.*?)\\)"); + private static final Pattern nonEmptyPattern = Pattern.compile("!\\[(.*?)\\]\\((.*?)\\)"); + + public static String processImageCaptions(String markdown, String replacementCaption) { + // Pattern for Markdown images with empty captions + + // Pattern for Markdown images with non-empty captions + + + Matcher emptyMatcher = emptyPattern.matcher(markdown); + StringBuffer sb = new StringBuffer(); + + while (emptyMatcher.find()) { + // Replace the matched pattern with the same URL, but with a caption + emptyMatcher.appendReplacement(sb, "[" + replacementCaption + "](" + emptyMatcher.group(1) + ")"); + } + + // Append the rest of the content + emptyMatcher.appendTail(sb); + + // Now process non-empty captions + Matcher nonEmptyMatcher = nonEmptyPattern.matcher(sb.toString()); + StringBuffer finalSb = new StringBuffer(); + + while (nonEmptyMatcher.find()) { + // Replace the matched pattern with the same URL and caption, but without the "!" + nonEmptyMatcher.appendReplacement(finalSb, "[" + nonEmptyMatcher.group(1) + "](" + nonEmptyMatcher.group(2) + ")"); + } + + // Append the rest of the content + nonEmptyMatcher.appendTail(finalSb); + + return finalSb.toString(); + } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java index 74ad6cba..7e1c614f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java @@ -31,6 +31,7 @@ import java.util.concurrent.Executor; import java.util.regex.Matcher; import java.util.regex.Pattern; +import eu.toldi.infinityforlemmy.markdown.MarkdownUtils; import eu.toldi.infinityforlemmy.postfilter.PostFilter; import eu.toldi.infinityforlemmy.utils.JSONUtils; import eu.toldi.infinityforlemmy.utils.LemmyUtils; @@ -640,17 +641,17 @@ public class ParsePost { } } } - if(data.getBoolean("read")){ + if (data.getBoolean("read")) { post.markAsRead(); } - if(!data.isNull("my_vote")){ + if (!data.isNull("my_vote")) { post.setVoteType(data.getInt("my_vote")); - post.setScore(post.getScore()-1); + post.setScore(post.getScore() - 1); } if (!data.getJSONObject("post").isNull("body")) { - String body = data.getJSONObject("post").getString("body"); + String body = MarkdownUtils.processImageCaptions(data.getJSONObject("post").getString("body"), "Image"); post.setSelfText(body); post.setSelfTextPlain(body); post.setSelfTextPlainTrimmed(body.trim());