From c0b71555723774624915162582095f9873f85b91 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Thu, 26 Dec 2013 21:53:48 +0200 Subject: [PATCH] implement link titles --- Parsedown.php | 29 ++++++++++++++++++++++++----- tests/data/inline_title.html | 1 + tests/data/inline_title.md | 1 + tests/data/reference_title.html | 1 + tests/data/reference_title.md | 1 + 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 tests/data/inline_title.html create mode 100644 tests/data/inline_title.md create mode 100644 tests/data/reference_title.html create mode 100644 tests/data/reference_title.md diff --git a/Parsedown.php b/Parsedown.php index ca9eb3d..2466077 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -401,11 +401,18 @@ class Parsedown # reference - if (preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches)) + if (preg_match('/^\[(.+?)\]:[ ]*(.+?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*$/', $deindented_line, $matches)) { $label = strtolower($matches[1]); - $this->reference_map[$label] = trim($matches[2], '<>');; + $this->reference_map[$label] = array( + '»' => trim($matches[2], '<>'), + ); + + if (isset($matches[3])) + { + $this->reference_map[$label]['#'] = $matches[3]; + } continue 2; } @@ -734,10 +741,15 @@ class Parsedown $remaining_text = substr($text, $offset); - if ($remaining_text[0] === '(' and preg_match('/^\((.*?)\)/', $remaining_text, $matches)) + if ($remaining_text[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remaining_text, $matches)) { $element['»'] = $matches[1]; + if (isset($matches[2])) + { + $element['#'] = $matches[2]; + } + $offset += strlen($matches[0]); } elseif ($this->reference_map) @@ -755,7 +767,12 @@ class Parsedown if (isset($this->reference_map[$reference])) { - $element['»'] = $this->reference_map[$reference]; + $element['»'] = $this->reference_map[$reference]['»']; + + if (isset($this->reference_map[$reference]['#'])) + { + $element['#'] = $this->reference_map[$reference]['#']; + } } else { @@ -781,7 +798,9 @@ class Parsedown { $element['a'] = $this->parse_span_elements($element['a'], $markers); - $markup .= ''.$element['a'].''; + $markup .= isset($element['#']) + ? ''.$element['a'].'' + : ''.$element['a'].''; } unset($element); diff --git a/tests/data/inline_title.html b/tests/data/inline_title.html new file mode 100644 index 0000000..bbab93b --- /dev/null +++ b/tests/data/inline_title.html @@ -0,0 +1 @@ +

single quotes and double quotes

\ No newline at end of file diff --git a/tests/data/inline_title.md b/tests/data/inline_title.md new file mode 100644 index 0000000..cb09344 --- /dev/null +++ b/tests/data/inline_title.md @@ -0,0 +1 @@ +[single quotes](http://example.com 'Example') and [double quotes](http://example.com "Example") \ No newline at end of file diff --git a/tests/data/reference_title.html b/tests/data/reference_title.html new file mode 100644 index 0000000..70e589a --- /dev/null +++ b/tests/data/reference_title.html @@ -0,0 +1 @@ +

single quotes and double quotes

\ No newline at end of file diff --git a/tests/data/reference_title.md b/tests/data/reference_title.md new file mode 100644 index 0000000..162b832 --- /dev/null +++ b/tests/data/reference_title.md @@ -0,0 +1 @@ +[single quotes](http://example.com 'Title') and [double quotes](http://example.com "Title") \ No newline at end of file