Commit 2f9014bb authored by Yusuke Iwaki's avatar Yusuke Iwaki

render MarkDown link.

parent b617a4b8
package chat.rocket.android.widget.helper;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.nibor.autolink.LinkExtractor;
import org.nibor.autolink.LinkSpan;
import org.nibor.autolink.LinkType;
......@@ -41,33 +33,10 @@ public class Linkify {
final int idx2 = link.getEndIndex();
final String url = text.subSequence(idx1, idx2).toString();
spannableString.setSpan(createLinkSpan(url), idx1, idx2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(MarkDown.createLinkSpan(url), idx1, idx2,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
private static ClickableSpan createLinkSpan(final String url) {
return new ClickableSpan() {
@Override
public void onClick(View view) {
final Context context = view.getContext();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return;
} catch (Exception exception) {
}
try {
ClipboardManager clipboardManager =
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText("linkURL", url));
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
}
}
};
}
}
\ No newline at end of file
package chat.rocket.android.widget.helper;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -22,6 +30,8 @@ public class MarkDown {
public static void apply(TextView textView) {
SpannableString text = new SpannableString(textView.getText());
removeImage(text);
highlightLink1(text);
highlightLink2(text);
bold(text);
italic(text);
strike(text);
......@@ -39,6 +49,31 @@ public class MarkDown {
}
}
private static void highlightLink1(SpannableString inputText) {
Pattern linkPattern = Pattern.compile(
"\\[([^\\]]+)\\]\\(((?:http|https):\\/\\/[^\\)]+)\\)", Pattern.MULTILINE);
final Matcher matcher = linkPattern.matcher(inputText);
while (matcher.find()) {
ClickableSpan span = createLinkSpan(matcher.group(2));
setSpan(span, inputText,
matcher.start(), matcher.end(),
1, matcher.group(2).length() + 3);
}
}
private static void highlightLink2(SpannableString inputText) {
Pattern linkPattern = Pattern.compile(
"(?:<|&lt;)((?:http|https):\\/\\/[^\\|]+)\\|(.+?)(?=>|&gt;)(?:>|&gt;)", Pattern.MULTILINE);
Matcher matcher = linkPattern.matcher(inputText);
while (matcher.find()) {
ClickableSpan span = createLinkSpan(matcher.group(2));
setSpan(span, inputText,
matcher.start(), matcher.end(),
matcher.group(1).length() + matcher.group(2).length() + 1,
matcher.group(5).length() + matcher.group(6).length());
}
}
private static void bold(SpannableString inputText) {
Pattern boldPattern = Pattern.compile(
"(^|&gt;|[ >_~`])(\\*{1,2})[^\\*\\r\\n]+(\\*{1,2})([<_~`]|\\B|\\b|$)", Pattern.MULTILINE);
......@@ -96,4 +131,28 @@ public class MarkDown {
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
/*package*/ static ClickableSpan createLinkSpan(final String url) {
return new ClickableSpan() {
@Override
public void onClick(View view) {
final Context context = view.getContext();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return;
} catch (Exception exception) {
}
try {
ClipboardManager clipboardManager =
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText("linkURL", url));
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
}
}
};
}
}
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