Parse comments to replace empty image captions

Closes #33
This commit is contained in:
Balazs Toldi 2023-07-30 11:02:11 +02:00
parent 8c758460bc
commit debb3e04df
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
3 changed files with 49 additions and 8 deletions

View File

@ -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"));

View File

@ -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();
}
}

View File

@ -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());