From b61060302c563617fd7514c21c9d7fd43f6b716d Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Tue, 1 Mar 2022 18:50:57 +0100 Subject: [PATCH] implemented horizontal rules. closes #1 --- README.md | 6 ++++++ src/Parser.php | 19 +++++++++++++++++++ {test => tests}/paragraph.md | 0 {test => tests}/test1.md | 0 4 files changed, 25 insertions(+) rename {test => tests}/paragraph.md (100%) rename {test => tests}/test1.md (100%) diff --git a/README.md b/README.md index 82f7679..7ac3eeb 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Parkdown currently support the following block types: * tables *(with alignment specification)* * paragraphs * block quotes +* horizontal rules `---` ### Supported inline types Parkdown currently support the following block types: @@ -43,7 +44,12 @@ or via reference to [a later defined url][massivedynamic], i we so desire. ![this is an alt text](https://images.unsplash.com/photo-1571171637578-41bc2dd41cd2?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&h=300&w=1740&q=80) +### Horizontal rules +```markdown +--- +``` +--- ### Block quotes ```markdown > Only two things are infinite, diff --git a/src/Parser.php b/src/Parser.php index 5cea154..1b7e42f 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -457,6 +457,22 @@ class Parser { $this->document->appendChild($elm); } + private function parseHorizontalRule() : void { + if (!($this->next()->type === TokenType::MINUS && + $this->peek(2)->type === TokenType::MINUS && + $this->peek(3)->type === TokenType::EOL)) { + $this->buildParagraph($this->parseText()); + return; + } + $this->consume(); // - + $this->consume(); // - + $this->consume(); // - + $this->consume(); // EOL + + $elm = $this->document->createElement("hr"); + $this->document->appendChild($elm); + } + public function parse() : DOMDocument { while ($this->current()->type !== TokenType::EOF) { switch($this->current()->type) { @@ -484,6 +500,9 @@ class Parser { case TokenType::GT: $this->parseBlockQuote(); break; + case TokenType::MINUS: + $this->parseHorizontalRule(); + break; case TokenType::TEXT: default: $this->buildParagraph($this->parseText(true)); diff --git a/test/paragraph.md b/tests/paragraph.md similarity index 100% rename from test/paragraph.md rename to tests/paragraph.md diff --git a/test/test1.md b/tests/test1.md similarity index 100% rename from test/test1.md rename to tests/test1.md