1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Implement SpecialCharacter

This commit is contained in:
Aidan Woods 2019-01-20 02:36:32 +00:00
parent 778eacd081
commit f256352f53
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 54 additions and 14 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Inline;
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 SpecialCharacter implements Inline
{
use WidthTrait, DefaultBeginPosition;
/** @var string */
private $charCodeHtml;
/**
* @param string $charCodeHtml
*/
public function __construct($charCodeHtml)
{
$this->charCodeHtml = $charCodeHtml;
$this->width = \strlen($charCodeHtml) + 2;
}
/**
* @param Excerpt $Excerpt
* @param State $State
* @return static|null
*/
public static function build(Excerpt $Excerpt, State $State)
{
if (\substr($Excerpt->text(), 1, 1) !== ' ' and \strpos($Excerpt->text(), ';') !== false
and \preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt->text(), $matches)
) {
return new self($matches[1]);
}
return null;
}
/**
* @return RawHtml
*/
public function stateRenderable(Parsedown $_)
{
return new RawHtml(
'&' . (new Text($this->charCodeHtml))->getHtml() . ';'
);
}
}

View File

@ -411,20 +411,6 @@ class Parsedown
return $Elements;
}
protected function inlineSpecialCharacter($Excerpt)
{
if (\substr($Excerpt['text'], 1, 1) !== ' ' and \strpos($Excerpt['text'], ';') !== false
and \preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches)
) {
return [
'element' => ['rawHtml' => '&' . $matches[1] . ';'],
'extent' => \strlen($matches[0]),
];
}
return;
}
protected function inlineStrikethrough($Excerpt)
{
if (! isset($Excerpt['text'][1])) {