From 06135cd75a61e8c31429b8987a69b61f4a77ab34 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Sat, 10 Jan 2015 02:45:51 +0200 Subject: [PATCH] improve CommonMark compliance --- Parsedown.php | 27 +++++++++++++++++++++++---- test/data/atx_heading.html | 2 +- test/data/atx_heading.md | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 4579e2f..7a84d06 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -35,9 +35,6 @@ class Parsedown $text = str_replace("\r\n", "\n", $text); $text = str_replace("\r", "\n", $text); - # replace tabs with spaces - $text = str_replace("\t", ' ', $text); - # remove surrounding line breaks $text = trim($text, "\n"); @@ -145,6 +142,23 @@ class Parsedown continue; } + if (strpos($line, "\t") !== false) + { + $parts = explode("\t", $line); + + $line = $parts[0]; + + unset($parts[0]); + + foreach ($parts as $part) + { + $shortage = 4 - mb_strlen($line, 'utf-8') % 4; + + $line .= str_repeat(' ', $shortage); + $line .= $part; + } + } + $indent = 0; while (isset($line[$indent]) and $line[$indent] === ' ') @@ -471,6 +485,11 @@ class Parsedown $level ++; } + if ($level > 6 or $Line['text'][$level] !== ' ') + { + return; + } + $text = trim($Line['text'], '# '); $Block = array( @@ -614,7 +633,7 @@ class Parsedown protected function blockRule($Line) { - if (preg_match('/^(['.$Line['text'][0].'])([ ]{0,2}\1){2,}[ ]*$/', $Line['text'])) + if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text'])) { $Block = array( 'element' => array( diff --git a/test/data/atx_heading.html b/test/data/atx_heading.html index 3ab3fd5..751f873 100644 --- a/test/data/atx_heading.html +++ b/test/data/atx_heading.html @@ -4,6 +4,6 @@

h4

h5
h6
-
h6
+

####### not a heading

closed h1

#

\ No newline at end of file diff --git a/test/data/atx_heading.md b/test/data/atx_heading.md index 5339a5b..ad97b44 100644 --- a/test/data/atx_heading.md +++ b/test/data/atx_heading.md @@ -10,7 +10,7 @@ ###### h6 -####### h6 +####### not a heading # closed h1 #