Generalise line parsing to return Inlines before applying state

This commit is contained in:
Aidan Woods 2019-01-22 19:04:45 +00:00
parent 083ad582c7
commit 576a2c4519
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9

View File

@ -245,12 +245,25 @@ final class Parsedown
* @return StateRenderable[] * @return StateRenderable[]
*/ */
public function line($text) 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 # standardize line breaks
$text = \str_replace(["\r\n", "\r"], "\n", $text); $text = \str_replace(["\r\n", "\r"], "\n", $text);
/** @var StateRenderable[] */ /** @var Inline[] */
$StateRenderables = []; $Inlines = [];
# $excerpt is based on the first occurrence of a marker # $excerpt is based on the first occurrence of a marker
@ -285,12 +298,10 @@ final class Parsedown
# the text that comes before the inline # the text that comes before the inline
# compile the unmarked text # compile the unmarked text
$StateRenderables[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition)) $Inlines[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition));
->stateRenderable($this)
;
# compile the inline # compile the inline
$StateRenderables[] = $Inline->stateRenderable($this); $Inlines[] = $Inline;
# remove the examined text # remove the examined text
/** @psalm-suppress LoopInvalidation */ /** @psalm-suppress LoopInvalidation */
@ -305,20 +316,16 @@ final class Parsedown
# the marker does not belong to an inline # the marker does not belong to an inline
$StateRenderables[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition + 1)) $Inlines[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition + 1));
->stateRenderable($this)
;
$text = \substr($Excerpt->text(), $startPosition + 1); $text = \substr($Excerpt->text(), $startPosition + 1);
/** @psalm-suppress LoopInvalidation */ /** @psalm-suppress LoopInvalidation */
$Excerpt = $Excerpt->choppingFromOffset($startPosition + 1); $Excerpt = $Excerpt->choppingFromOffset($startPosition + 1);
} }
$StateRenderables[] = Plaintext::build($Excerpt->choppingFromOffset(0)) $Inlines[] = Plaintext::build($Excerpt->choppingFromOffset(0));
->stateRenderable($this)
;
return $StateRenderables; return $Inlines;
} }
/** /**