From b66ec74f98915827628caf52ed74d7159e6d9686 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 5 Aug 2018 08:53:43 +0000 Subject: [PATCH] Linkify Especially for XDA notification ... --- .../java/eu/faircode/email/HtmlHelper.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 0723baefc7..f84b19eeae 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -33,12 +33,15 @@ import org.jsoup.select.NodeVisitor; import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class HtmlHelper implements NodeVisitor { private Context context; private String newline; private List refs = new ArrayList<>(); private StringBuilder sb = new StringBuilder(); + private Pattern pattern = Pattern.compile("([http|https]+://[\\w\\S(\\.|:|/)]+)"); private HtmlHelper(Context context, boolean reply) { this.context = context; @@ -47,9 +50,18 @@ public class HtmlHelper implements NodeVisitor { public void head(Node node, int depth) { String name = node.nodeName(); - if (node instanceof TextNode) - sb.append(((TextNode) node).text()); - else if (name.equals("li")) + if (node instanceof TextNode) { + String text = ((TextNode) node).text(); + Matcher matcher = pattern.matcher(text); + while (matcher.find()) { + String ref = matcher.group(); + if (!refs.contains(ref)) + refs.add(ref); + String alt = context.getString(R.string.title_link); + text = text.replace(ref, String.format("%s [%d]", ref, alt, refs.size())); + } + sb.append(text); + } else if (name.equals("li")) sb.append(newline).append(" * "); else if (name.equals("dt")) sb.append(" ");