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
5e8905c455
commit
778eacd081
77
src/Components/Inlines/Markup.php
Normal file
77
src/Components/Inlines/Markup.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Erusev\Parsedown\Components\Inlines;
|
||||
|
||||
use Erusev\Parsedown\AST\Handler;
|
||||
use Erusev\Parsedown\AST\StateRenderable;
|
||||
use Erusev\Parsedown\Components\Inline;
|
||||
use Erusev\Parsedown\Configurables\SafeMode;
|
||||
use Erusev\Parsedown\Html\Renderables\RawHtml;
|
||||
use Erusev\Parsedown\Html\Renderables\Text;
|
||||
use Erusev\Parsedown\Parsedown;
|
||||
use Erusev\Parsedown\Parsing\Excerpt;
|
||||
use Erusev\Parsedown\State;
|
||||
|
||||
final class Markup implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
|
||||
const HTML_ATT_REGEX = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+';
|
||||
|
||||
/** @var string */
|
||||
private $html;
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
*/
|
||||
public function __construct($html)
|
||||
{
|
||||
$this->html = $html;
|
||||
$this->width = \strlen($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Excerpt $Excerpt
|
||||
* @param State $State
|
||||
* @return static|null
|
||||
*/
|
||||
public static function build(Excerpt $Excerpt, State $State)
|
||||
{
|
||||
if (\strpos($Excerpt->text(), '>') === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$secondChar = \substr($Excerpt->text(), 1, 1);
|
||||
|
||||
if ($secondChar === '/' and \preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt->text(), $matches)) {
|
||||
return new self($matches[0]);
|
||||
}
|
||||
|
||||
if ($secondChar === '!' and \preg_match('/^<!---?[^>-](?:-?+[^-])*-->/s', $Excerpt->text(), $matches)) {
|
||||
return new self($matches[0]);
|
||||
}
|
||||
|
||||
if ($secondChar !== ' ' and \preg_match('/^<\w[\w-]*+(?:[ ]*+'.self::HTML_ATT_REGEX.')*+[ ]*+\/?>/s', $Excerpt->text(), $matches)) {
|
||||
return new self($matches[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Text|RawHtml>
|
||||
*/
|
||||
public function stateRenderable(Parsedown $_)
|
||||
{
|
||||
return new Handler(
|
||||
/** @return Text|RawHtml */
|
||||
function (State $State) {
|
||||
$SafeMode = $State->getOrDefault(SafeMode::class);
|
||||
|
||||
if ($SafeMode->enabled()) {
|
||||
return new Text($this->html);
|
||||
} else {
|
||||
return new RawHtml($this->html);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -411,34 +411,6 @@ class Parsedown
|
||||
return $Elements;
|
||||
}
|
||||
|
||||
protected function inlineMarkup($Excerpt)
|
||||
{
|
||||
if ($this->markupEscaped or $this->safeMode or \strpos($Excerpt['text'], '>') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($Excerpt['text'][1] === '/' and \preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt['text'], $matches)) {
|
||||
return [
|
||||
'element' => ['rawHtml' => $matches[0]],
|
||||
'extent' => \strlen($matches[0]),
|
||||
];
|
||||
}
|
||||
|
||||
if ($Excerpt['text'][1] === '!' and \preg_match('/^<!---?[^>-](?:-?+[^-])*-->/s', $Excerpt['text'], $matches)) {
|
||||
return [
|
||||
'element' => ['rawHtml' => $matches[0]],
|
||||
'extent' => \strlen($matches[0]),
|
||||
];
|
||||
}
|
||||
|
||||
if ($Excerpt['text'][1] !== ' ' and \preg_match('/^<\w[\w-]*+(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+\/?>/s', $Excerpt['text'], $matches)) {
|
||||
return [
|
||||
'element' => ['rawHtml' => $matches[0]],
|
||||
'extent' => \strlen($matches[0]),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function inlineSpecialCharacter($Excerpt)
|
||||
{
|
||||
if (\substr($Excerpt['text'], 1, 1) !== ' ' and \strpos($Excerpt['text'], ';') !== false
|
||||
|
Loading…
Reference in New Issue
Block a user