From 565c8dd3cc36eb980e9a5f655cd14b51df5d6597 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 20 Jan 2019 02:29:40 +0000 Subject: [PATCH] Implement IndentedCode --- src/Components/Blocks/IndentedCode.php | 81 ++++++++++++++++++++++++++ src/Parsedown.php | 46 --------------- 2 files changed, 81 insertions(+), 46 deletions(-) create mode 100644 src/Components/Blocks/IndentedCode.php diff --git a/src/Components/Blocks/IndentedCode.php b/src/Components/Blocks/IndentedCode.php new file mode 100644 index 0000000..9760e3b --- /dev/null +++ b/src/Components/Blocks/IndentedCode.php @@ -0,0 +1,81 @@ +code = $code; + } + + /** + * @param Context $Context + * @param Block|null $Block + * @param State|null $State + * @return static|null + */ + public static function build( + Context $Context, + Block $Block = null, + State $State = null + ) { + if (isset($Block) and $Block instanceof Paragraph and ! $Context->previousEmptyLines() > 0) { + return null; + } + + if ($Context->line()->indent() < 4) { + return null; + } + + return new self($Context->line()->ltrimBodyUpto(4)); + } + + /** + * @param Context $Context + * @return self|null + */ + public function continue(Context $Context) + { + if ($Context->line()->indent() < 4) { + return null; + } + + $newCode = $this->code; + + if ($Context->previousEmptyLines() > 0) { + $newCode .= \str_repeat("\n", $Context->previousEmptyLines()); + } + + return new self($newCode . "\n" . $Context->line()->ltrimBodyUpto(4)); + } + + /** + * @return Element + */ + public function stateRenderable(Parsedown $_) + { + return new Element( + 'pre', + [], + [new Element('code', [], [new Text($this->code)])] + ); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index deb0572..4975eae 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -288,52 +288,6 @@ class Parsedown return \method_exists($this, 'block' . $Type . 'Complete'); } - # - # Code - - protected function blockCode(Context $Context, $Block = null) - { - if (isset($Block) and $Block['type'] === 'Paragraph' and ! $Context->previousEmptyLines() > 0) { - return; - } - - if ($Context->line()->indent() >= 4) { - $Block = [ - 'element' => [ - 'name' => 'pre', - 'element' => [ - 'name' => 'code', - 'text' => $Context->line()->ltrimBodyUpto(4), - ], - ], - ]; - - return $Block; - } - } - - protected function blockCodeContinue(Context $Context, $Block) - { - if ($Context->line()->indent() >= 4) { - if ($Context->previousEmptyLines() > 0) { - $Block['element']['element']['text'] .= \str_repeat("\n", $Context->previousEmptyLines()); - - unset($Block['interrupted']); - } - - $Block['element']['element']['text'] .= "\n"; - - $Block['element']['element']['text'] .= $Context->line()->ltrimBodyUpto(4); - - return $Block; - } - } - - protected function blockCodeComplete($Block) - { - return $Block; - } - # # Reference