Fix regex for spoiler. Support spoiler in CommentsListingRecyclerViewAdapter.

This commit is contained in:
Alex Ning 2020-08-14 14:14:57 +08:00
parent 362932460f
commit 400b8b5274
2 changed files with 76 additions and 22 deletions

View File

@ -13,7 +13,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
@ -251,7 +250,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
@Override
public String processMarkdown(@NonNull String markdown) {
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
Pattern spoilerPattern = Pattern.compile(">!.+!<");
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
while (matcher.find()) {
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, "&gt;");
@ -262,18 +261,15 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
@Override
public void afterSetText(@NonNull TextView textView) {
textView.setHighlightColor(Color.TRANSPARENT);
StringBuilder markdownStringBuilder = new StringBuilder(textView.getText().toString());
Pattern spoilerPattern = Pattern.compile(">!.+!<");
SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString());
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
Spannable spannable = new SpannableString(markdownStringBuilder);
int start = 0;
boolean find = false;
while (matcher.find(start)) {
find = true;
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
Spannable spannableCopy = new SpannableString(markdownStringBuilder);
ClickableSpan clickableSpan = new ClickableSpan() {
private boolean isShowing = false;
@Override
@ -294,13 +290,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
view.invalidate();
}
};
spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable = spannableCopy;
markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start = matcher.end() - 4;
}
if (find) {
textView.setText(spannable);
textView.setText(markdownStringBuilder);
}
}
@ -353,7 +347,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
@Override
public String processMarkdown(@NonNull String markdown) {
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
Pattern spoilerPattern = Pattern.compile(">!.+!<");
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
while (matcher.find()) {
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, "&gt;");
@ -364,27 +358,24 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
@Override
public void afterSetText(@NonNull TextView textView) {
SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString());
Pattern spoilerPattern = Pattern.compile(">!.+!<");
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
Spannable spannable = new SpannableString(markdownStringBuilder);
int start = 0;
boolean find = false;
while (matcher.find(start)) {
find = true;
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
Spannable spannableCopy = new SpannableString(markdownStringBuilder);
ClickableSpan clickableSpan = new ClickableSpan() {
private boolean isShowing = false;
@Override
public void updateDrawState(@NonNull TextPaint ds) {
if (isShowing) {
super.updateDrawState(ds);
ds.setColor(mCommentTextColor);
ds.setColor(markdownColor);
} else {
ds.bgColor = Color.BLACK;
ds.setColor(mCommentTextColor);
ds.setColor(markdownColor);
}
ds.setUnderlineText(false);
}
@ -395,13 +386,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
view.invalidate();
}
};
spannableCopy.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable = spannableCopy;
markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start = matcher.end() - 4;
}
if (find) {
textView.setText(spannable);
textView.setText(markdownStringBuilder);
}
}

View File

@ -4,9 +4,15 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
@ -27,6 +33,8 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -102,8 +110,65 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
super(DIFF_CALLBACK);
mContext = context;
mOauthRetrofit = oauthRetrofit;
mCommentColor = customThemeWrapper.getCommentColor();
mMarkwon = Markwon.builder(mContext)
.usePlugin(new AbstractMarkwonPlugin() {
@NonNull
@Override
public String processMarkdown(@NonNull String markdown) {
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
while (matcher.find()) {
markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, "&gt;");
}
return super.processMarkdown(markdownStringBuilder.toString());
}
@Override
public void afterSetText(@NonNull TextView textView) {
textView.setHighlightColor(Color.TRANSPARENT);
SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString());
Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<");
Matcher matcher = spoilerPattern.matcher(markdownStringBuilder);
int start = 0;
boolean find = false;
while (matcher.find(start)) {
find = true;
markdownStringBuilder.delete(matcher.end() - 2, matcher.end());
markdownStringBuilder.delete(matcher.start(), matcher.start() + 2);
int matcherStart = matcher.start();
int matcherEnd = matcher.end();
ClickableSpan clickableSpan = new ClickableSpan() {
private boolean isShowing = false;
@Override
public void updateDrawState(@NonNull TextPaint ds) {
if (isShowing) {
super.updateDrawState(ds);
ds.setColor(mCommentColor);
} else {
ds.bgColor = Color.BLACK;
ds.setColor(mCommentColor);
}
ds.setUnderlineText(false);
}
@Override
public void onClick(@NonNull View view) {
if (!(isShowing && markdownStringBuilder.getSpans(matcherStart, matcherEnd - 4, URLSpan.class).length > 0)) {
isShowing = !isShowing;
view.invalidate();
}
}
};
markdownStringBuilder.setSpan(clickableSpan, matcherStart, matcherEnd - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start = matcherEnd - 4;
}
if (find) {
textView.setText(markdownStringBuilder);
}
}
@Override
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
builder.linkColor(customThemeWrapper.getLinkColor());