Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on suggestion by Dean and the mentioned example, here's the "solution" to the problem. Keep in mind that it's a very expensive one due mainly to the parsing of HTML string (~160ms on quad-core/16GB RAM MBPr). This solution also handles both valid and invalid HTML. Keep in mind there is a little hack around the limitation of JSOUP to make sure extra tags are not included to make the end result a valid HTML. I really hope someone can come up with a better solution, but here it is for now.</p> <pre><code>public static String makeHTML(String htmlText){ boolean isValidDoc = false; if((htmlText.contains("&lt;html") || htmlText.contains("&lt;HTML")) &amp;&amp; (htmlText.contains("&lt;head") || htmlText.contains("&lt;HEAD")) &amp;&amp; (htmlText.contains("&lt;body") || htmlText.contains("&lt;BODY"))){ isValidDoc = true; } Document doc = Jsoup.parseBodyFragment(htmlText); final String urlRegex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&amp;@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&amp;@#/%=~_|]"; final List&lt;TextNode&gt; nodesToChange = new ArrayList&lt;&gt;(); final List&lt;String&gt; changedContent = new ArrayList&lt;&gt;(); NodeTraversor nd = new NodeTraversor(new NodeVisitor() { @Override public void tail(Node node, int depth) { if (node instanceof TextNode) { TextNode textNode = (TextNode) node; Node parent = node.parent(); if(parent.nodeName().equals("a")){ return; } String text = textNode.getWholeText(); List&lt;String&gt; allMatches = new ArrayList&lt;String&gt;(); Matcher m = Pattern.compile(urlRegex) .matcher(text); while (m.find()) { allMatches.add(m.group()); } if(allMatches.size() &gt; 0){ String result = text; for(String match : allMatches){ result = result.replace(match, "&lt;a href=\"" + match + "\"&gt;" + match + "&lt;/a&gt;"); } changedContent.add(result); nodesToChange.add(textNode); } } } @Override public void head(Node node, int depth) { } }); nd.traverse(doc.body()); int count = 0; for (TextNode textNode : nodesToChange) { String result = changedContent.get(count++); Node newNode = new DataNode(result, textNode.baseUri()); textNode.replaceWith(newNode); } String processed = doc.toString(); if(!isValidDoc){ int start = processed.indexOf("&lt;body&gt;") + 6; int end = processed.lastIndexOf("&lt;/body&gt;"); processed = processed.substring(start, end); } return processed; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload