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

improve CommonMark compliance

This commit is contained in:
Emanuil Rusev 2014-12-15 00:52:03 +02:00
parent 4c24e68b42
commit 361febf7c6
2 changed files with 43 additions and 12 deletions

View File

@ -301,8 +301,13 @@ class Parsedown
# #
# Code # Code
protected function identifyCodeBlock($Line) protected function identifyCodeBlock($Line, $Block = null)
{ {
if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted']))
{
return;
}
if ($Line['indent'] >= 4) if ($Line['indent'] >= 4)
{ {
$text = substr($Line['body'], 4); $text = substr($Line['body'], 4);
@ -968,6 +973,7 @@ class Parsedown
protected $SpanTypes = array( protected $SpanTypes = array(
'!' => array('Link'), # ? '!' => array('Link'), # ?
'"' => array('QuotationMark'),
'&' => array('Ampersand'), '&' => array('Ampersand'),
'*' => array('Emphasis'), '*' => array('Emphasis'),
'/' => array('Url'), '/' => array('Url'),
@ -982,7 +988,7 @@ class Parsedown
# ~ # ~
protected $spanMarkerList = '*_!&[<>/`~\\'; protected $spanMarkerList = '!"*_&[<>/`~\\';
# #
# ~ # ~
@ -1141,6 +1147,14 @@ class Parsedown
); );
} }
protected function identifyQuotationMark()
{
return array(
'markup' => '&quot;',
'extent' => 1,
);
}
protected function identifyUrlTag($Excerpt) protected function identifyUrlTag($Excerpt)
{ {
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $Excerpt['text'], $matches)) if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $Excerpt['text'], $matches))
@ -1162,15 +1176,22 @@ class Parsedown
protected function identifyEmailTag($Excerpt) protected function identifyEmailTag($Excerpt)
{ {
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\S+?@\S+?)>/', $Excerpt['text'], $matches)) if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches))
{ {
$url = $matches[1];
if ( ! isset($matches[2]))
{
$url = 'mailto:' . $url;
}
return array( return array(
'extent' => strlen($matches[0]), 'extent' => strlen($matches[0]),
'element' => array( 'element' => array(
'name' => 'a', 'name' => 'a',
'text' => $matches[1], 'text' => $matches[1],
'attributes' => array( 'attributes' => array(
'href' => 'mailto:'.$matches[1], 'href' => $url,
), ),
), ),
); );
@ -1342,10 +1363,20 @@ class Parsedown
protected function readPlainText($text) protected function readPlainText($text)
{ {
$breakMarker = $this->breaksEnabled ? "\n" : array(" \n", "\\\n"); if (strpos($text, "\n") === false)
{
return $text;
}
$text = str_replace($breakMarker, "<br />\n", $text); if ($this->breaksEnabled)
$text = str_replace(" \n", "\n", $text); {
$text = preg_replace('/[ ]*\n/', "<br />\n", $text);
}
else
{
$text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\n", $text);
$text = str_replace(" \n", "\n", $text);
}
return $text; return $text;
} }

View File

@ -124,11 +124,11 @@ MARKDOWN_WITH_MARKUP;
&lt;/div&gt; &lt;/div&gt;
&lt;/div&gt;</p> &lt;/div&gt;</p>
<p>paragraph</p> <p>paragraph</p>
<p>&lt;style type="text/css"&gt;</p> <p>&lt;style type="text/css"&gt;
<pre><code>p { p {
color: red; color: red;
}</code></pre> }
<p>&lt;/style&gt;</p> &lt;/style&gt;</p>
<p>comment</p> <p>comment</p>
<p>&lt;!-- html comment --&gt;</p> <p>&lt;!-- html comment --&gt;</p>
EXPECTED_HTML; EXPECTED_HTML;