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:36:11 +00:00
parent 5e8905c455
commit 778eacd081
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 77 additions and 28 deletions

View 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);
}
}
);
}
}

View File

@ -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