diff --git a/playground/index.php b/playground/index.php index a933707..7514cac 100644 --- a/playground/index.php +++ b/playground/index.php @@ -186,16 +186,175 @@
1
diff --git a/src/Parser.php b/src/Parser.php index c62d606..6655ef7 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -83,6 +83,14 @@ class Parser { assert($assertion, new ParserError(self::LOC($token->location).$message)); } + public static function TextToSlug(string $html) : string { + $out = trim(strip_tags($html)); + $out = strtolower($out); + $out = str_replace(" ", "_", $out); + + return $out; + } + private function resolveReferences(DOMElement $node) : void { if (count($this->references) < 1) return; @@ -173,7 +181,7 @@ class Parser { $backtick = $this->consume(); self::Assert($backtick->type === TokenType::BACKTICK, $backtick, "inline code expression not autmatically closed (expected backtick)"); - return $this->document->createElement("code", $buffer); + return @$this->document->createElement("code", $buffer); } private function parseLink() : ?DOMNode { @@ -246,7 +254,11 @@ class Parser { continue; } elseif ($this->current()->type === TokenType::BACKTICK) { $clearBuffer(); - array_push($elms, $this->parseCode()); + $code = $this->parseCode(); + + self::Assert($code !== false, $this->current(), "malformed code block"); + + array_push($elms, $code); continue; } elseif ($this->current()->type === TokenType::LBRACKET) { $links = $this->parseLink(); @@ -364,13 +376,16 @@ class Parser { break; // then we expect an asterisk or a number followed by a period if ($type === ListType::UNORDERED) { - $asterisk = $this->consume(); - self::Assert($asterisk->type === TokenType::ASTERISK, $asterisk, "expected asterisk, got ".$asterisk->type->name); + if ($this->current()->type === TokenType::ASTERISK) + $this->consume(); } else { - $number = $this->consume(); - self::Assert($number->type === TokenType::NUMBER, $number, "expected number, got ".$number->type->name); - $period = $this->consume(); - self::Assert($period->type === TokenType::DOT, $period, "expected period, got ".$period->type->name); + if ($this->current()->type === TokenType::NUMBER) { + $this->consume(); + if ($this->strict && $this->current()->type !== TokenType::DOT) + $this->insert(new Token(TokenType::DOT, ".", $this->current()->location)); + $period = $this->consume(); + self::Assert($period->type === TokenType::DOT, $period, "expected period, got ".$period->type->name); + } } // then we parse the node content @@ -444,6 +459,7 @@ class Parser { foreach ($this->parseText() as $node) if ($node instanceof DOMNode) $elm->appendChild($node); + $elm->setAttribute("id", self::TextToSlug($elm->textContent)); $this->document->appendChild($elm); }