diff --git a/Parsedown.php b/Parsedown.php index 35f48b1..ca9eb3d 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -631,9 +631,14 @@ class Parsedown # ~ - private $em_strong_regex = array( - '*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s', - '_' => '/^_(.*?)__(.+?)__(.*?)_/s', + private $strong_regex = array( + '*' => '/^[*]{2}([^*]+?)[*]{2}(?![*])/s', + '_' => '/^__([^_]+?)__(?!_)/s', + ); + + private $em_regex = array( + '*' => '/^[*]([^*]+?)[*](?![*])/s', + '_' => '/^_([^_]+?)[_](?![_])\b/s', ); private $strong_em_regex = array( @@ -641,14 +646,9 @@ class Parsedown '_' => '/^__(.*?)_(.+?)_(.*?)__/s', ); - private $strong_regex = array( - '*' => '/^[*]{2}(.+?)[*]{2}/s', - '_' => '/^__(.+?)__/s', - ); - - private $em_regex = array( - '*' => '/^[*](.+?)[*]/s', - '_' => '/^_(.+?)_\b/s', + private $em_strong_regex = array( + '*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s', + '_' => '/^_(.*?)__(.+?)__(.*?)_/s', ); private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '_', '`', 'http', '~~')) @@ -806,7 +806,28 @@ class Parsedown case '*': case '_': - if (preg_match($this->em_strong_regex[$closest_marker], $text, $matches)) + if ($text[1] === $closest_marker and preg_match($this->strong_regex[$closest_marker], $text, $matches)) + { + $matches[1] = $this->parse_span_elements($matches[1], $markers); + + $markup .= ''.$matches[1].''; + } + elseif (preg_match($this->em_regex[$closest_marker], $text, $matches)) + { + $matches[1] = $this->parse_span_elements($matches[1], $markers); + + $markup .= ''.$matches[1].''; + } + elseif ($text[1] === $closest_marker and preg_match($this->strong_em_regex[$closest_marker], $text, $matches)) + { + $matches[2] = $this->parse_span_elements($matches[2], $markers); + + $matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers); + $matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers); + + $markup .= ''.$matches[1].''.$matches[2].''.$matches[3].''; + } + elseif (preg_match($this->em_strong_regex[$closest_marker], $text, $matches)) { $matches[2] = $this->parse_span_elements($matches[2], $markers); @@ -815,27 +836,8 @@ class Parsedown $markup .= ''.$matches[1].''.$matches[2].''.$matches[3].''; } - elseif ($text[1] === $closest_marker) - { - if (preg_match($this->strong_em_regex[$closest_marker], $text, $matches)) - { - $markup .= ''.$matches[1].''.$matches[2].''.$matches[3].''; - } - elseif (preg_match($this->strong_regex[$closest_marker], $text, $matches)) - { - $matches[1] = $this->parse_span_elements($matches[1], $markers); - $markup .= ''.$matches[1].''; - } - } - elseif (preg_match($this->em_regex[$closest_marker], $text, $matches)) - { - $element_text = $this->parse_span_elements($matches[1], $markers); - - $markup .= ''.$element_text.''; - } - - if ($matches) + if (isset($matches) and $matches) { $offset = strlen($matches[0]); } diff --git a/tests/data/em_strong.html b/tests/data/em_strong.html index adc08ce..323d60a 100644 --- a/tests/data/em_strong.html +++ b/tests/data/em_strong.html @@ -1,6 +1,8 @@ +

em strong

em strong strong

strong em strong

strong em strong strong

+

em strong

em strong strong

strong em strong

strong em strong strong

\ No newline at end of file diff --git a/tests/data/em_strong.md b/tests/data/em_strong.md index 748190c..9abeb3f 100644 --- a/tests/data/em_strong.md +++ b/tests/data/em_strong.md @@ -1,9 +1,13 @@ +___em strong___ + ___em strong_ strong__ __strong _em strong___ __strong _em strong_ strong__ +***em strong*** + ***em strong* strong** **strong *em strong*** diff --git a/tests/data/emphasis.html b/tests/data/emphasis.html index a5f107b..9d3f871 100644 --- a/tests/data/emphasis.html +++ b/tests/data/emphasis.html @@ -1,7 +1,8 @@

underscore, asterisk, one two, three four, a, b

-

multiline -emphasis

-

_ this _ is not an emphasis, neither is _ this_, _this _, or _this*

+

strong and em and strong and em

+

line +line +line

this_is_not_an_emphasis

an empty emphasis __ ** is not an emphasis

*mixed *double and single asterisk** spans

\ No newline at end of file diff --git a/tests/data/emphasis.md b/tests/data/emphasis.md index 132681d..a8f0c98 100644 --- a/tests/data/emphasis.md +++ b/tests/data/emphasis.md @@ -1,9 +1,10 @@ _underscore_, *asterisk*, _one two_, *three four*, _a_, *b* -_multiline -emphasis_ +**strong** and *em* and **strong** and *em* -_ this _ is not an emphasis, neither is _ this_, _this _, or _this* +_line +line +line_ this_is_not_an_emphasis diff --git a/tests/data/strong_em.html b/tests/data/strong_em.html index 87764fa..b709c99 100644 --- a/tests/data/strong_em.html +++ b/tests/data/strong_em.html @@ -1,6 +1,6 @@ -

strong em

em strong em

strong em em

-

strong em

+

em strong em em

em strong em

-

strong em em

\ No newline at end of file +

strong em em

+

em strong em em

\ No newline at end of file diff --git a/tests/data/strong_em.md b/tests/data/strong_em.md index 4336eec..f2aa3c7 100644 --- a/tests/data/strong_em.md +++ b/tests/data/strong_em.md @@ -1,11 +1,11 @@ -***strong em*** - *em **strong em*** ***strong em** em* -___strong em___ +*em **strong em** em* _em __strong em___ -___strong em__ em_ \ No newline at end of file +___strong em__ em_ + +_em __strong em__ em_ \ No newline at end of file