From 85ad014f743dae28ad1c3d2535419ff2225ad960 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Fri, 6 Dec 2013 01:43:55 +0200 Subject: [PATCH] parse code span after recursive types to resolve #44 --- Parsedown.php | 66 +++++++++++++++++----------------- tests/data/inline_link.html | 1 + tests/data/inline_link.md | 2 ++ tests/data/text_reference.html | 3 +- tests/data/text_reference.md | 4 ++- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index b4a3f1e..9ac8226 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -629,38 +629,7 @@ class Parsedown $index = 0; - # code span - - if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER)) - { - foreach ($matches as $matches) - { - $element_text = $matches[1]; - $element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8'); - - # decodes escape sequences - - $this->escape_sequence_map - and strpos($element_text, "\x1A") !== FALSE - and $element_text = strtr($element_text, $this->escape_sequence_map); - - # composes element - - $element = ''.$element_text.''; - - # encodes element - - $code = "\x1A".'$'.$index; - - $text = str_replace($matches[0], $code, $text); - - $map[$code] = $element; - - $index ++; - } - } - - # inline link or image + # inline link or image (recursive) if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) # inline { @@ -693,7 +662,7 @@ class Parsedown } } - # reference link or image + # reference link or image (recursive) if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:\n?[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER)) { @@ -735,6 +704,37 @@ class Parsedown } } + # code span + + if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER)) + { + foreach ($matches as $matches) + { + $element_text = $matches[1]; + $element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8'); + + # decodes escape sequences + + $this->escape_sequence_map + and strpos($element_text, "\x1A") !== FALSE + and $element_text = strtr($element_text, $this->escape_sequence_map); + + # composes element + + $element = ''.$element_text.''; + + # encodes element + + $code = "\x1A".'$'.$index; + + $text = str_replace($matches[0], $code, $text); + + $map[$code] = $element; + + $index ++; + } + } + # automatic link if (strpos($text, '://') !== FALSE) diff --git a/tests/data/inline_link.html b/tests/data/inline_link.html index 7dc6b21..1ff9f1b 100644 --- a/tests/data/inline_link.html +++ b/tests/data/inline_link.html @@ -1,2 +1,3 @@

link

+

link

MD Logo

\ No newline at end of file diff --git a/tests/data/inline_link.md b/tests/data/inline_link.md index a4931a0..554de0e 100644 --- a/tests/data/inline_link.md +++ b/tests/data/inline_link.md @@ -1,3 +1,5 @@ [link](http://example.com) +[`link`](http://example.com) + [![MD Logo](http://parsedown.org/md.png)](http://example.com) \ No newline at end of file diff --git a/tests/data/text_reference.html b/tests/data/text_reference.html index 7eeecd1..26587f5 100644 --- a/tests/data/text_reference.html +++ b/tests/data/text_reference.html @@ -3,4 +3,5 @@

[one][404] with no definition

multiline one defined on 2 lines

-

one with an upper case label

\ No newline at end of file +

one with an upper case label

+

link

\ No newline at end of file diff --git a/tests/data/text_reference.md b/tests/data/text_reference.md index c1ddeb1..7768e30 100644 --- a/tests/data/text_reference.md +++ b/tests/data/text_reference.md @@ -13,4 +13,6 @@ one][website] defined on 2 lines [one][label] with an upper case label -[LABEL]: http://example.com \ No newline at end of file +[LABEL]: http://example.com + +[`link`][website] \ No newline at end of file