From 576a2c4519a9b2625ac3c3cfc2f3a0b12c15615c Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Tue, 22 Jan 2019 19:04:45 +0000 Subject: [PATCH] Generalise line parsing to return Inlines before applying state --- src/Parsedown.php | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Parsedown.php b/src/Parsedown.php index fd3de49..ca1039f 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -245,12 +245,25 @@ final class Parsedown * @return StateRenderable[] */ public function line($text) + { + return \array_map( + /** @return StateRenderable */ + function (Inline $Inline) { return $Inline->stateRenderable($this); }, + $this->inlines($text) + ); + } + + /** + * @param string $text + * @return Inline[] + */ + public function inlines($text) { # standardize line breaks $text = \str_replace(["\r\n", "\r"], "\n", $text); - /** @var StateRenderable[] */ - $StateRenderables = []; + /** @var Inline[] */ + $Inlines = []; # $excerpt is based on the first occurrence of a marker @@ -285,12 +298,10 @@ final class Parsedown # the text that comes before the inline # compile the unmarked text - $StateRenderables[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition)) - ->stateRenderable($this) - ; + $Inlines[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition)); # compile the inline - $StateRenderables[] = $Inline->stateRenderable($this); + $Inlines[] = $Inline; # remove the examined text /** @psalm-suppress LoopInvalidation */ @@ -305,20 +316,16 @@ final class Parsedown # the marker does not belong to an inline - $StateRenderables[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition + 1)) - ->stateRenderable($this) - ; + $Inlines[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition + 1)); $text = \substr($Excerpt->text(), $startPosition + 1); /** @psalm-suppress LoopInvalidation */ $Excerpt = $Excerpt->choppingFromOffset($startPosition + 1); } - $StateRenderables[] = Plaintext::build($Excerpt->choppingFromOffset(0)) - ->stateRenderable($this) - ; + $Inlines[] = Plaintext::build($Excerpt->choppingFromOffset(0)); - return $StateRenderables; + return $Inlines; } /**