From 5353ebb524005cf570e04e791a7659aefeb8cc33 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 8 Apr 2018 20:29:09 +0100 Subject: [PATCH] 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']; } # ~