From b917c74bdf1318477c7d026d4e9789fa397491a1 Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Sat, 26 Feb 2022 19:39:24 +0100 Subject: [PATCH] implemented code blocks --- src/Lexer.php | 8 +++++--- src/Parser.php | 53 +++++++++++++++++++++++++++++++++++++++++++++----- test/test1.md | 9 +++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/Lexer.php b/src/Lexer.php index 8a6cb03..51a4ac7 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -14,8 +14,10 @@ class Lexer { $tokens = []; foreach ($this->source as $line) { - if (strlen($line) < 1) + if (strlen($line) < 1) { + array_push($tokens, new Token(TokenType::EOL, "\n")); continue; + } $buffer = ""; $number = false; @@ -59,10 +61,10 @@ class Lexer { } } $clearBuffer(); - array_push($tokens, new Token(TokenType::EOL)); + array_push($tokens, new Token(TokenType::EOL, "\n")); } $clearBuffer(); - array_push($tokens, new Token(TokenType::EOF)); + array_push($tokens, new Token(TokenType::EOF, "\0")); return $tokens; } diff --git a/src/Parser.php b/src/Parser.php index 47f05cf..c95364c 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -81,7 +81,7 @@ class Parser { $buffer = ""; }; - while ($this->current()->type !== TokenType::EOL) { + while ($this->current()->type !== TokenType::EOL && $this->current()->type !== TokenType::EOF) { if ($this->current()->type === TokenType::ASTERISK) { $clearBuffer(); if ($this->next()->type === TokenType::ASTERISK) { @@ -144,11 +144,8 @@ class Parser { $elm->appendChild($node); $list->appendChild($elm); } else { - $elm = $this->document->createElement("p"); $elms = $this->parseText(); - foreach ($elms as $node) - $elm->appendChild($node); - $this->document->appendChild($elm); + $this->buildParagraph($elms); continue; } } @@ -156,6 +153,13 @@ class Parser { $this->document->appendChild($list); } + private function buildParagraph(array $elms) : void { + $elm = $this->document->createElement("p"); + foreach ($elms as $node) + $elm->appendChild($node); + $this->document->appendChild($elm); + } + private function parseHeading() : void { $level = 0; @@ -169,6 +173,42 @@ class Parser { $this->document->appendChild($elm); } + private function parseCodeBlock() : void { + if (!($this->next()->type === TokenType::BACKTICK && $this->peek(2)->type === TokenType::BACKTICK)) { + $this->buildParagraph($this->parseText()); + + return; + } + $this->consume(); + $this->consume(); + $this->consume(); // ``` + + $lang = $this->parseText(); + $lang = count($lang) > 0 ? trim($lang[0]->data) : null; + + $container = $this->document->createElement("pre"); + if ($lang) + $container->setAttribute("data-lang", $lang); + + $buffer = ""; + while (!($this->current()->type === TokenType::BACKTICK && + $this->next()->type === TokenType::BACKTICK && + $this->peek(2)->type === TokenType::BACKTICK) && $this->current()->type !== TokenType::EOF) { + $buffer .= $this->consume()->data; + } + echo $buffer; + if ($this->current()->type !== TokenType::EOF) { + $this->consume(); + $this->consume(); + $this->consume(); + } + + $elm = $this->document->createElement("code", $buffer); + $container->appendChild($elm); + $this->document->appendChild($container); + $this->consume(); + } + public function parse() : string { while ($this->current()->type !== TokenType::EOF) { switch($this->current()->type) { @@ -187,6 +227,9 @@ class Parser { case TokenType::NUMBER: $this->parseOrderedList(); break; + case TokenType::BACKTICK: + $this->parseCodeBlock(); + break; case TokenType::EOL: $this->consume(); break; diff --git a/test/test1.md b/test/test1.md index b80f1e2..80e0945 100644 --- a/test/test1.md +++ b/test/test1.md @@ -14,3 +14,12 @@ Lorem **ipsum** dolor sit *amet*, `consetetur` sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. + +``` + //this should be a codeblock + function helloWorld() : void { + echo "Hello World!"; + } + + helloWorld(); +``` \ No newline at end of file