Commit 3898b7aa authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix InlineHighlighter to use the same regex as the web app.

parent 2f9014bb
...@@ -13,6 +13,8 @@ import android.text.style.StyleSpan; ...@@ -13,6 +13,8 @@ import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
import android.widget.TextView; import android.widget.TextView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import chat.rocket.android.widget.R; import chat.rocket.android.widget.R;
public class InlineHightlighter { public class InlineHightlighter {
...@@ -39,31 +41,45 @@ public class InlineHightlighter { ...@@ -39,31 +41,45 @@ public class InlineHightlighter {
} }
private static CharSequence highlightInner(final Context context, final CharSequence text) { private static CharSequence highlightInner(final Context context, final CharSequence text) {
final SpannableString s = new SpannableString(text); final SpannableString inputText = new SpannableString(text);
final int length = text.length(); Pattern highlightPattern = Pattern.compile(
int highlightStart = length; "(^|&gt;|[ >_*~])\\`([^`\\r\\n]+)\\`([<_*~]|\\B|\\b|$)", Pattern.MULTILINE);
for (int i = 0; i < length; i++) { Matcher matcher = highlightPattern.matcher(inputText);
char chr = text.charAt(i);
if (chr == '`') { while (matcher.find()) {
if (i > highlightStart) { setSpan(inputText, context,
final int highlightEnd = i; matcher.start() + matcher.group(1).length(),
if (highlightStart + 1 < highlightEnd) { matcher.end() - matcher.group(3).length(),
s.setSpan(createTransparentSpan(), highlightStart, highlightStart + 1, 1, 1);
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); }
for (CharacterStyle span : createCharStyles(context)) {
s.setSpan(span, highlightStart + 1, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return inputText;
} }
s.setSpan(createTransparentSpan(), highlightEnd, highlightEnd + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); private static void setSpan(SpannableString inputText, Context context,
} int start, int end, int markStartLen, int markEndLen) {
highlightStart = length; if (markStartLen > 0) {
} else { inputText.setSpan(createTransparentSpan(),
highlightStart = i; start, start + markStartLen,
} Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} }
return s; CharacterStyle[] spans =
inputText.getSpans(start + markStartLen, end - markEndLen, CharacterStyle.class);
for (CharacterStyle span : spans) {
inputText.removeSpan(span);
}
for (CharacterStyle span : createCharStyles(context)) {
inputText.setSpan(span,
start + markStartLen, end - markEndLen,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (markEndLen > 0) {
inputText.setSpan(createTransparentSpan(),
end - markEndLen, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment