|
|
|
@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
|
|
namespace parkdown; |
|
|
|
|
|
|
|
|
|
use Attribute; |
|
|
|
|
use DOMDocument; |
|
|
|
|
use DOMElement; |
|
|
|
|
use DOMNode; |
|
|
|
@ -11,6 +12,11 @@ enum ListType { |
|
|
|
|
case UNORDERED; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Attributes { |
|
|
|
|
public array $classes = []; |
|
|
|
|
public ?string $id = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Parser { |
|
|
|
|
const MAGIC_CHAR = "*"; |
|
|
|
|
|
|
|
|
@ -228,6 +234,41 @@ class Parser { |
|
|
|
|
$clearBuffer(); |
|
|
|
|
array_push($elms, $elm); |
|
|
|
|
continue; |
|
|
|
|
} elseif ($this->current()->type === TokenType::LBRACE) { |
|
|
|
|
$lbrace = $this->consume(); |
|
|
|
|
assert($lbrace->type === TokenType::LBRACE, "expected left brace, got ".$lbrace->type->name); |
|
|
|
|
|
|
|
|
|
$content = ""; |
|
|
|
|
while ($this->current()->type !== TokenType::EOF && |
|
|
|
|
$this->current()->type !== TokenType::EOL && |
|
|
|
|
$this->current()->type !== TokenType::RBRACE) { |
|
|
|
|
$content .= $this->consume()->data; |
|
|
|
|
} |
|
|
|
|
$rbrace = $this->consume(); |
|
|
|
|
assert($rbrace->type === TokenType::RBRACE, "expected right brace, got ".$rbrace->type->name); |
|
|
|
|
|
|
|
|
|
$attributes = array_map(function($element) { |
|
|
|
|
return trim($element); |
|
|
|
|
}, explode(',', $content)); |
|
|
|
|
|
|
|
|
|
$obj = new Attributes(); |
|
|
|
|
|
|
|
|
|
foreach($attributes as $attribute) { |
|
|
|
|
if (!in_array($attribute[0], [".", "#"])) |
|
|
|
|
continue; |
|
|
|
|
switch ($attribute[0]) { |
|
|
|
|
case ".": |
|
|
|
|
array_push($obj->classes, substr($attribute, 1)); |
|
|
|
|
break; |
|
|
|
|
case "#": |
|
|
|
|
$obj->id = substr($attribute, 1); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
continue 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
array_push($elms, $obj); |
|
|
|
|
} else |
|
|
|
|
$buffer .= self::StripBackslashes($this->consume()->data); |
|
|
|
|
} |
|
|
|
@ -307,6 +348,14 @@ class Parser { |
|
|
|
|
$elm = $this->document->createElement("p"); |
|
|
|
|
$i = 0; |
|
|
|
|
foreach ($elms as $node) { |
|
|
|
|
if ($node instanceof Attributes) { |
|
|
|
|
if (count($node->classes) > 0) |
|
|
|
|
$elm->setAttribute("class", join(" ", $node->classes)); |
|
|
|
|
if ($node->id) |
|
|
|
|
$elm->setAttribute("id", $node->id); |
|
|
|
|
|
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if ($node->nodeName === "#text" && trim($node->textContent) === "") |
|
|
|
|
continue; |
|
|
|
|
$elm->appendChild($node); |
|
|
|
|