mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
improve parsing of emphasis
**strong** and *em* and **strong** and *em*
This commit is contained in:
parent
68484504ca
commit
be366b63ea
@ -631,9 +631,14 @@ class Parsedown
|
|||||||
|
|
||||||
# ~
|
# ~
|
||||||
|
|
||||||
private $em_strong_regex = array(
|
private $strong_regex = array(
|
||||||
'*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s',
|
'*' => '/^[*]{2}([^*]+?)[*]{2}(?![*])/s',
|
||||||
'_' => '/^_(.*?)__(.+?)__(.*?)_/s',
|
'_' => '/^__([^_]+?)__(?!_)/s',
|
||||||
|
);
|
||||||
|
|
||||||
|
private $em_regex = array(
|
||||||
|
'*' => '/^[*]([^*]+?)[*](?![*])/s',
|
||||||
|
'_' => '/^_([^_]+?)[_](?![_])\b/s',
|
||||||
);
|
);
|
||||||
|
|
||||||
private $strong_em_regex = array(
|
private $strong_em_regex = array(
|
||||||
@ -641,14 +646,9 @@ class Parsedown
|
|||||||
'_' => '/^__(.*?)_(.+?)_(.*?)__/s',
|
'_' => '/^__(.*?)_(.+?)_(.*?)__/s',
|
||||||
);
|
);
|
||||||
|
|
||||||
private $strong_regex = array(
|
private $em_strong_regex = array(
|
||||||
'*' => '/^[*]{2}(.+?)[*]{2}/s',
|
'*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s',
|
||||||
'_' => '/^__(.+?)__/s',
|
'_' => '/^_(.*?)__(.+?)__(.*?)_/s',
|
||||||
);
|
|
||||||
|
|
||||||
private $em_regex = array(
|
|
||||||
'*' => '/^[*](.+?)[*]/s',
|
|
||||||
'_' => '/^_(.+?)_\b/s',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '_', '`', 'http', '~~'))
|
private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '_', '`', 'http', '~~'))
|
||||||
@ -806,7 +806,28 @@ class Parsedown
|
|||||||
case '*':
|
case '*':
|
||||||
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);
|
$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>';
|
$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>';
|
if (isset($matches) and $matches)
|
||||||
}
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
$offset = strlen($matches[0]);
|
$offset = strlen($matches[0]);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
<p><strong><em>em strong</em></strong></p>
|
||||||
<p><strong><em>em strong</em> strong</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></p>
|
||||||
<p><strong>strong <em>em strong</em> strong</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><em>em strong</em> strong</strong></p>
|
||||||
<p><strong>strong <em>em strong</em></strong></p>
|
<p><strong>strong <em>em strong</em></strong></p>
|
||||||
<p><strong>strong <em>em strong</em> strong</strong></p>
|
<p><strong>strong <em>em strong</em> strong</strong></p>
|
@ -1,9 +1,13 @@
|
|||||||
|
___em strong___
|
||||||
|
|
||||||
___em strong_ strong__
|
___em strong_ strong__
|
||||||
|
|
||||||
__strong _em strong___
|
__strong _em strong___
|
||||||
|
|
||||||
__strong _em strong_ strong__
|
__strong _em strong_ strong__
|
||||||
|
|
||||||
|
***em strong***
|
||||||
|
|
||||||
***em strong* strong**
|
***em strong* strong**
|
||||||
|
|
||||||
**strong *em strong***
|
**strong *em strong***
|
||||||
|
@ -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>underscore</em>, <em>asterisk</em>, <em>one two</em>, <em>three four</em>, <em>a</em>, <em>b</em></p>
|
||||||
<p><em>multiline
|
<p><strong>strong</strong> and <em>em</em> and <strong>strong</strong> and <em>em</em></p>
|
||||||
emphasis</em></p>
|
<p><em>line
|
||||||
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p>
|
line
|
||||||
|
line</em></p>
|
||||||
<p>this_is_not_an_emphasis</p>
|
<p>this_is_not_an_emphasis</p>
|
||||||
<p>an empty emphasis __ ** is not an emphasis</p>
|
<p>an empty emphasis __ ** is not an emphasis</p>
|
||||||
<p>*mixed *<em>double and</em> single asterisk** spans</p>
|
<p>*mixed *<em>double and</em> single asterisk** spans</p>
|
@ -1,9 +1,10 @@
|
|||||||
_underscore_, *asterisk*, _one two_, *three four*, _a_, *b*
|
_underscore_, *asterisk*, _one two_, *three four*, _a_, *b*
|
||||||
|
|
||||||
_multiline
|
**strong** and *em* and **strong** and *em*
|
||||||
emphasis_
|
|
||||||
|
|
||||||
_ this _ is not an emphasis, neither is _ this_, _this _, or _this*
|
_line
|
||||||
|
line
|
||||||
|
line_
|
||||||
|
|
||||||
this_is_not_an_emphasis
|
this_is_not_an_emphasis
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<p><em><strong>strong em</strong></em> </p>
|
|
||||||
<p><em>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</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>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>
|
@ -1,11 +1,11 @@
|
|||||||
***strong em***
|
|
||||||
|
|
||||||
*em **strong em***
|
*em **strong em***
|
||||||
|
|
||||||
***strong em** em*
|
***strong em** em*
|
||||||
|
|
||||||
___strong em___
|
*em **strong em** em*
|
||||||
|
|
||||||
_em __strong em___
|
_em __strong em___
|
||||||
|
|
||||||
___strong em__ em_
|
___strong em__ em_
|
||||||
|
|
||||||
|
_em __strong em__ em_
|
Loading…
x
Reference in New Issue
Block a user