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)*
* tables *(with alignment specification)*
* paragraphs
* block quotes
### Supported inline types
Parkdown currently support the following block types:
* bold text (`**bold**`)
* bold text (`**bold**`)
* italic text (`*italic*`)
* code snippets
* images (`![alt text](src url)`)
@ -26,7 +27,6 @@ Parkdown currently support the following block types:
* references (`[marker]: URL`)
## Examples
### Paragraphs
```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)*
@ -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)
### 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
```markdown

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

@ -48,6 +48,31 @@ class Parser {
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
private function parseBold() : DOMNode {
@ -405,30 +430,28 @@ class Parser {
$this->document->appendChild($elm);
}
private function resolveReferences(DOMElement $node) : void {
if (count($this->references) < 1)
private function parseBlockQuote() : void {
if (!str_starts_with($this->next()->data, " ")) {
$this->buildParagraph($this->parseText());
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);
$buffer = "";
$elm = $this->document->createElement("blockquote", $buffer);
while (!($this->current()->type === TokenType::EOL && $this->next()->type !== TokenType::GT)) {
$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 {
echo "<pre>";
print_r($this->tokenStream);
echo "</pre>";
$this->document->appendChild($elm);
}
public function parse() : DOMDocument {
@ -455,6 +478,9 @@ class Parser {
case TokenType::PIPE:
$this->parseTable();
break;
case TokenType::GT:
$this->parseBlockQuote();
break;
case TokenType::TEXT:
default:
$this->buildParagraph($this->parseText(true));

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

Loading…
Cancel
Save