match balanced parens in URLs to resolve #126

* match balanced parens using recursive pattern
* match quoted title correctly
This commit is contained in:
Won-Kyu Park 2014-02-27 23:47:00 +09:00
parent 886c620440
commit 700b5ddbff

View File

@ -1020,13 +1020,13 @@ class Parsedown
$remainingText = substr($text, $offset); $remainingText = substr($text, $offset);
if ($remainingText[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remainingText, $matches)) if ($remainingText[0] === '(' and preg_match('/^\([ ]*((?:[^()\s]+|(\((?:[^()\s]+|(?2))*\)))+)(?:[ ]+([\'"])(.+?)\\3)?[ ]*\)/', $remainingText, $matches))
{ {
$element['link'] = $matches[1]; $element['link'] = $matches[1];
if (isset($matches[2])) if (isset($matches[4]))
{ {
$element['title'] = $matches[2]; $element['title'] = $matches[4];
} }
$offset += strlen($matches[0]); $offset += strlen($matches[0]);