mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement Markup
This commit is contained in:
parent
edc004f503
commit
ee094cb397
89
src/Components/Blocks/Markup.php
Normal file
89
src/Components/Blocks/Markup.php
Normal 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);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -334,45 +334,6 @@ class Parsedown
|
||||
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user