mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
State management
Decouple state from final rendering Restore initial state after parsing
This commit is contained in:
parent
c9388cb5c2
commit
083ad582c7
@ -31,6 +31,7 @@ use Erusev\Parsedown\Components\Inlines\Strikethrough;
|
|||||||
use Erusev\Parsedown\Components\Inlines\Url;
|
use Erusev\Parsedown\Components\Inlines\Url;
|
||||||
use Erusev\Parsedown\Components\Inlines\UrlTag;
|
use Erusev\Parsedown\Components\Inlines\UrlTag;
|
||||||
use Erusev\Parsedown\Components\StateUpdatingBlock;
|
use Erusev\Parsedown\Components\StateUpdatingBlock;
|
||||||
|
use Erusev\Parsedown\Html\Renderable;
|
||||||
use Erusev\Parsedown\Html\Renderables\Invisible;
|
use Erusev\Parsedown\Html\Renderables\Invisible;
|
||||||
use Erusev\Parsedown\Html\Renderables\Text;
|
use Erusev\Parsedown\Html\Renderables\Text;
|
||||||
use Erusev\Parsedown\Parsing\Context;
|
use Erusev\Parsedown\Parsing\Context;
|
||||||
@ -60,25 +61,20 @@ final class Parsedown
|
|||||||
*/
|
*/
|
||||||
public function text($text)
|
public function text($text)
|
||||||
{
|
{
|
||||||
$StateRenderables = $this->textElements($text);
|
$InitialState = $this->State;
|
||||||
|
|
||||||
# convert to markup
|
$StateRenderables = $this->lines(Lines::fromTextLines($text, 0));
|
||||||
$markup = $this->elements($this->State, $StateRenderables);
|
|
||||||
|
$Renderables = $this->State->applyTo($StateRenderables);
|
||||||
|
|
||||||
|
$this->State = $InitialState;
|
||||||
|
|
||||||
|
$html = self::render($Renderables);
|
||||||
|
|
||||||
# trim line breaks
|
# trim line breaks
|
||||||
$markup = \trim($markup, "\n");
|
$html = \trim($html, "\n");
|
||||||
|
|
||||||
return $markup;
|
return $html;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $text
|
|
||||||
* @return StateRenderable[]
|
|
||||||
*/
|
|
||||||
protected function textElements($text)
|
|
||||||
{
|
|
||||||
# iterate through lines to identify blocks
|
|
||||||
return $this->lines(Lines::fromTextLines($text, 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -326,20 +322,18 @@ final class Parsedown
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param State $State
|
* @param Renderable[] $Renderables
|
||||||
* @param StateRenderable[] $StateRenderables
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function elements(State $State, array $StateRenderables)
|
public static function render(array $Renderables)
|
||||||
{
|
{
|
||||||
return \array_reduce(
|
return \array_reduce(
|
||||||
$StateRenderables,
|
$Renderables,
|
||||||
/**
|
/**
|
||||||
* @param string $html
|
* @param string $html
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function ($html, StateRenderable $StateRenderable) use ($State) {
|
function ($html, Renderable $Renderable) {
|
||||||
$Renderable = $StateRenderable->renderable($State);
|
|
||||||
return (
|
return (
|
||||||
$html
|
$html
|
||||||
. ($Renderable instanceof Invisible ? '' : "\n")
|
. ($Renderable instanceof Invisible ? '' : "\n")
|
||||||
|
Loading…
Reference in New Issue
Block a user