1
0
mirror of https://github.com/erusev/parsedown.git synced 2025-09-08 05:40:59 +02:00

Return state after block parse instead of mutating the instance copy

This commit is contained in:
Aidan Woods
2019-01-26 20:37:02 +00:00
parent b728f254b7
commit d6f526d80f
11 changed files with 47 additions and 36 deletions

View File

@@ -102,11 +102,14 @@ final class BlockQuote implements ContinuableBlock
return new Handler( return new Handler(
/** @return Element */ /** @return Element */
function (State $State) { function (State $State) {
return new Element( list($StateRenderables, $State) = Parsedown::lines(
'blockquote', $this->Lines,
[], $State
$State->applyTo((new Parsedown($State))->lines($this->Lines))
); );
$Renderables = $State->applyTo($StateRenderables);
return new Element('blockquote', [], $Renderables);
} }
); );
} }

View File

@@ -84,7 +84,7 @@ final class Header implements Block
return new Element( return new Element(
'h' . \strval($this->level), 'h' . \strval($this->level),
[], [],
$State->applyTo((new Parsedown($State))->line($this->text)) $State->applyTo(Parsedown::line($this->text, $State))
); );
} }
); );

View File

@@ -70,7 +70,7 @@ final class Paragraph implements ContinuableBlock
return new Element( return new Element(
'p', 'p',
[], [],
$State->applyTo((new Parsedown($State))->line($this->text)) $State->applyTo(Parsedown::line($this->text, $State))
); );
} }
); );

View File

@@ -66,7 +66,7 @@ final class SetextHeader implements Block
return new Element( return new Element(
'h' . \strval($this->level), 'h' . \strval($this->level),
[], [],
$State->applyTo((new Parsedown($State))->line($this->text)) $State->applyTo(Parsedown::line($this->text, $State))
); );
} }
); );

View File

@@ -295,7 +295,12 @@ final class TList implements ContinuableBlock
$Lines = $Lines->appendingBlankLines(1); $Lines = $Lines->appendingBlankLines(1);
} }
$Renderables = $State->applyTo((new Parsedown($State))->lines($Lines)); list($StateRenderables, $State) = Parsedown::lines(
$Lines,
$State
);
$Renderables = $State->applyTo($StateRenderables);
if (! $Lines->containsBlankLines() if (! $Lines->containsBlankLines()
&& isset($Renderables[0]) && isset($Renderables[0])

View File

@@ -181,7 +181,7 @@ final class Table implements ContinuableBlock
return new Element( return new Element(
'th', 'th',
isset($alignment) ? ['style' => "text-align: $alignment;"] : [], isset($alignment) ? ['style' => "text-align: $alignment;"] : [],
$State->applyTo((new Parsedown($State))->line($cell)) $State->applyTo(Parsedown::line($cell, $State))
); );
}, },
$this->headerCells, $this->headerCells,
@@ -203,7 +203,7 @@ final class Table implements ContinuableBlock
return new Element( return new Element(
'td', 'td',
isset($alignment) ? ['style' => "text-align: $alignment;"] : [], isset($alignment) ? ['style' => "text-align: $alignment;"] : [],
$State->applyTo((new Parsedown($State))->line($cell)) $State->applyTo(Parsedown::line($cell, $State))
); );
}, },
$cells, $cells,

View File

@@ -80,7 +80,7 @@ final class Emphasis implements Inline
return new Element( return new Element(
$this->type, $this->type,
[], [],
$State->applyTo((new Parsedown($State))->line($this->text)) $State->applyTo(Parsedown::line($this->text, $State))
); );
} }
); );

View File

@@ -62,7 +62,7 @@ final class Image implements Inline
$attributes = [ $attributes = [
'src' => $this->Link->url(), 'src' => $this->Link->url(),
'alt' => \array_reduce( 'alt' => \array_reduce(
(new Parsedown($State))->inlines($this->Link->label()), Parsedown::inlines($this->Link->label(), $State),
/** /**
* @param string $text * @param string $text
* @return string * @return string

View File

@@ -134,7 +134,7 @@ final class Link implements Inline
return new Element( return new Element(
'a', 'a',
$attributes, $attributes,
$State->applyTo((new Parsedown($State))->line($this->label)) $State->applyTo(Parsedown::line($this->label, $State))
); );
} }
); );

View File

@@ -59,7 +59,7 @@ final class Strikethrough implements Inline
return new Element( return new Element(
'del', 'del',
[], [],
$State->applyTo((new Parsedown($State))->line($this->text)) $State->applyTo(Parsedown::line($this->text, $State))
); );
} }
); );

View File

@@ -43,13 +43,12 @@ final class Parsedown
*/ */
public function text($text) public function text($text)
{ {
$InitialState = $this->State; list($StateRenderables, $State) = self::lines(
Lines::fromTextLines($text, 0),
$this->State
);
$StateRenderables = $this->lines(Lines::fromTextLines($text, 0)); $Renderables = $State->applyTo($StateRenderables);
$Renderables = $this->State->applyTo($StateRenderables);
$this->State = $InitialState;
$html = self::render($Renderables); $html = self::render($Renderables);
@@ -60,21 +59,25 @@ final class Parsedown
} }
/** /**
* @return StateRenderable[] * @return array{0: StateRenderable[], 1: State}
*/ */
public function lines(Lines $Lines) public static function lines(Lines $Lines, State $State)
{ {
return \array_map( list($Blocks, $State) = self::blocks($Lines, $State);
$StateRenderables = \array_map(
/** @return StateRenderable */ /** @return StateRenderable */
function (Block $Block) { return $Block->stateRenderable(); }, function (Block $Block) { return $Block->stateRenderable(); },
$this->blocks($Lines) $Blocks
); );
return [$StateRenderables, $State];
} }
/** /**
* @return Block[] * @return array{0: Block[], 1: State}
*/ */
public function blocks(Lines $Lines) public static function blocks(Lines $Lines, State $State)
{ {
/** @var Block[] */ /** @var Block[] */
$Blocks = []; $Blocks = [];
@@ -103,16 +106,16 @@ final class Parsedown
$marker = $Line->text()[0]; $marker = $Line->text()[0];
$potentialBlockTypes = \array_merge( $potentialBlockTypes = \array_merge(
$this->State->get(BlockTypes::class)->unmarked(), $State->get(BlockTypes::class)->unmarked(),
$this->State->get(BlockTypes::class)->markedBy($marker) $State->get(BlockTypes::class)->markedBy($marker)
); );
foreach ($potentialBlockTypes as $blockType) { foreach ($potentialBlockTypes as $blockType) {
$Block = $blockType::build($Context, $CurrentBlock, $this->State); $Block = $blockType::build($Context, $CurrentBlock, $State);
if (isset($Block)) { if (isset($Block)) {
if ($Block instanceof StateUpdatingBlock) { if ($Block instanceof StateUpdatingBlock) {
$this->State = $Block->latestState(); $State = $Block->latestState();
} }
if (isset($CurrentBlock) && ! $Block->acquiredPrevious()) { if (isset($CurrentBlock) && ! $Block->acquiredPrevious()) {
@@ -144,19 +147,19 @@ final class Parsedown
$Blocks[] = $CurrentBlock; $Blocks[] = $CurrentBlock;
} }
return $Blocks; return [$Blocks, $State];
} }
/** /**
* @param string $text * @param string $text
* @return StateRenderable[] * @return StateRenderable[]
*/ */
public function line($text) public static function line($text, State $State)
{ {
return \array_map( return \array_map(
/** @return StateRenderable */ /** @return StateRenderable */
function (Inline $Inline) { return $Inline->stateRenderable(); }, function (Inline $Inline) { return $Inline->stateRenderable(); },
$this->inlines($text) self::inlines($text, $State)
); );
} }
@@ -164,7 +167,7 @@ final class Parsedown
* @param string $text * @param string $text
* @return Inline[] * @return Inline[]
*/ */
public function inlines($text) public static function inlines($text, State $State)
{ {
# standardize line breaks # standardize line breaks
$text = \str_replace(["\r\n", "\r"], "\n", $text); $text = \str_replace(["\r\n", "\r"], "\n", $text);
@@ -174,7 +177,7 @@ final class Parsedown
# $excerpt is based on the first occurrence of a marker # $excerpt is based on the first occurrence of a marker
$InlineTypes = $this->State->get(InlineTypes::class); $InlineTypes = $State->get(InlineTypes::class);
$markerMask = $InlineTypes->markers(); $markerMask = $InlineTypes->markers();
for ( for (
@@ -187,7 +190,7 @@ final class Parsedown
foreach ($InlineTypes->markedBy($marker) as $inlineType) { foreach ($InlineTypes->markedBy($marker) as $inlineType) {
# check to see if the current inline type is nestable in the current context # check to see if the current inline type is nestable in the current context
$Inline = $inlineType::build($Excerpt, $this->State); $Inline = $inlineType::build($Excerpt, $State);
if (! isset($Inline)) { if (! isset($Inline)) {
continue; continue;