Commit 57e89ce8 authored by Yusuke Iwaki's avatar Yusuke Iwaki

make Pattern objects to static

parent f4a8ccd2
...@@ -40,12 +40,13 @@ public class InlineHightlighter { ...@@ -40,12 +40,13 @@ public class InlineHightlighter {
return new ForegroundColorSpan(Color.TRANSPARENT); return new ForegroundColorSpan(Color.TRANSPARENT);
} }
private static final Pattern HIGHLIGHT_PATTERN = Pattern.compile(
"(^|&gt;|[ >_*~])\\`([^`\\r\\n]+)\\`([<_*~]|\\B|\\b|$)", Pattern.MULTILINE);
private static CharSequence highlightInner(final Context context, final CharSequence text) { private static CharSequence highlightInner(final Context context, final CharSequence text) {
final SpannableString inputText = new SpannableString(text); final SpannableString inputText = new SpannableString(text);
Pattern highlightPattern = Pattern.compile( Matcher matcher = HIGHLIGHT_PATTERN.matcher(inputText);
"(^|&gt;|[ >_*~])\\`([^`\\r\\n]+)\\`([<_*~]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = highlightPattern.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
setSpan(inputText, context, setSpan(inputText, context,
......
...@@ -38,10 +38,10 @@ public class MarkDown { ...@@ -38,10 +38,10 @@ public class MarkDown {
textView.setText(text); textView.setText(text);
} }
private static void removeImage(SpannableString inputText) { private static final Pattern IMAGE_PATTERN = Pattern.compile(
Pattern imagePattern = Pattern.compile(
"!\\[([^\\]]+)\\]\\(((?:http|https):\\/\\/[^\\)]+)\\)", Pattern.MULTILINE); "!\\[([^\\]]+)\\]\\(((?:http|https):\\/\\/[^\\)]+)\\)", Pattern.MULTILINE);
Matcher matcher = imagePattern.matcher(inputText); private static void removeImage(SpannableString inputText) {
Matcher matcher = IMAGE_PATTERN.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
inputText.setSpan(new AbsoluteSizeSpan(0), inputText.setSpan(new AbsoluteSizeSpan(0),
matcher.start(), matcher.end(), matcher.start(), matcher.end(),
...@@ -49,10 +49,11 @@ public class MarkDown { ...@@ -49,10 +49,11 @@ public class MarkDown {
} }
} }
private static void highlightLink1(SpannableString inputText) {
Pattern linkPattern = Pattern.compile( private static final Pattern LINK_PATTERN = Pattern.compile(
"\\[([^\\]]+)\\]\\(((?:http|https):\\/\\/[^\\)]+)\\)", Pattern.MULTILINE); "\\[([^\\]]+)\\]\\(((?:http|https):\\/\\/[^\\)]+)\\)", Pattern.MULTILINE);
final Matcher matcher = linkPattern.matcher(inputText); private static void highlightLink1(SpannableString inputText) {
final Matcher matcher = LINK_PATTERN.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
ClickableSpan span = createLinkSpan(matcher.group(2)); ClickableSpan span = createLinkSpan(matcher.group(2));
setSpan(span, inputText, setSpan(span, inputText,
...@@ -61,10 +62,10 @@ public class MarkDown { ...@@ -61,10 +62,10 @@ public class MarkDown {
} }
} }
private static void highlightLink2(SpannableString inputText) { private static final Pattern LINK_PATTERN2 = Pattern.compile(
Pattern linkPattern = Pattern.compile(
"((?:<|&lt;))((?:http|https):\\/\\/[^\\|]+)\\|(.+?)((?=>|&gt;)(?:>|&gt;))", Pattern.MULTILINE); "((?:<|&lt;))((?:http|https):\\/\\/[^\\|]+)\\|(.+?)((?=>|&gt;)(?:>|&gt;))", Pattern.MULTILINE);
Matcher matcher = linkPattern.matcher(inputText); private static void highlightLink2(SpannableString inputText) {
Matcher matcher = LINK_PATTERN2.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
ClickableSpan span = createLinkSpan(matcher.group(2)); ClickableSpan span = createLinkSpan(matcher.group(2));
setSpan(span, inputText, setSpan(span, inputText,
...@@ -74,10 +75,10 @@ public class MarkDown { ...@@ -74,10 +75,10 @@ public class MarkDown {
} }
} }
private static void bold(SpannableString inputText) { private static final Pattern BOLD_PATTERN = Pattern.compile(
Pattern boldPattern = Pattern.compile(
"(^|&gt;|[ >_~`])(\\*{1,2})[^\\*\\r\\n]+(\\*{1,2})([<_~`]|\\B|\\b|$)", Pattern.MULTILINE); "(^|&gt;|[ >_~`])(\\*{1,2})[^\\*\\r\\n]+(\\*{1,2})([<_~`]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = boldPattern.matcher(inputText); private static void bold(SpannableString inputText) {
Matcher matcher = BOLD_PATTERN.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
setSpan(new StyleSpan(Typeface.BOLD), inputText, setSpan(new StyleSpan(Typeface.BOLD), inputText,
matcher.start() + matcher.group(1).length(), matcher.start() + matcher.group(1).length(),
...@@ -87,10 +88,10 @@ public class MarkDown { ...@@ -87,10 +88,10 @@ public class MarkDown {
} }
} }
private static void italic(SpannableString inputText) { private static final Pattern ITALIC_PATTERN = Pattern.compile(
Pattern italicPattern = Pattern.compile(
"(^|&gt;|[ >*~`])(\\_)[^\\_\\r\\n]+(\\_)([<*~`]|\\B|\\b|$)", Pattern.MULTILINE); "(^|&gt;|[ >*~`])(\\_)[^\\_\\r\\n]+(\\_)([<*~`]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = italicPattern.matcher(inputText); private static void italic(SpannableString inputText) {
Matcher matcher = ITALIC_PATTERN.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
setSpan(new StyleSpan(Typeface.ITALIC), inputText, setSpan(new StyleSpan(Typeface.ITALIC), inputText,
matcher.start() + matcher.group(1).length(), matcher.start() + matcher.group(1).length(),
...@@ -100,10 +101,10 @@ public class MarkDown { ...@@ -100,10 +101,10 @@ public class MarkDown {
} }
} }
private static void strike(SpannableString inputText) { private static final Pattern STRIKE_PATTERN = Pattern.compile(
Pattern strikePattern = Pattern.compile(
"(^|&gt;|[ >_*`])(\\~{1,2})[^~\\r\\n]+(\\~{1,2})([<_*`]|\\B|\\b|$)", Pattern.MULTILINE); "(^|&gt;|[ >_*`])(\\~{1,2})[^~\\r\\n]+(\\~{1,2})([<_*`]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = strikePattern.matcher(inputText); private static void strike(SpannableString inputText) {
Matcher matcher = STRIKE_PATTERN.matcher(inputText);
while (matcher.find()) { while (matcher.find()) {
setSpan(new StrikethroughSpan(), inputText, setSpan(new StrikethroughSpan(), inputText,
matcher.start() + matcher.group(1).length(), matcher.start() + matcher.group(1).length(),
......
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