From adcba805022d380e99636b66ae882f40eb9baaef Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 18 Mar 2018 22:37:40 +0000 Subject: [PATCH] Implement unmarked text via AST --- Parsedown.php | 80 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 715deee..79e3ba6 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1070,6 +1070,41 @@ class Parsedown # ~ # + protected function inlineText($text) + { + $Inline = array( + 'extent' => strlen($text), + 'element' => array( + 'handler' => 'elements', + ), + ); + + if ($this->breaksEnabled) + { + $Inline['element']['text'] = self::pregReplaceElements( + '/[ ]*\n/', + array( + array('name' => 'br'), + array('text' => "\n"), + ), + $text + ); + } + else + { + $Inline['element']['text'] = self::pregReplaceElements( + '/(?:[ ][ ]+|[ ]*\\\\)\n/', + array( + array('name' => 'br'), + array('text' => "\n"), + ), + $text + ); + } + + return $Inline; + } + protected function inlineCode($Excerpt) { $marker = $Excerpt['text'][0]; @@ -1391,17 +1426,7 @@ class Parsedown protected function unmarkedText($text) { - if ($this->breaksEnabled) - { - $text = preg_replace('/[ ]*\n/', "
\n", $text); - } - else - { - $text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "
\n", $text); - $text = str_replace(" \n", "\n", $text); - } - - return $text; + return $this->element($this->inlineText($text)['element']); } # @@ -1526,6 +1551,39 @@ class Parsedown return $markup; } + # + # AST Convenience + # + + /** + * Replace occurrences $regexp with $Elements in $text. Return an array of + * elements representing the replacement. + */ + protected static function pregReplaceElements($regexp, $Elements, $text) + { + $newElements = array(); + + while (preg_match($regexp, $text, $matches, PREG_OFFSET_CAPTURE)) + { + $offset = $matches[0][1]; + $before = substr($text, 0, $offset); + $after = substr($text, $offset + strlen($matches[0][0])); + + $newElements[] = array('text' => $before); + + foreach ($Elements as $Element) + { + $newElements[] = $Element; + } + + $text = $after; + } + + $newElements[] = array('text' => $text); + + return $newElements; + } + # # Deprecated Methods #