added support for blockquotes

feature/tests
Michael Ochmann 3 years ago
parent 7f70681411
commit 349d5bc012
  1. 19
      README.md
  2. 4
      src/Lexer.php
  3. 64
      src/Parser.php
  4. 1
      src/Token.php

@ -11,11 +11,12 @@ Parkdown currently support the following block types:
* codeblocks *(with the ability to specify a language for the code block)* * codeblocks *(with the ability to specify a language for the code block)*
* tables *(with alignment specification)* * tables *(with alignment specification)*
* paragraphs * paragraphs
* block quotes
### Supported inline types ### Supported inline types
Parkdown currently support the following block types: Parkdown currently support the following block types:
* bold text (`**bold**`) * bold text (`**bold**`)
* italic text (`*italic*`) * italic text (`*italic*`)
* code snippets * code snippets
* images (`![alt text](src url)`) * images (`![alt text](src url)`)
@ -26,7 +27,6 @@ Parkdown currently support the following block types:
* references (`[marker]: URL`) * references (`[marker]: URL`)
## Examples ## Examples
### Paragraphs ### Paragraphs
```markdown ```markdown
A simple paragraph can contain **bold text**, `inline codeblocks` and *italic text*. We can also link [with a direct url][https://google.com] *(i.e. to google)* A simple paragraph can contain **bold text**, `inline codeblocks` and *italic text*. We can also link [with a direct url][https://google.com] *(i.e. to google)*
@ -43,6 +43,21 @@ 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) ![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)
### Block quotes
```markdown
> Only two things are infinite,
> the universe and hima stupidity,
> i am not totally shure about the universe, though...
> - Albert Einstein
```
> Only two things are infinite,
> the universe and hima stupidity,
> i am not totally shure about the universe, though...
> - Albert Einstein
### Code blocks ### Code blocks
```markdown ```markdown

@ -87,6 +87,10 @@ class Lexer {
$clearBuffer(); $clearBuffer();
array_push($tokens, new Token(TokenType::BACKSLASH, $char)); array_push($tokens, new Token(TokenType::BACKSLASH, $char));
break; break;
case '>':
$clearBuffer();
array_push($tokens, new Token(TokenType::GT, $char));
break;
case ':': case ':':
if (str_ends_with($buffer, "http") || str_ends_with($buffer, "https")) { if (str_ends_with($buffer, "http") || str_ends_with($buffer, "https")) {
$buffer .= $char; $buffer .= $char;

@ -48,6 +48,31 @@ class Parser {
return stripslashes($text); return stripslashes($text);
} }
private function resolveReferences(DOMElement $node) : void {
if (count($this->references) < 1)
return;
if ($node->hasAttribute("href")) {
$href = $node->getAttribute("href");
if (substr($href, 0, 1) === self::MAGIC_CHAR) {
$index = substr($href, 1, strlen($href) - 2);
if (array_key_exists($index, $this->references))
$node->setAttribute("href", $this->references[$index]);
}
}
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $child) {
if ($child->nodeType === XML_ELEMENT_NODE)
$this->resolveReferences($child);
}
}
}
public function debug() : void {
echo "<pre>";
print_r($this->tokenStream);
echo "</pre>";
}
// PARSING // PARSING
private function parseBold() : DOMNode { private function parseBold() : DOMNode {
@ -405,30 +430,28 @@ class Parser {
$this->document->appendChild($elm); $this->document->appendChild($elm);
} }
private function resolveReferences(DOMElement $node) : void { private function parseBlockQuote() : void {
if (count($this->references) < 1) if (!str_starts_with($this->next()->data, " ")) {
$this->buildParagraph($this->parseText());
return; return;
if ($node->hasAttribute("href")) {
$href = $node->getAttribute("href");
if (substr($href, 0, 1) === self::MAGIC_CHAR) {
$index = substr($href, 1, strlen($href) - 2);
if (array_key_exists($index, $this->references))
$node->setAttribute("href", $this->references[$index]);
}
} }
if ($node->hasChildNodes()) { $buffer = "";
foreach ($node->childNodes as $child) { $elm = $this->document->createElement("blockquote", $buffer);
if ($child->nodeType === XML_ELEMENT_NODE) while (!($this->current()->type === TokenType::EOL && $this->next()->type !== TokenType::GT)) {
$this->resolveReferences($child); $gt = $this->consume();
if ($this->current()->type === TokenType::EOL) {
$this->consume();
$line = $this->document->createTextNode($buffer);
$br = $this->document->createElement("br");
$buffer = "";
$elm->appendChild($line);
$elm->appendChild($br);
continue;
} }
$buffer .= $this->current()->data;
} }
}
public function debug() : void { $this->document->appendChild($elm);
echo "<pre>";
print_r($this->tokenStream);
echo "</pre>";
} }
public function parse() : DOMDocument { public function parse() : DOMDocument {
@ -455,6 +478,9 @@ class Parser {
case TokenType::PIPE: case TokenType::PIPE:
$this->parseTable(); $this->parseTable();
break; break;
case TokenType::GT:
$this->parseBlockQuote();
break;
case TokenType::TEXT: case TokenType::TEXT:
default: default:
$this->buildParagraph($this->parseText(true)); $this->buildParagraph($this->parseText(true));

@ -20,6 +20,7 @@ enum TokenType {
case COLON ; case COLON ;
case BACKSLASH; case BACKSLASH;
case PIPE ; case PIPE ;
case GT ;
} }
class Token { class Token {

Loading…
Cancel
Save