mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement SpecialCharacter
This commit is contained in:
parent
778eacd081
commit
f256352f53
54
src/Components/Inlines/SpecialCharacter.php
Normal file
54
src/Components/Inlines/SpecialCharacter.php
Normal 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() . ';'
|
||||
);
|
||||
}
|
||||
}
|
@ -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])) {
|
||||
|
Loading…
Reference in New Issue
Block a user