1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

implement URL auto-linking

This commit is contained in:
Emanuil Rusev 2013-11-22 00:20:45 +02:00
parent 5a563008aa
commit ddc5b7e2dd
3 changed files with 22 additions and 14 deletions

View File

@ -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+;)/', '&amp;', $url);
foreach ($matches as $matches)
{
$url = $matches[1];
$element = '<a href=":href">:text</a>';
$element = str_replace(':text', $url, $element);
$element = str_replace(':href', $url, $element);
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
# ~
$element = '<a href=":href">:text</a>';
$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;
}
}

View File

@ -0,0 +1 @@
<p>Here's an autolink <a href="http://example.com">http://example.com</a>.</p>

View File

@ -0,0 +1 @@
Here's an autolink http://example.com.