From 39df7d4f8ea22f67c03ab1776f4b8f3efb4db365 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 8 Apr 2018 20:27:44 +0100 Subject: [PATCH 1/5] Swap 'hidden' blocks for empty elements --- Parsedown.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 0a1bee5..f9f269e 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -878,18 +878,13 @@ class Parsedown $Data = array( 'url' => $matches[2], - 'title' => null, + 'title' => isset($matches[3]) ? $matches[3] : null, ); - if (isset($matches[3])) - { - $Data['title'] = $matches[3]; - } - $this->DefinitionData['Reference'][$id] = $Data; $Block = array( - 'hidden' => true, + 'element' => array(), ); return $Block; @@ -1776,6 +1771,11 @@ class Parsedown foreach ($Elements as $Element) { + if (empty($Element)) + { + continue; + } + $autoBreakNext = (isset($Element['autobreak']) && $Element['autobreak'] || ! isset($Element['autobreak']) && isset($Element['name']) ); From 5353ebb524005cf570e04e791a7659aefeb8cc33 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 8 Apr 2018 20:29:09 +0100 Subject: [PATCH 2/5] Avoid needing two arrays We only need to collect elements, we can discard finished blocks --- Parsedown.php | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index f9f269e..090bd7f 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -174,6 +174,7 @@ class Parsedown protected function linesElements(array $lines) { + $Elements = array(); $CurrentBlock = null; foreach ($lines as $line) @@ -278,7 +279,10 @@ class Parsedown if ( ! isset($Block['identified'])) { - $Blocks []= $CurrentBlock; + if (isset($CurrentBlock)) + { + $Elements[] = $CurrentBlock['element']; + } $Block['identified'] = true; } @@ -306,7 +310,10 @@ class Parsedown } else { - $Blocks []= $CurrentBlock; + if (isset($CurrentBlock)) + { + $Elements[] = $CurrentBlock['element']; + } $CurrentBlock = $this->paragraph($Line); @@ -323,22 +330,9 @@ class Parsedown # ~ - $Blocks []= $CurrentBlock; - - unset($Blocks[0]); - - # ~ - - $Elements = array(); - - foreach ($Blocks as $Block) + if (isset($CurrentBlock)) { - if (isset($Block['hidden'])) - { - continue; - } - - $Elements[] = $Block['element']; + $Elements[] = $CurrentBlock['element']; } # ~ From ae8067e862dda3070aab204bc6f826ac1296b34d Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 8 Apr 2018 20:34:57 +0100 Subject: [PATCH 3/5] Swap undefined type for type === 'Paragraph' for ease of reading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The way in which we use this assumes that it is a paragraph, for example appending text into the handler argument — so there is no loss of generality here, we're simply being explicit. --- Parsedown.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 090bd7f..ffdc83d 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -302,8 +302,7 @@ class Parsedown if ( isset($CurrentBlock) - and isset($CurrentBlock['element']['name']) - and $CurrentBlock['element']['name'] === 'p' + and $CurrentBlock['type'] === 'Paragraph' and ! isset($CurrentBlock['interrupted']) ) { $CurrentBlock['element']['handler']['argument'] .= "\n".$text; @@ -355,7 +354,7 @@ class Parsedown protected function blockCode($Line, $Block = null) { - if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted'])) + if (isset($Block) and $Block['type'] === 'Paragraph' and ! isset($Block['interrupted'])) { return; } @@ -610,7 +609,7 @@ class Parsedown { if ( isset($CurrentBlock) - and ! isset($CurrentBlock['type']) + and $CurrentBlock['type'] === 'Paragraph' and ! isset($CurrentBlock['interrupted']) ) { return; @@ -803,7 +802,7 @@ class Parsedown protected function blockSetextHeader($Line, array $Block = null) { - if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) + if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) { return; } @@ -890,7 +889,7 @@ class Parsedown protected function blockTable($Line, array $Block = null) { - if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) + if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) { return; } @@ -1070,18 +1069,17 @@ class Parsedown protected function paragraph($Line) { - $Block = array( + return array( + 'type' => 'Paragraph', 'element' => array( 'name' => 'p', 'handler' => array( 'function' => 'lineElements', 'argument' => $Line['text'], 'destination' => 'elements', - ) + ), ), ); - - return $Block; } # From e4cd13350b11f676afa0fd1e270078f65d0a0cff Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 9 Apr 2018 15:11:45 +0100 Subject: [PATCH 4/5] Remove setLiteralBreaks --- Parsedown.php | 21 ++------------------- test/ParsedownTest.php | 1 - test/data/literal_breaks.html | 6 ------ test/data/literal_breaks.md | 6 ------ 4 files changed, 2 insertions(+), 32 deletions(-) delete mode 100644 test/data/literal_breaks.html delete mode 100644 test/data/literal_breaks.md diff --git a/Parsedown.php b/Parsedown.php index ffdc83d..e62ddd3 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -65,15 +65,6 @@ class Parsedown protected $breaksEnabled; - function setLiteralBreaks($literalBreaks) - { - $this->literalBreaks = $literalBreaks; - - return $this; - } - - protected $literalBreaks; - function setMarkupEscaped($markupEscaped) { $this->markupEscaped = $markupEscaped; @@ -179,7 +170,7 @@ class Parsedown foreach ($lines as $line) { - if ( ! $this->literalBreaks and chop($line) === '') + if (chop($line) === '') { if (isset($CurrentBlock)) { @@ -244,15 +235,7 @@ class Parsedown # ~ - if (isset($text[0])) - { - $marker = $text[0]; - } - elseif ($this->literalBreaks) - { - $marker = '\n'; - $text = ' '; - } + $marker = $text[0]; # ~ diff --git a/test/ParsedownTest.php b/test/ParsedownTest.php index d92e4fc..bf40317 100755 --- a/test/ParsedownTest.php +++ b/test/ParsedownTest.php @@ -52,7 +52,6 @@ class ParsedownTest extends TestCase $this->Parsedown->setSafeMode(substr($test, 0, 3) === 'xss'); $this->Parsedown->setStrictMode(substr($test, 0, 6) === 'strict'); - $this->Parsedown->setLiteralBreaks(substr($test, 0, 14) === 'literal_breaks'); $actualMarkup = $this->Parsedown->text($markdown); diff --git a/test/data/literal_breaks.html b/test/data/literal_breaks.html deleted file mode 100644 index bf9e273..0000000 --- a/test/data/literal_breaks.html +++ /dev/null @@ -1,6 +0,0 @@ -

first line -
-
-
-
-sixth line

\ No newline at end of file diff --git a/test/data/literal_breaks.md b/test/data/literal_breaks.md deleted file mode 100644 index 2b95a92..0000000 --- a/test/data/literal_breaks.md +++ /dev/null @@ -1,6 +0,0 @@ -first line - - - - -sixth line \ No newline at end of file From 043c55e4c6f6cb2bfb6b9399e2f73b56f83ddb8b Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 9 Apr 2018 15:12:17 +0100 Subject: [PATCH 5/5] Give paragraph block semantics for overloading --- Parsedown.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index e62ddd3..9e02e16 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -283,12 +283,14 @@ class Parsedown # ~ - if ( - isset($CurrentBlock) - and $CurrentBlock['type'] === 'Paragraph' - and ! isset($CurrentBlock['interrupted']) - ) { - $CurrentBlock['element']['handler']['argument'] .= "\n".$text; + if (isset($CurrentBlock) and $CurrentBlock['type'] === 'Paragraph') + { + $Block = $this->paragraphContinue($Line, $CurrentBlock); + } + + if (isset($Block)) + { + $CurrentBlock = $Block; } else { @@ -1065,6 +1067,18 @@ class Parsedown ); } + protected function paragraphContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + $Block['element']['handler']['argument'] .= "\n".$Line['text']; + + return $Block; + } + # # Inline Elements #