diff --git a/src/Components/Inlines/Emphasis.php b/src/Components/Inlines/Emphasis.php new file mode 100644 index 0000000..bf8a2ab --- /dev/null +++ b/src/Components/Inlines/Emphasis.php @@ -0,0 +1,86 @@ + '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s', + '_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us', + ]; + + const EM_REGEX = [ + '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', + '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', + ]; + + /** + * @param string $text + * @param 'em'|'strong' $type + * @param int $width + */ + public function __construct($text, $type, $width) + { + $this->text = $text; + $this->type = $type; + $this->width = $width; + } + + /** + * @param Excerpt $Excerpt + * @param State $State + * @return static|null + */ + public static function build(Excerpt $Excerpt, State $State) + { + if (! isset($Excerpt->text()[1])) { + return null; + } + + $marker = $Excerpt->text()[0] === '*' ? '*' : '_'; + + if ($Excerpt->text()[1] === $marker and \preg_match(self::STRONG_REGEX[$marker], $Excerpt->text(), $matches)) { + $emphasis = 'strong'; + } elseif (\preg_match(self::EM_REGEX[$marker], $Excerpt->text(), $matches)) { + $emphasis = 'em'; + } else { + return null; + } + + return new self($matches[1], $emphasis, \strlen($matches[0])); + } + + /** + * @return Handler + */ + public function stateRenderable(Parsedown $Parsedown) + { + return new Handler( + /** @return Element */ + function (State $State) use ($Parsedown) { + return new Element( + $this->type, + [], + $State->applyTo($Parsedown->lineElements($this->text)) + ); + } + ); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index c96c4d0..55c130c 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -411,35 +411,6 @@ class Parsedown return $Elements; } - protected function inlineEmphasis($Excerpt) - { - if (! isset($Excerpt['text'][1])) { - return; - } - - $marker = $Excerpt['text'][0]; - - if ($Excerpt['text'][1] === $marker and \preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) { - $emphasis = 'strong'; - } elseif (\preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches)) { - $emphasis = 'em'; - } else { - return; - } - - return [ - 'extent' => \strlen($matches[0]), - 'element' => [ - 'name' => $emphasis, - 'handler' => [ - 'function' => 'lineElements', - 'argument' => $matches[1], - 'destination' => 'elements', - ] - ], - ]; - } - protected function inlineEscapeSequence($Excerpt) { if (isset($Excerpt['text'][1]) and \in_array($Excerpt['text'][1], $this->specialCharacters, true)) {