mirror of
https://github.com/erusev/parsedown.git
synced 2025-09-03 20:02:33 +02:00
Return state after block parse instead of mutating the instance copy
This commit is contained in:
@@ -102,11 +102,14 @@ final class BlockQuote implements ContinuableBlock
|
||||
return new Handler(
|
||||
/** @return Element */
|
||||
function (State $State) {
|
||||
return new Element(
|
||||
'blockquote',
|
||||
[],
|
||||
$State->applyTo((new Parsedown($State))->lines($this->Lines))
|
||||
list($StateRenderables, $State) = Parsedown::lines(
|
||||
$this->Lines,
|
||||
$State
|
||||
);
|
||||
|
||||
$Renderables = $State->applyTo($StateRenderables);
|
||||
|
||||
return new Element('blockquote', [], $Renderables);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@@ -84,7 +84,7 @@ final class Header implements Block
|
||||
return new Element(
|
||||
'h' . \strval($this->level),
|
||||
[],
|
||||
$State->applyTo((new Parsedown($State))->line($this->text))
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
@@ -70,7 +70,7 @@ final class Paragraph implements ContinuableBlock
|
||||
return new Element(
|
||||
'p',
|
||||
[],
|
||||
$State->applyTo((new Parsedown($State))->line($this->text))
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
@@ -66,7 +66,7 @@ final class SetextHeader implements Block
|
||||
return new Element(
|
||||
'h' . \strval($this->level),
|
||||
[],
|
||||
$State->applyTo((new Parsedown($State))->line($this->text))
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
@@ -295,7 +295,12 @@ final class TList implements ContinuableBlock
|
||||
$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()
|
||||
&& isset($Renderables[0])
|
||||
|
@@ -181,7 +181,7 @@ final class Table implements ContinuableBlock
|
||||
return new Element(
|
||||
'th',
|
||||
isset($alignment) ? ['style' => "text-align: $alignment;"] : [],
|
||||
$State->applyTo((new Parsedown($State))->line($cell))
|
||||
$State->applyTo(Parsedown::line($cell, $State))
|
||||
);
|
||||
},
|
||||
$this->headerCells,
|
||||
@@ -203,7 +203,7 @@ final class Table implements ContinuableBlock
|
||||
return new Element(
|
||||
'td',
|
||||
isset($alignment) ? ['style' => "text-align: $alignment;"] : [],
|
||||
$State->applyTo((new Parsedown($State))->line($cell))
|
||||
$State->applyTo(Parsedown::line($cell, $State))
|
||||
);
|
||||
},
|
||||
$cells,
|
||||
|
@@ -80,7 +80,7 @@ final class Emphasis implements Inline
|
||||
return new Element(
|
||||
$this->type,
|
||||
[],
|
||||
$State->applyTo((new Parsedown($State))->line($this->text))
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
@@ -62,7 +62,7 @@ final class Image implements Inline
|
||||
$attributes = [
|
||||
'src' => $this->Link->url(),
|
||||
'alt' => \array_reduce(
|
||||
(new Parsedown($State))->inlines($this->Link->label()),
|
||||
Parsedown::inlines($this->Link->label(), $State),
|
||||
/**
|
||||
* @param string $text
|
||||
* @return string
|
||||
|
@@ -134,7 +134,7 @@ final class Link implements Inline
|
||||
return new Element(
|
||||
'a',
|
||||
$attributes,
|
||||
$State->applyTo((new Parsedown($State))->line($this->label))
|
||||
$State->applyTo(Parsedown::line($this->label, $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
@@ -59,7 +59,7 @@ final class Strikethrough implements Inline
|
||||
return new Element(
|
||||
'del',
|
||||
[],
|
||||
$State->applyTo((new Parsedown($State))->line($this->text))
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
@@ -43,13 +43,12 @@ final class Parsedown
|
||||
*/
|
||||
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 = $this->State->applyTo($StateRenderables);
|
||||
|
||||
$this->State = $InitialState;
|
||||
$Renderables = $State->applyTo($StateRenderables);
|
||||
|
||||
$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 */
|
||||
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[] */
|
||||
$Blocks = [];
|
||||
@@ -103,16 +106,16 @@ final class Parsedown
|
||||
$marker = $Line->text()[0];
|
||||
|
||||
$potentialBlockTypes = \array_merge(
|
||||
$this->State->get(BlockTypes::class)->unmarked(),
|
||||
$this->State->get(BlockTypes::class)->markedBy($marker)
|
||||
$State->get(BlockTypes::class)->unmarked(),
|
||||
$State->get(BlockTypes::class)->markedBy($marker)
|
||||
);
|
||||
|
||||
foreach ($potentialBlockTypes as $blockType) {
|
||||
$Block = $blockType::build($Context, $CurrentBlock, $this->State);
|
||||
$Block = $blockType::build($Context, $CurrentBlock, $State);
|
||||
|
||||
if (isset($Block)) {
|
||||
if ($Block instanceof StateUpdatingBlock) {
|
||||
$this->State = $Block->latestState();
|
||||
$State = $Block->latestState();
|
||||
}
|
||||
|
||||
if (isset($CurrentBlock) && ! $Block->acquiredPrevious()) {
|
||||
@@ -144,19 +147,19 @@ final class Parsedown
|
||||
$Blocks[] = $CurrentBlock;
|
||||
}
|
||||
|
||||
return $Blocks;
|
||||
return [$Blocks, $State];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @return StateRenderable[]
|
||||
*/
|
||||
public function line($text)
|
||||
public static function line($text, State $State)
|
||||
{
|
||||
return \array_map(
|
||||
/** @return StateRenderable */
|
||||
function (Inline $Inline) { return $Inline->stateRenderable(); },
|
||||
$this->inlines($text)
|
||||
self::inlines($text, $State)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -164,7 +167,7 @@ final class Parsedown
|
||||
* @param string $text
|
||||
* @return Inline[]
|
||||
*/
|
||||
public function inlines($text)
|
||||
public static function inlines($text, State $State)
|
||||
{
|
||||
# standardize line breaks
|
||||
$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
|
||||
|
||||
$InlineTypes = $this->State->get(InlineTypes::class);
|
||||
$InlineTypes = $State->get(InlineTypes::class);
|
||||
$markerMask = $InlineTypes->markers();
|
||||
|
||||
for (
|
||||
@@ -187,7 +190,7 @@ final class Parsedown
|
||||
foreach ($InlineTypes->markedBy($marker) as $inlineType) {
|
||||
# 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)) {
|
||||
continue;
|
||||
|
Reference in New Issue
Block a user