Skip to content

Commit

Permalink
implement URL auto-linking
Browse files Browse the repository at this point in the history
  • Loading branch information
erusev committed Nov 21, 2013
1 parent 5a56300 commit ddc5b7e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
34 changes: 20 additions & 14 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,29 +733,35 @@ private function parse_span_elements($text)
}
}

# 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;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/data/url_autolinking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Here's an autolink <a href="http://example.com">http://example.com</a>.</p>
1 change: 1 addition & 0 deletions tests/data/url_autolinking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here's an autolink http://example.com.

0 comments on commit ddc5b7e

Please sign in to comment.