From 778eacd08110bb669c99c933abe16958194bbfc0 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 20 Jan 2019 02:36:11 +0000 Subject: [PATCH] Implement Markup --- src/Components/Inlines/Markup.php | 77 +++++++++++++++++++++++++++++++ src/Parsedown.php | 28 ----------- 2 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 src/Components/Inlines/Markup.php diff --git a/src/Components/Inlines/Markup.php b/src/Components/Inlines/Markup.php new file mode 100644 index 0000000..6b82da3 --- /dev/null +++ b/src/Components/Inlines/Markup.php @@ -0,0 +1,77 @@ +`\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 + */ + 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); + } + } + ); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index 656a677..63b91ce 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -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