diff --git a/src/Lexer.php b/src/Lexer.php index af7033f..82b9222 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -83,6 +83,10 @@ class Lexer { $clearBuffer(); array_push($tokens, new Token(TokenType::PIPE, $char)); break; + case '\\': + $clearBuffer(); + array_push($tokens, new Token(TokenType::BACKSLASH, $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 ccdd37e..a09bb8e 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -44,6 +44,10 @@ class Parser { return $char; } + private static function StripBackslashes(string $text) : string { + return stripslashes($text); + } + // PARSING private function parseBold() : DOMNode { @@ -122,7 +126,16 @@ class Parser { if ((!$paragraph && $this->current()->type === TokenType::EOL) || ($paragraph && ($this->current()->type === TokenType::EOL && $this->next()->type === TokenType::EOL)) || $this->current()->type === TokenType::EOF) break; - if ($this->current()->type === TokenType::ASTERISK) { + if ($this->current()->type === TokenType::BACKSLASH && in_array($this->next()->type, [ + TokenType::BACKTICK, + TokenType::ASTERISK, + TokenType::LBRACKET, + TokenType::BANG + ])) { + $this->consume()->data; // backslash + $buffer .= $this->consume()->data; + continue; + } elseif ($this->current()->type === TokenType::ASTERISK) { $clearBuffer(); if ($this->next()->type === TokenType::ASTERISK) { $this->consume(); @@ -143,20 +156,20 @@ class Parser { $clearBuffer(); array_push($elms, $links); } else { - $buffer .= $this->consume()->data; + $buffer .= self::StripBackslashes($this->consume()->data); continue; } continue; } elseif ($this->current()->type === TokenType::BANG) { $bang = $this->consume(); if ($this->current()->type !== TokenType::LBRACKET) { - $buffer .= $this->consume()->data; + $buffer .= self::StripBackslashes($this->consume()->data); continue; } $lbracket = $this->consume(); $alt = ""; while ($this->current()->type !== TokenType::RBRACKET && $this->current()->type !== TokenType::EOL) - $alt .= $this->consume()->data; + $alt .= self::StripBackslashes($this->consume()->data); if ($this->current()->type !== TokenType::RBRACKET || $this->next()->type !== TokenType::LPAREN) { $buffer .= "!"; @@ -182,7 +195,7 @@ class Parser { array_push($elms, $elm); continue; } else - $buffer .= $this->consume()->data; + $buffer .= self::StripBackslashes($this->consume()->data); } if (strlen($buffer) > 0) array_push($elms, $this->document->createTextNode($buffer)); @@ -285,7 +298,7 @@ class Parser { 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; + $buffer .= self::StripBackslashes($this->consume()->data); } if ($this->current()->type !== TokenType::EOF) { $this->consume(); @@ -412,6 +425,12 @@ class Parser { } } + public function debug() : void { + echo "
";
+		print_r($this->tokenStream);
+		echo "
"; + } + public function parse() : DOMDocument { while ($this->current()->type !== TokenType::EOF) { switch($this->current()->type) { diff --git a/src/Token.php b/src/Token.php index 3d5711c..967e875 100644 --- a/src/Token.php +++ b/src/Token.php @@ -3,22 +3,23 @@ namespace parkdown; enum TokenType { - case HASH ; - case ASTERISK; - case TEXT ; - case DOT ; - case MINUS ; - case NUMBER ; - case EOL ; - case EOF ; - case BACKTICK; - case LBRACKET; - case RBRACKET; - case LPAREN ; - case RPAREN ; - case BANG ; - case COLON ; - case PIPE ; + case HASH ; + case ASTERISK ; + case TEXT ; + case DOT ; + case MINUS ; + case NUMBER ; + case EOL ; + case EOF ; + case BACKTICK ; + case LBRACKET ; + case RBRACKET ; + case LPAREN ; + case RPAREN ; + case BANG ; + case COLON ; + case BACKSLASH; + case PIPE ; } class Token {