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

Merge pull request #10 from hkdobrev/emphasis

Better parsing of emphasis and strong elements
This commit is contained in:
Emanuil Rusev 2013-07-23 00:52:41 -07:00
commit 69a620110a
3 changed files with 16 additions and 13 deletions

View File

@ -605,19 +605,13 @@ class Parsedown
$index ++; $index ++;
} }
} }
if (strpos($text, '*') !== FALSE) if (strpos($text, '*') !== FALSE or strpos($text, '_') !== FALSE)
{ {
$text = preg_replace('/\*{2}(.*?)\*{2}/', '<strong>$1</strong>', $text); $text = preg_replace('/(\*\*|__)(.+?[*_]*)(?<=\S)\1/', '<strong>$2</strong>', $text);
$text = preg_replace('/\*(.*?)\*/', '<em>$1</em>', $text); $text = preg_replace('/(\*|_)(.+?)(?<=\S)\1/', '<em>$2</em>', $text);
} }
if (strpos($text, '_') !== FALSE)
{
$text = preg_replace('/_{2}(\S.*?\S)_{2}/', '<strong>$1</strong>', $text);
$text = preg_replace('/_(\S.*?\S)_/', '<em>$1</em>', $text);
}
$text = strtr($text, $map); $text = strtr($text, $map);
return $text; return $text;

View File

@ -1,5 +1,8 @@
<p>Here's <em>an emphasis</em>.</p> <p>Here's <em>an emphasis</em>.</p>
<p>A short emphasis <em>a</em> <em>b</em> .</p>
<p>Here's <strong>a strong one</strong>. </p> <p>Here's <strong>a strong one</strong>. </p>
<p>Here's <em>an emphasis that uses underscores</em>. </p> <p>Here's <em>an emphasis that uses underscores</em>. </p>
<p>Here's <strong>a strong emphasis that uses underscores</strong>.</p> <p>Here's <strong>a strong emphasis that uses underscores</strong>.</p>
<p>This is _ not an emphasis _.</p> <p>This is _ not an emphasis _ neither is * that * .</p>
<p>Empty emphasis ** is not __ an emphasis.</p>
<p>Three asterisks are an emphasized asterisk <em>*</em> .</p>

View File

@ -1,9 +1,15 @@
Here's *an emphasis*. Here's *an emphasis*.
A short emphasis _a_ *b* .
Here's **a strong one**. Here's **a strong one**.
Here's _an emphasis that uses underscores_. Here's _an emphasis that uses underscores_.
Here's __a strong emphasis that uses underscores__. Here's __a strong emphasis that uses underscores__.
This is _ not an emphasis _. This is _ not an emphasis _ neither is * that * .
Empty emphasis ** is not __ an emphasis.
Three asterisks are an emphasized asterisk *** .