1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Implement Markup

This commit is contained in:
Aidan Woods 2019-01-20 02:29:05 +00:00
parent edc004f503
commit ee094cb397
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 89 additions and 39 deletions

View File

@ -0,0 +1,89 @@
<?php
namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\ContinuableBlock;
use Erusev\Parsedown\Configurables\SafeMode;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\RawHtml;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class Markup implements ContinuableBlock
{
use ContinuableBlockDefaultInterrupt, BlockAcquisition;
const REGEX_HTML_ATTRIBUTE = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+';
/** @var string */
private $html;
/**
* @param string $html
*/
public function __construct($html)
{
$this->html = $html;
}
/**
* @param Context $Context
* @param Block|null $Block
* @param State|null $State
* @return static|null
*/
public static function build(
Context $Context,
Block $Block = null,
State $State = null
) {
if (\preg_match('/^<[\/]?+(\w*)(?:[ ]*+'.self::REGEX_HTML_ATTRIBUTE.')*+[ ]*+(\/)?>/', $Context->line()->text(), $matches)) {
$element = \strtolower($matches[1]);
if (\array_key_exists($element, Element::TEXT_LEVEL_ELEMENTS)) {
return null;
}
return new self($Context->line()->text());
}
}
/**
* @param Context $Context
* @return self|null
*/
public function continue(Context $Context)
{
if ($this->interrupted) {
return null;
}
$html = $this->html . "\n" . $Context->line()->rawLine();
return new self($html);
}
/**
* @return Handler<Element|RawHtml>
*/
public function stateRenderable(Parsedown $Parsedown)
{
return new Handler(
/** @return Element|RawHtml */
function (State $State) {
$SafeMode = $State->getOrDefault(SafeMode::class);
if ($SafeMode->enabled()) {
return new Element('p', [], [new Text($this->html)]);
} else {
return new RawHtml($this->html);
}
}
);
}
}

View File

@ -334,45 +334,6 @@ class Parsedown
return $Block; return $Block;
} }
#
# Markup
protected function blockMarkup(Context $Context)
{
if ($this->markupEscaped or $this->safeMode) {
return;
}
if (\preg_match('/^<[\/]?+(\w*)(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+(\/)?>/', $Context->line()->text(), $matches)) {
$element = \strtolower($matches[1]);
if (\in_array($element, $this->textLevelElements, true)) {
return;
}
$Block = [
'name' => $matches[1],
'element' => [
'rawHtml' => $Context->line()->text(),
'autobreak' => true,
],
];
return $Block;
}
}
protected function blockMarkupContinue(Context $Context, array $Block)
{
if (isset($Block['closed']) or $Context->previousEmptyLines() > 0) {
return;
}
$Block['element']['rawHtml'] .= "\n" . $Context->line()->rawLine();
return $Block;
}
# #
# Reference # Reference