mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement plaintext
This commit is contained in:
parent
25cf5a1729
commit
760945008b
75
src/Components/Inlines/PlainText.php
Normal file
75
src/Components/Inlines/PlainText.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Erusev\Parsedown\Components\Inlines;
|
||||
|
||||
use Erusev\Parsedown\AST\Handler;
|
||||
use Erusev\Parsedown\AST\StateRenderable;
|
||||
use Erusev\Parsedown\Components\Inline;
|
||||
use Erusev\Parsedown\Html\Renderables\Container;
|
||||
use Erusev\Parsedown\Html\Renderables\Element;
|
||||
use Erusev\Parsedown\Html\Renderables\Text;
|
||||
use Erusev\Parsedown\Parsedown;
|
||||
use Erusev\Parsedown\Parsing\Excerpt;
|
||||
use Erusev\Parsedown\State;
|
||||
|
||||
final class PlainText implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
*/
|
||||
public function __construct($text)
|
||||
{
|
||||
$this->text = $text;
|
||||
$this->width = \strlen($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Excerpt $Excerpt
|
||||
* @param State $State
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Excerpt $Excerpt, State $State = null)
|
||||
{
|
||||
return new self($Excerpt->text());
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function text()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Container>
|
||||
*/
|
||||
public function stateRenderable(Parsedown $_)
|
||||
{
|
||||
return new Handler(
|
||||
/** @return Container */
|
||||
function (State $_) {
|
||||
$Renderables = [];
|
||||
$text = $this->text;
|
||||
|
||||
while (\preg_match('/(?:[ ]*+\\\\|[ ]{2,}+)\n/', $text, $matches, \PREG_OFFSET_CAPTURE)) {
|
||||
$offset = \intval($matches[0][1]);
|
||||
$before = \substr($text, 0, $offset);
|
||||
$after = \substr($text, $offset + \strlen($matches[0][0]));
|
||||
$Renderables[] = new Text($before);
|
||||
$Renderables[] = Element::selfClosing('br', []);
|
||||
$Renderables[] = new Text("\n");
|
||||
|
||||
$text = $after;
|
||||
}
|
||||
|
||||
$Renderables[] = new Text($text);
|
||||
|
||||
return new Container($Renderables);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -411,29 +411,6 @@ class Parsedown
|
||||
return $Elements;
|
||||
}
|
||||
|
||||
#
|
||||
# ~
|
||||
#
|
||||
|
||||
protected function inlineText($text)
|
||||
{
|
||||
$Inline = [
|
||||
'extent' => \strlen($text),
|
||||
'element' => [],
|
||||
];
|
||||
|
||||
$Inline['element']['elements'] = self::pregReplaceElements(
|
||||
$this->breaksEnabled ? '/[ ]*+\n/' : '/(?:[ ]*+\\\\|[ ]{2,}+)\n/',
|
||||
[
|
||||
['name' => 'br'],
|
||||
['text' => "\n"],
|
||||
],
|
||||
$text
|
||||
);
|
||||
|
||||
return $Inline;
|
||||
}
|
||||
|
||||
protected function inlineCode($Excerpt)
|
||||
{
|
||||
$marker = $Excerpt['text'][0];
|
||||
|
Loading…
Reference in New Issue
Block a user