improve parsing of emphasis

**strong** and *em* and **strong** and *em*
This commit is contained in:
Emanuil Rusev 2013-12-24 23:40:12 +02:00
parent 68484504ca
commit be366b63ea
7 changed files with 55 additions and 45 deletions

View File

@ -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 .= '<strong>'.$matches[1].'</strong>';
}
elseif (preg_match($this->em_regex[$closest_marker], $text, $matches))
{
$matches[1] = $this->parse_span_elements($matches[1], $markers);
$markup .= '<em>'.$matches[1].'</em>';
}
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 .= '<strong>'.$matches[1].'<em>'.$matches[2].'</em>'.$matches[3].'</strong>';
}
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 .= '<em>'.$matches[1].'<strong>'.$matches[2].'</strong>'.$matches[3].'</em>';
}
elseif ($text[1] === $closest_marker)
{
if (preg_match($this->strong_em_regex[$closest_marker], $text, $matches))
{
$markup .= '<strong>'.$matches[1].'<em>'.$matches[2].'</em>'.$matches[3].'</strong>';
}
elseif (preg_match($this->strong_regex[$closest_marker], $text, $matches))
{
$matches[1] = $this->parse_span_elements($matches[1], $markers);
$markup .= '<strong>'.$matches[1].'</strong>';
}
}
elseif (preg_match($this->em_regex[$closest_marker], $text, $matches))
{
$element_text = $this->parse_span_elements($matches[1], $markers);
$markup .= '<em>'.$element_text.'</em>';
}
if ($matches)
if (isset($matches) and $matches)
{
$offset = strlen($matches[0]);
}

View File

@ -1,6 +1,8 @@
<p><strong><em>em strong</em></strong></p>
<p><strong><em>em strong</em> strong</strong></p>
<p><strong>strong <em>em strong</em></strong></p>
<p><strong>strong <em>em strong</em> strong</strong></p>
<p><strong><em>em strong</em></strong></p>
<p><strong><em>em strong</em> strong</strong></p>
<p><strong>strong <em>em strong</em></strong></p>
<p><strong>strong <em>em strong</em> strong</strong></p>

View File

@ -1,9 +1,13 @@
___em strong___
___em strong_ strong__
__strong _em strong___
__strong _em strong_ strong__
***em strong***
***em strong* strong**
**strong *em strong***

View File

@ -1,7 +1,8 @@
<p><em>underscore</em>, <em>asterisk</em>, <em>one two</em>, <em>three four</em>, <em>a</em>, <em>b</em></p>
<p><em>multiline
emphasis</em></p>
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p>
<p><strong>strong</strong> and <em>em</em> and <strong>strong</strong> and <em>em</em></p>
<p><em>line
line
line</em></p>
<p>this_is_not_an_emphasis</p>
<p>an empty emphasis __ ** is not an emphasis</p>
<p>*mixed *<em>double and</em> single asterisk** spans</p>

View File

@ -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

View File

@ -1,6 +1,6 @@
<p><em><strong>strong em</strong></em> </p>
<p><em>em <strong>strong em</strong></em></p>
<p><em><strong>strong em</strong> em</em></p>
<p><em><strong>strong em</strong></em></p>
<p><em>em <strong>strong em</strong> em</em></p>
<p><em>em <strong>strong em</strong></em></p>
<p><em><strong>strong em</strong> em</em></p>
<p><em><strong>strong em</strong> em</em></p>
<p><em>em <strong>strong em</strong> em</em></p>

View File

@ -1,11 +1,11 @@
***strong em***
*em **strong em***
***strong em** em*
___strong em___
*em **strong em** em*
_em __strong em___
___strong em__ em_
___strong em__ em_
_em __strong em__ em_