added support for images

feature/tests
Michael Ochmann 3 years ago
parent 2d7cf901b9
commit 65744f1363
  1. 12
      src/Lexer.php
  2. 48
      src/Parser.php
  3. 5
      src/Token.php
  4. 6
      test/test1.md

@ -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;

@ -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();
@ -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;
}
@ -155,6 +189,11 @@ class Parser {
private function parseUnorderedList() : void {
$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) {
$this->consume();
@ -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;
}

@ -13,7 +13,10 @@ enum TokenType {
case BACKTICK;
case LBRACKET;
case RBRACKET;
case COLON;
case LPAREN ;
case RPAREN ;
case BANG ;
case COLON ;
}
class Token {

@ -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.
```

Loading…
Cancel
Save