diff --git a/src/Parser.php b/src/Parser.php index f7da5c7..ccdd37e 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -44,6 +44,8 @@ class Parser { return $char; } + // PARSING + private function parseBold() : DOMNode { $buffer = ""; while ($this->current()->type !== TokenType::ASTERISK && $this->current()->type !== TokenType::EOL) { @@ -107,7 +109,7 @@ class Parser { return $elm; } - private function parseText() : array { + private function parseText($paragraph = false) : array { $elms = []; $buffer = ""; @@ -115,8 +117,11 @@ class Parser { array_push($elms, $this->document->createTextNode($buffer)); $buffer = ""; }; - - while ($this->current()->type !== TokenType::EOL && $this->current()->type !== TokenType::EOF) { + + while ($this->current()->type !== TokenType::EOF) { + 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) { $clearBuffer(); if ($this->next()->type === TokenType::ASTERISK) { @@ -407,7 +412,7 @@ class Parser { } } - public function parse() : string { + public function parse() : DOMDocument { while ($this->current()->type !== TokenType::EOF) { switch($this->current()->type) { case TokenType::ASTERISK: @@ -416,12 +421,6 @@ class Parser { case TokenType::HASH: $this->parseHeading(); break; - case TokenType::TEXT: - $elm = $this->document->createElement("p"); - foreach ($this->parseText() as $node) - $elm->appendChild($node); - $this->document->appendChild($elm); - break; case TokenType::NUMBER: $this->parseOrderedList(); break; @@ -437,14 +436,16 @@ class Parser { case TokenType::PIPE: $this->parseTable(); break; + case TokenType::TEXT: default: - $this->buildParagraph($this->parseText()); + $this->buildParagraph($this->parseText(true)); break; } } foreach($this->document->childNodes as $node) $this->resolveReferences($node); - return $this->document->saveHTML(); + + return $this->document; } } \ No newline at end of file