Skip to content

Commit

Permalink
#224: Better handling of links to subreddits
Browse files Browse the repository at this point in the history
fixed Markdown stuff and exception handling when getting
list of links per comment.
  • Loading branch information
talklittle committed Jan 9, 2012
1 parent fb7b0fb commit 2eb5a11
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
58 changes: 34 additions & 24 deletions src/com/andrewshu/android/reddit/comments/CommentsListActivity.java
Expand Up @@ -45,6 +45,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
Expand Down Expand Up @@ -1902,12 +1903,10 @@ public void onClick(View v) {
*/
private void linkToEmbeddedURLs(Button linkButton) {
final ArrayList<String> urls = new ArrayList<String>();
final ArrayList<String> anchorTexts = new ArrayList<String>();
final ArrayList<MarkdownURL> vtUrls = mVoteTargetThing.getUrls();
int urlsCount = vtUrls.size();
for (int i = 0; i < urlsCount; i++) {
urls.add(vtUrls.get(i).url);
anchorTexts.add(vtUrls.get(i).anchorText);
}
if (urlsCount == 0) {
linkButton.setEnabled(false);
Expand All @@ -1920,29 +1919,40 @@ public void onClick(View v) {
ArrayAdapter<MarkdownURL> adapter =
new ArrayAdapter<MarkdownURL>(CommentsListActivity.this, android.R.layout.select_dialog_item, vtUrls) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
try {
String url = getItem(position).url;
String anchorText = getItem(position).anchorText;
TextView tv = (TextView) v;
Drawable d = getPackageManager().getActivityIcon(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
if (d != null) {
d.setBounds(0, 0, d.getIntrinsicHeight(), d.getIntrinsicHeight());
tv.setCompoundDrawablePadding(10);
tv.setCompoundDrawables(d, null, null, null);
}
final String telPrefix = "tel:";
if (url.startsWith(telPrefix)) {
url = PhoneNumberUtils.formatNumber(url.substring(telPrefix.length()));
}
if (anchorText != null)
tv.setText(Html.fromHtml(anchorText + "<br /><small>" + url + "</small>"));
else
tv.setText(Html.fromHtml(url));
} catch (android.content.pm.PackageManager.NameNotFoundException ex) {
;
TextView tv;
if (convertView == null) {
tv = (TextView) ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(android.R.layout.select_dialog_item, null);
} else {
tv = (TextView) convertView;
}
return v;

String url = getItem(position).url;
String anchorText = getItem(position).anchorText;
if (Constants.LOGGING) Log.d(TAG, "links url="+url + " anchorText="+anchorText);

Drawable d = null;
try {
d = getPackageManager().getActivityIcon(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (NameNotFoundException ignore) {
}
if (d != null) {
d.setBounds(0, 0, d.getIntrinsicHeight(), d.getIntrinsicHeight());
tv.setCompoundDrawablePadding(10);
tv.setCompoundDrawables(d, null, null, null);
}

final String telPrefix = "tel:";
if (url.startsWith(telPrefix)) {
url = PhoneNumberUtils.formatNumber(url.substring(telPrefix.length()));
}

if (anchorText != null)
tv.setText(Html.fromHtml("<span>" + anchorText + "</span><br /><small>" + url + "</small>"));
else
tv.setText(Html.fromHtml(url));

return tv;
}
};

Expand Down
34 changes: 27 additions & 7 deletions src/com/andrewshu/android/reddit/markdown/Markdown.java
Expand Up @@ -75,6 +75,8 @@ public class Markdown {

static final RunAutomaton autoLinkUrlAutomaton = new RunAutomaton(new RegExp("((https?|ftp):([^'\"> \t\r\n])+)", RegExp.NONE).toAutomaton());
// static final Pattern autoLinkEmail = Pattern.compile("<([-.\\w]+\\@[-a-z0-9]+(\\.[-a-z0-9]+)*\\.[a-z]+)>");

static final RunAutomaton subredditAutomaton = new RunAutomaton(new RegExp("/[rR]/[a-zA-Z0-9]+/?", RegExp.NONE).toAutomaton());

/**
* @param txt input
Expand All @@ -100,6 +102,9 @@ public void getURLs(String txt, ArrayList<MarkdownURL> urls) {
// doAnchors originally called from runBlockGamut -> formParagraphs -> runSpanGamut
txt = doAnchorURLs(txt, urls);
txt = doAutoLinkURLs(txt, urls);
txt = doAutoLinkSubredditURLs(txt, urls);

Collections.sort(urls);
}

/**
Expand All @@ -108,6 +113,7 @@ public void getURLs(String txt, ArrayList<MarkdownURL> urls) {
* @param txt - input in markdown format
* @return HTML block corresponding to txt passed in.
*/
@Deprecated
public SpannableStringBuilder markdown(String txt, SpannableStringBuilder ssb, ArrayList<MarkdownURL> urls) {
if (txt == null) {
txt = "";
Expand Down Expand Up @@ -163,7 +169,7 @@ private String doAnchorURLs(String txt, ArrayList<MarkdownURL> urls) {
String title = m.group(6);
int linkTextLength = linkText.length();

if (Constants.LOGGING) Log.d(TAG, "linkText="+linkText + " url="+url + " title="+ title);
if (Constants.LOGGING) Log.d(TAG, "pos="+(start + anchorStart) + " linkText="+linkText + " url="+url + " title="+ title);

// protect emphasis (* and _) within urls
// url = url.replaceAll("\\*", CHAR_PROTECTOR.encode("*"));
Expand All @@ -181,10 +187,6 @@ private String doAnchorURLs(String txt, ArrayList<MarkdownURL> urls) {
// result.append("\"");
// }

// Replace whole anchor thing with just linkText, colored different color
SpannableString ss = new SpannableString(linkText);
ForegroundColorSpan fcs = new ForegroundColorSpan(Constants.MARKDOWN_LINK_COLOR);
ss.setSpan(fcs, 0, linkTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txt = new StringBuilder(txt.substring(0, start + anchorStart))
.append(linkText)
.append(txt.substring(start + anchorEnd, txt.length()))
Expand All @@ -205,7 +207,10 @@ private String doAutoLinkURLs(String txt, ArrayList<MarkdownURL> urls) {
// Colorize URLs
AutomatonMatcher am = autoLinkUrlAutomaton.newMatcher(txt);
while (am.find()) {
urls.add(new MarkdownURL(am.start(), Util.absolutePathToURL(am.group()), null));
String linkText = am.group();
String url = Util.absolutePathToURL(am.group());
if (Constants.LOGGING) Log.d(TAG, "pos="+am.start() + " linkText="+linkText + " url="+url);
urls.add(new MarkdownURL(am.start(), url, am.group()));
}
// Don't autolink emails for now. Neither does reddit.com
// m = autoLinkEmail.matcher(ssb);
Expand All @@ -228,6 +233,21 @@ private String doAutoLinkURLs(String txt, ArrayList<MarkdownURL> urls) {
return txt;
}

/**
* @param txt input text
* @param urls Out URLs from subreddit references
* @return txt, unchanged
*/
private String doAutoLinkSubredditURLs(String txt, ArrayList<MarkdownURL> urls) {
AutomatonMatcher am = subredditAutomaton.newMatcher(txt);
while (am.find()) {
String subreddit = am.group();
if (Constants.LOGGING) Log.d(TAG, "pos="+am.start() + " subreddit="+subreddit);
urls.add(new MarkdownURL(am.start(), Util.absolutePathToURL(subreddit), null));
}
return txt;
}


/** Adapted from MarkdownJ. Convert links, return URL */
private SpannableStringBuilder doAnchors(SpannableStringBuilder ssb, ArrayList<MarkdownURL> urls) {
Expand All @@ -247,7 +267,7 @@ private SpannableStringBuilder doAnchors(SpannableStringBuilder ssb, ArrayList<M
String title = m.group(6);
int linkTextLength = linkText.length();

if (Constants.LOGGING) Log.d(TAG, "linkText="+linkText + " url="+url + " title="+ title);
if (Constants.LOGGING) Log.d(TAG, "pos="+am.start() + " linkText="+linkText + " url="+url + " title="+ title);

// protect emphasis (* and _) within urls
// url = url.replaceAll("\\*", CHAR_PROTECTOR.encode("*"));
Expand Down

0 comments on commit 2eb5a11

Please sign in to comment.