diff --git a/src/Lexer.php b/src/Lexer.php index 6187e62..1b9857b 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -63,6 +63,18 @@ class Lexer { $clearBuffer(); array_push($tokens, new Token(TokenType::RBRACKET, $char)); break; + case '(': + $clearBuffer(); + array_push($tokens, new Token(TokenType::LPAREN, $char)); + break; + case ')': + $clearBuffer(); + array_push($tokens, new Token(TokenType::RPAREN, $char)); + break; + case '!': + $clearBuffer(); + array_push($tokens, new Token(TokenType::BANG, $char)); + break; case ':': if (str_ends_with($buffer, "http") || str_ends_with($buffer, "https")) { $buffer .= $char; diff --git a/src/Parser.php b/src/Parser.php index 7c8c1d7..255fc6b 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -47,7 +47,7 @@ class Parser { private function parseBold() : DOMNode { $buffer = ""; - while ($this->current()->type !== TokenType::ASTERISK) { + while ($this->current()->type !== TokenType::ASTERISK && $this->current()->type !== TokenType::EOL) { $buffer .= $this->consume()->data; } $this->consume(); @@ -58,7 +58,7 @@ class Parser { private function parseItalic() : DOMNode { $buffer = ""; - while ($this->current()->type !== TokenType::ASTERISK) { + while ($this->current()->type !== TokenType::ASTERISK && $this->current()->type !== TokenType::EOL) { $buffer .= $this->consume()->data; } $this->consume(); @@ -109,7 +109,7 @@ class Parser { } private function parseText() : array { - $elms = []; + $elms = []; $buffer = ""; $clearBuffer = function() use (&$elms, &$buffer) { @@ -143,6 +143,40 @@ class Parser { continue; } continue; + } elseif ($this->current()->type === TokenType::BANG) { + $bang = $this->consume(); + if ($this->current()->type !== TokenType::LBRACKET) { + $buffer .= $this->consume()->data; + continue; + } + $lbracket = $this->consume(); + $alt = ""; + while ($this->current()->type !== TokenType::RBRACKET && $this->current()->type !== TokenType::EOL) + $alt .= $this->consume()->data; + + if ($this->current()->type !== TokenType::RBRACKET || $this->next()->type !== TokenType::LPAREN) { + $buffer .= "!"; + $this->pointer -= strlen($alt) + 1; + continue; + } + $rbracket = $this->consume(); + $lparen = $this->consume(); + $src = ""; + while ($this->current()->type !== TokenType::RPAREN && $this->current()->type !== TokenType::EOL) + $src .= $this->consume()->data; + if ($this->current()->type !== TokenType::RPAREN) { + $buffer .= "]("; + $this->pointer -= strlen($alt) + 1; + continue; + } + $rparen = $this->consume(); + $elm = $this->document->createElement("img"); + if (strlen($alt) > 0) + $elm->setAttribute("alt", $alt); + $elm->setAttribute("src", $src); + $clearBuffer(); + array_push($elms, $elm); + continue; } else $buffer .= $this->consume()->data; } @@ -153,7 +187,12 @@ class Parser { } private function parseUnorderedList() : void { - $list = $this->document->createElement("ul"); + $list = $this->document->createElement("ul"); + + if (!str_starts_with($this->next()->data, " ")) { + $this->buildParagraph($this->parseText()); + return; + } while (!($this->current()->type === TokenType::EOL && $this->next()->type !== TokenType::ASTERISK) && $this->current()->type !== TokenType::EOF) { if ($this->current()->type === TokenType::EOL) { @@ -166,6 +205,8 @@ class Parser { foreach($this->parseText() as $node) $elm->appendChild($node); $list->appendChild($elm); + } else { + break; } } $this->consume(); @@ -320,8 +361,7 @@ class Parser { $this->parseReference(); break; default: - $c = $this->consume(); - echo "::".$c->type->name."::"; + $this->buildParagraph($this->parseText()); break; } diff --git a/src/Token.php b/src/Token.php index a2bb4ff..4c3cb48 100644 --- a/src/Token.php +++ b/src/Token.php @@ -13,7 +13,10 @@ enum TokenType { case BACKTICK; case LBRACKET; case RBRACKET; - case COLON; + case LPAREN ; + case RPAREN ; + case BANG ; + case COLON ; } class Token { diff --git a/test/test1.md b/test/test1.md index 32fcdb4..a06653b 100644 --- a/test/test1.md +++ b/test/test1.md @@ -15,6 +15,12 @@ Lorem **ipsum** dolor sit *amet*, `consetetur` sadipscing elitr, sed diam nonum And we test: all [special chars][1] in random [text][hallo]. this [word][http://lol.de] is a link. +### Testing an Image +**bold** at the beginning + +lol 1 bild ![Eine Perle: für **die Ewigkeit* ..](https://mike-ochmann.de/assets/images/perl_jam.jpg) + + 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. ```