1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Implement unmarked text via AST

This commit is contained in:
Aidan Woods 2018-03-18 22:37:40 +00:00
parent 65d7bc5013
commit adcba80502
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9

View File

@ -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/', "<br />\n", $text);
}
else
{
$text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\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
#