fixed paragraphs not being able to have line breaks

feature/tests
Michael Ochmann 3 years ago
parent 03356fc709
commit 4c0ff45e02
  1. 23
      src/Parser.php

@ -44,6 +44,8 @@ class Parser {
return $char; return $char;
} }
// PARSING
private function parseBold() : DOMNode { private function parseBold() : DOMNode {
$buffer = ""; $buffer = "";
while ($this->current()->type !== TokenType::ASTERISK && $this->current()->type !== TokenType::EOL) { while ($this->current()->type !== TokenType::ASTERISK && $this->current()->type !== TokenType::EOL) {
@ -107,7 +109,7 @@ class Parser {
return $elm; return $elm;
} }
private function parseText() : array { private function parseText($paragraph = false) : array {
$elms = []; $elms = [];
$buffer = ""; $buffer = "";
@ -116,7 +118,10 @@ class Parser {
$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) { if ($this->current()->type === TokenType::ASTERISK) {
$clearBuffer(); $clearBuffer();
if ($this->next()->type === TokenType::ASTERISK) { 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) { while ($this->current()->type !== TokenType::EOF) {
switch($this->current()->type) { switch($this->current()->type) {
case TokenType::ASTERISK: case TokenType::ASTERISK:
@ -416,12 +421,6 @@ class Parser {
case TokenType::HASH: case TokenType::HASH:
$this->parseHeading(); $this->parseHeading();
break; 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: case TokenType::NUMBER:
$this->parseOrderedList(); $this->parseOrderedList();
break; break;
@ -437,14 +436,16 @@ class Parser {
case TokenType::PIPE: case TokenType::PIPE:
$this->parseTable(); $this->parseTable();
break; break;
case TokenType::TEXT:
default: default:
$this->buildParagraph($this->parseText()); $this->buildParagraph($this->parseText(true));
break; break;
} }
} }
foreach($this->document->childNodes as $node) foreach($this->document->childNodes as $node)
$this->resolveReferences($node); $this->resolveReferences($node);
return $this->document->saveHTML();
return $this->document;
} }
} }
Loading…
Cancel
Save