From ddc5b7e2ddeaecc4d4dec181c10ebac00cfa6046 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Fri, 22 Nov 2013 00:20:45 +0200 Subject: [PATCH] implement URL auto-linking --- Parsedown.php | 34 +++++++++++++++++++-------------- tests/data/url_autolinking.html | 1 + tests/data/url_autolinking.md | 1 + 3 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 tests/data/url_autolinking.html create mode 100644 tests/data/url_autolinking.md diff --git a/Parsedown.php b/Parsedown.php index e4c2ad3..a26acc0 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -733,29 +733,35 @@ class Parsedown } } - # automatic link - - if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER)) + if (strpos($text, '://') !== FALSE) { - foreach ($matches as $matches) + switch (TRUE) { - $url = $matches[1]; + case preg_match_all('{<(https?:[/]{2}[^\s]+)>}i', $text, $matches, PREG_SET_ORDER): + case preg_match_all('{\b(https?:[/]{2}[^\s]+)\b}i', $text, $matches, PREG_SET_ORDER): - strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&', $url); + foreach ($matches as $matches) + { + $url = $matches[1]; - $element = ':text'; - $element = str_replace(':text', $url, $element); - $element = str_replace(':href', $url, $element); + strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&', $url); - # ~ + $element = ':text'; + $element = str_replace(':text', $url, $element); + $element = str_replace(':href', $url, $element); - $code = "\x1A".'$'.$index; + # ~ - $text = str_replace($matches[0], $code, $text); + $code = "\x1A".'$'.$index; - $map[$code] = $element; + $text = str_replace($matches[0], $code, $text); - $index ++; + $map[$code] = $element; + + $index ++; + } + + break; } } diff --git a/tests/data/url_autolinking.html b/tests/data/url_autolinking.html new file mode 100644 index 0000000..5f59097 --- /dev/null +++ b/tests/data/url_autolinking.html @@ -0,0 +1 @@ +

Here's an autolink http://example.com.

\ No newline at end of file diff --git a/tests/data/url_autolinking.md b/tests/data/url_autolinking.md new file mode 100644 index 0000000..7094a09 --- /dev/null +++ b/tests/data/url_autolinking.md @@ -0,0 +1 @@ +Here's an autolink http://example.com. \ No newline at end of file