1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

improve parsing of emphasis

This commit is contained in:
Emanuil Rusev 2013-12-07 17:21:36 +02:00
parent 7fb08f334a
commit 51a08fad85
5 changed files with 18 additions and 12 deletions

View File

@ -784,18 +784,15 @@ class Parsedown
if (strpos($text, '_') !== FALSE) if (strpos($text, '_') !== FALSE)
{ {
$text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count); $text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count);
$count or $text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text); $text = preg_replace('/(\b|_)_(?=\S)([^_]+?)(?<=\S)_(\b|_)/s', '$1<em>$2</em>$3', $text);
$text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count);
$text = preg_replace('/\b_(?=\S)(.+?)(?<=\S)_\b/s', '<em>$1</em>', $text);
} }
if (strpos($text, '*') !== FALSE) if (strpos($text, '*') !== FALSE)
{ {
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text, -1, $count); $text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text);
$count or $text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text); $text = preg_replace('/\*(?=\S)([^*]+?)(?<=\S)\*/s', '<em>$1</em>', $text);
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\*(?=\S)([^*]+?)(?<=\S)\*/s', '<em>$1</em>', $text, -1, $count);
$count or $text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*(?!\*)/s', '<em>$1</em>', $text);
} }
$text = strtr($text, $map); $text = strtr($text, $map);

View File

@ -1,4 +1,6 @@
<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</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>

View File

@ -4,4 +4,8 @@ __strong _em strong___
__strong _em strong_ strong__ __strong _em strong_ strong__
***em strong* strong**
**strong *em strong*** **strong *em strong***
**strong *em strong* strong**

View File

@ -4,3 +4,4 @@ emphasis</em></p>
<p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</p> <p>_ this _ is not an emphasis, neither is _ this_, _this _, or _this*</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>

View File

@ -8,3 +8,5 @@ _ this _ is not an emphasis, neither is _ this_, _this _, or _this*
this_is_not_an_emphasis this_is_not_an_emphasis
an empty emphasis __ ** is not an emphasis an empty emphasis __ ** is not an emphasis
*mixed **double and* single asterisk** spans