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

Seperate handler delegation from AST

This also splits 'text' into 'text', 'elements', and
'element' to hopefully better communicate structure
This commit is contained in:
Aidan Woods 2018-03-21 02:32:01 +00:00
parent eb55e426b9
commit 098f24d189
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
3 changed files with 153 additions and 118 deletions

View File

@ -260,9 +260,13 @@ class Parsedown
# ~ # ~
if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted'])) if (
{ isset($CurrentBlock)
$CurrentBlock['element']['text'] .= "\n".$text; and isset($CurrentBlock['element']['name'])
and $CurrentBlock['element']['name'] === 'p'
and ! isset($CurrentBlock['interrupted'])
) {
$CurrentBlock['element']['elements'] .= "\n".$text;
} }
else else
{ {
@ -333,8 +337,7 @@ class Parsedown
$Block = array( $Block = array(
'element' => array( 'element' => array(
'name' => 'pre', 'name' => 'pre',
'handler' => 'element', 'element' => array(
'text' => array(
'name' => 'code', 'name' => 'code',
'text' => $text, 'text' => $text,
), ),
@ -351,16 +354,16 @@ class Parsedown
{ {
if (isset($Block['interrupted'])) if (isset($Block['interrupted']))
{ {
$Block['element']['text']['text'] .= "\n"; $Block['element']['element']['text'] .= "\n";
unset($Block['interrupted']); unset($Block['interrupted']);
} }
$Block['element']['text']['text'] .= "\n"; $Block['element']['element']['text'] .= "\n";
$text = substr($Line['body'], 4); $text = substr($Line['body'], 4);
$Block['element']['text']['text'] .= $text; $Block['element']['element']['text'] .= $text;
return $Block; return $Block;
} }
@ -368,9 +371,9 @@ class Parsedown
protected function blockCodeComplete($Block) protected function blockCodeComplete($Block)
{ {
$text = $Block['element']['text']['text']; $text = $Block['element']['element']['text'];
$Block['element']['text']['text'] = $text; $Block['element']['element']['text'] = $text;
return $Block; return $Block;
} }
@ -445,8 +448,7 @@ class Parsedown
'char' => $Line['text'][0], 'char' => $Line['text'][0],
'element' => array( 'element' => array(
'name' => 'pre', 'name' => 'pre',
'handler' => 'element', 'element' => $Element,
'text' => $Element,
), ),
); );
@ -463,30 +465,30 @@ class Parsedown
if (isset($Block['interrupted'])) if (isset($Block['interrupted']))
{ {
$Block['element']['text']['text'] .= "\n"; $Block['element']['element']['text'] .= "\n";
unset($Block['interrupted']); unset($Block['interrupted']);
} }
if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text'])) if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
{ {
$Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1); $Block['element']['element']['text'] = substr($Block['element']['element']['text'], 1);
$Block['complete'] = true; $Block['complete'] = true;
return $Block; return $Block;
} }
$Block['element']['text']['text'] .= "\n".$Line['body']; $Block['element']['element']['text'] .= "\n".$Line['body'];
return $Block; return $Block;
} }
protected function blockFencedCodeComplete($Block) protected function blockFencedCodeComplete($Block)
{ {
$text = $Block['element']['text']['text']; $text = $Block['element']['element']['text'];
$Block['element']['text']['text'] = $text; $Block['element']['element']['text'] = $text;
return $Block; return $Block;
} }
@ -516,8 +518,8 @@ class Parsedown
$Block = array( $Block = array(
'element' => array( 'element' => array(
'name' => 'h' . min(6, $level), 'name' => 'h' . min(6, $level),
'text' => $text, 'elements' => $text,
'handler' => 'line', 'handler' => 'lineElements'
), ),
); );
@ -557,7 +559,6 @@ class Parsedown
), ),
'element' => array( 'element' => array(
'name' => $name, 'name' => $name,
'handler' => 'elements',
), ),
); );
@ -574,10 +575,10 @@ class Parsedown
$Block['li'] = array( $Block['li'] = array(
'name' => 'li', 'name' => 'li',
'handler' => 'li', 'handler' => 'li',
'text' => !empty($matches[3]) ? array($matches[3]) : array(), 'elements' => !empty($matches[3]) ? array($matches[3]) : array(),
); );
$Block['element']['text'] []= & $Block['li']; $Block['element']['elements'] []= & $Block['li'];
return $Block; return $Block;
} }
@ -585,7 +586,7 @@ class Parsedown
protected function blockListContinue($Line, array $Block) protected function blockListContinue($Line, array $Block)
{ {
if (isset($Block['interrupted']) and empty($Block['li']['text'])) if (isset($Block['interrupted']) and empty($Block['li']['elements']))
{ {
return null; return null;
} }
@ -605,7 +606,7 @@ class Parsedown
) { ) {
if (isset($Block['interrupted'])) if (isset($Block['interrupted']))
{ {
$Block['li']['text'] []= ''; $Block['li']['elements'] []= '';
$Block['loose'] = true; $Block['loose'] = true;
@ -621,12 +622,12 @@ class Parsedown
$Block['li'] = array( $Block['li'] = array(
'name' => 'li', 'name' => 'li',
'handler' => 'li', 'handler' => 'li',
'text' => array( 'elements' => array(
$text, $text,
), ),
); );
$Block['element']['text'] []= & $Block['li']; $Block['element']['elements'] []= & $Block['li'];
return $Block; return $Block;
} }
@ -644,14 +645,16 @@ class Parsedown
{ {
if (isset($Block['interrupted'])) if (isset($Block['interrupted']))
{ {
$Block['li']['text'] []= ''; $Block['li']['elements'] []= '';
$Block['loose'] = true;
unset($Block['interrupted']); unset($Block['interrupted']);
} }
$text = substr($Line['body'], $requiredIndent); $text = substr($Line['body'], $requiredIndent);
$Block['li']['text'] []= $text; $Block['li']['elements'] []= $text;
return $Block; return $Block;
} }
@ -660,7 +663,7 @@ class Parsedown
{ {
$text = preg_replace('/^[ ]{0,'.$requiredIndent.'}/', '', $Line['body']); $text = preg_replace('/^[ ]{0,'.$requiredIndent.'}/', '', $Line['body']);
$Block['li']['text'] []= $text; $Block['li']['elements'] []= $text;
return $Block; return $Block;
} }
@ -670,11 +673,11 @@ class Parsedown
{ {
if (isset($Block['loose'])) if (isset($Block['loose']))
{ {
foreach ($Block['element']['text'] as &$li) foreach ($Block['element']['elements'] as &$li)
{ {
if (end($li['text']) !== '') if (end($li['elements']) !== '')
{ {
$li['text'] []= ''; $li['elements'] []= '';
} }
} }
} }
@ -692,8 +695,8 @@ class Parsedown
$Block = array( $Block = array(
'element' => array( 'element' => array(
'name' => 'blockquote', 'name' => 'blockquote',
'handler' => 'lines', 'handler' => 'linesElements',
'text' => (array) $matches[1], 'elements' => (array) $matches[1],
), ),
); );
@ -710,14 +713,14 @@ class Parsedown
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches)) if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
{ {
$Block['element']['text'] []= $matches[1]; $Block['element']['elements'] []= $matches[1];
return $Block; return $Block;
} }
if ( ! isset($Block['interrupted'])) if ( ! isset($Block['interrupted']))
{ {
$Block['element']['text'] []= $Line['text']; $Block['element']['elements'] []= $Line['text'];
return $Block; return $Block;
} }
@ -732,7 +735,7 @@ class Parsedown
{ {
$Block = array( $Block = array(
'element' => array( 'element' => array(
'name' => 'hr' 'name' => 'hr',
), ),
); );
@ -843,7 +846,7 @@ class Parsedown
} }
if ( if (
strpos($Block['element']['text'], '|') === false strpos($Block['element']['elements'], '|') === false
and strpos($Line['text'], '|') === false and strpos($Line['text'], '|') === false
and strpos($Line['text'], ':') === false and strpos($Line['text'], ':') === false
) { ) {
@ -892,7 +895,7 @@ class Parsedown
$HeaderElements = array(); $HeaderElements = array();
$header = $Block['element']['text']; $header = $Block['element']['elements'];
$header = trim($header); $header = trim($header);
$header = trim($header, '|'); $header = trim($header, '|');
@ -910,8 +913,8 @@ class Parsedown
$HeaderElement = array( $HeaderElement = array(
'name' => 'th', 'name' => 'th',
'text' => $headerCell, 'elements' => $headerCell,
'handler' => 'line', 'handler' => 'lineElements',
); );
if (isset($alignments[$index])) if (isset($alignments[$index]))
@ -933,25 +936,21 @@ class Parsedown
'identified' => true, 'identified' => true,
'element' => array( 'element' => array(
'name' => 'table', 'name' => 'table',
'handler' => 'elements',
), ),
); );
$Block['element']['text'] []= array( $Block['element']['elements'] []= array(
'name' => 'thead', 'name' => 'thead',
'handler' => 'elements',
); );
$Block['element']['text'] []= array( $Block['element']['elements'] []= array(
'name' => 'tbody', 'name' => 'tbody',
'handler' => 'elements', 'elements' => array(),
'text' => array(),
); );
$Block['element']['text'][0]['text'] []= array( $Block['element']['elements'][0]['elements'] []= array(
'name' => 'tr', 'name' => 'tr',
'handler' => 'elements', 'elements' => $HeaderElements,
'text' => $HeaderElements,
); );
return $Block; return $Block;
@ -983,8 +982,8 @@ class Parsedown
$Element = array( $Element = array(
'name' => 'td', 'name' => 'td',
'handler' => 'line', 'handler' => 'lineElements',
'text' => $cell, 'elements' => $cell,
); );
if (isset($Block['alignments'][$index])) if (isset($Block['alignments'][$index]))
@ -999,11 +998,10 @@ class Parsedown
$Element = array( $Element = array(
'name' => 'tr', 'name' => 'tr',
'handler' => 'elements', 'elements' => $Elements,
'text' => $Elements,
); );
$Block['element']['text'][1]['text'] []= $Element; $Block['element']['elements'][1]['elements'] []= $Element;
return $Block; return $Block;
} }
@ -1018,8 +1016,8 @@ class Parsedown
$Block = array( $Block = array(
'element' => array( 'element' => array(
'name' => 'p', 'name' => 'p',
'text' => $Line['text'], 'elements' => $Line['text'],
'handler' => 'line', 'handler' => 'lineElements',
), ),
); );
@ -1157,13 +1155,13 @@ class Parsedown
$Inline = array( $Inline = array(
'extent' => strlen($text), 'extent' => strlen($text),
'element' => array( 'element' => array(
'handler' => 'elements', 'elements' => array(),
), ),
); );
if ($this->breaksEnabled) if ($this->breaksEnabled)
{ {
$Inline['element']['text'] = self::pregReplaceElements( $Inline['element']['elements'] = self::pregReplaceElements(
'/[ ]*\n/', '/[ ]*\n/',
array( array(
array('name' => 'br'), array('name' => 'br'),
@ -1174,7 +1172,7 @@ class Parsedown
} }
else else
{ {
$Inline['element']['text'] = self::pregReplaceElements( $Inline['element']['elements'] = self::pregReplaceElements(
'/(?:[ ][ ]+|[ ]*\\\\)\n/', '/(?:[ ][ ]+|[ ]*\\\\)\n/',
array( array(
array('name' => 'br'), array('name' => 'br'),
@ -1262,8 +1260,8 @@ class Parsedown
'extent' => strlen($matches[0]), 'extent' => strlen($matches[0]),
'element' => array( 'element' => array(
'name' => $emphasis, 'name' => $emphasis,
'handler' => 'line', 'handler' => 'lineElements',
'text' => $matches[1], 'elements' => $matches[1],
), ),
); );
} }
@ -1301,8 +1299,9 @@ class Parsedown
'name' => 'img', 'name' => 'img',
'attributes' => array( 'attributes' => array(
'src' => $Link['element']['attributes']['href'], 'src' => $Link['element']['attributes']['href'],
'alt' => $Link['element']['text'], 'alt' => $Link['element']['elements'],
), ),
'autobreak' => true,
), ),
); );
@ -1317,9 +1316,9 @@ class Parsedown
{ {
$Element = array( $Element = array(
'name' => 'a', 'name' => 'a',
'handler' => 'line', 'handler' => 'lineElements',
'nonNestables' => array('Url', 'Link'), 'nonNestables' => array('Url', 'Link'),
'text' => null, 'elements' => null,
'attributes' => array( 'attributes' => array(
'href' => null, 'href' => null,
'title' => null, 'title' => null,
@ -1332,7 +1331,7 @@ class Parsedown
if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches)) if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
{ {
$Element['text'] = $matches[1]; $Element['elements'] = $matches[1];
$extent += strlen($matches[0]); $extent += strlen($matches[0]);
@ -1358,14 +1357,14 @@ class Parsedown
{ {
if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
{ {
$definition = strlen($matches[1]) ? $matches[1] : $Element['text']; $definition = strlen($matches[1]) ? $matches[1] : $Element['elements'];
$definition = strtolower($definition); $definition = strtolower($definition);
$extent += strlen($matches[0]); $extent += strlen($matches[0]);
} }
else else
{ {
$definition = strtolower($Element['text']); $definition = strtolower($Element['elements']);
} }
if ( ! isset($this->DefinitionData['Reference'][$definition])) if ( ! isset($this->DefinitionData['Reference'][$definition]))
@ -1443,8 +1442,8 @@ class Parsedown
'extent' => strlen($matches[0]), 'extent' => strlen($matches[0]),
'element' => array( 'element' => array(
'name' => 'del', 'name' => 'del',
'text' => $matches[1], 'elements' => $matches[1],
'handler' => 'line', 'handler' => 'lineElements',
), ),
); );
} }
@ -1508,6 +1507,41 @@ class Parsedown
# Handlers # Handlers
# #
protected function handle(array $Element)
{
$hasContent = isset($Element['text']) || isset($Element['element']) || isset($Element['elements']);
if (isset($Element['handler']) and $hasContent)
{
if (!isset($Element['nonNestables']))
{
$Element['nonNestables'] = array();
}
if (isset($Element['elements']))
{
$Element['elements'] = $this->{$Element['handler']}($Element['elements'], $Element['nonNestables']);
$Element['elements'] = array_map(array($this, 'handle'), $Element['elements']);
}
elseif (isset($Element['element']))
{
$Element['element'] = $this->{$Element['handler']}($Element['element'], $Element['nonNestables']);
$Element['element'] = $this->handle($Element['element']);
}
else
{
$Element['text'] = $this->{$Element['handler']}($Element['text'], $Element['nonNestables']);
}
}
unset($Element['handler']);
return $Element;
}
protected function element(array $Element) protected function element(array $Element)
{ {
if ($this->safeMode) if ($this->safeMode)
@ -1515,6 +1549,8 @@ class Parsedown
$Element = $this->sanitiseElement($Element); $Element = $this->sanitiseElement($Element);
} }
$Element = $this->handle($Element);
$hasName = isset($Element['name']); $hasName = isset($Element['name']);
$markup = ''; $markup = '';
@ -1553,26 +1589,30 @@ class Parsedown
$permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode; $permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode;
} }
if (isset($text)) $hasContent = isset($text) || isset($Element['element']) || isset($Element['elements']);
if ($hasContent)
{ {
$markup .= $hasName ? '>' : ''; $markup .= $hasName ? '>' : '';
if (!isset($Element['nonNestables'])) if (isset($Element['elements']))
{ {
$Element['nonNestables'] = array(); $markup .= $this->elements($Element['elements']);
} }
elseif (isset($Element['element']))
if (isset($Element['handler']))
{ {
$markup .= $this->{$Element['handler']}($text, $Element['nonNestables']); $markup .= $this->element($Element['element']);
}
elseif (!$permitRawHtml)
{
$markup .= self::escape($text, true);
} }
else else
{ {
$markup .= $text; if (!$permitRawHtml)
{
$markup .= self::escape($text, true);
}
else
{
$markup .= $text;
}
} }
$markup .= $hasName ? '</'.$Element['name'].'>' : ''; $markup .= $hasName ? '</'.$Element['name'].'>' : '';
@ -1612,21 +1652,16 @@ class Parsedown
protected function li($lines) protected function li($lines)
{ {
$markup = $this->lines($lines); $Elements = $this->linesElements($lines);
$trimmedMarkup = trim($markup); if ( ! in_array('', $lines)
and isset($Elements[0]) and isset($Elements[0]['name'])
if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>') and $Elements[0]['name'] === 'p'
{ ) {
$markup = $trimmedMarkup; unset($Elements[0]['name']);
$markup = substr($markup, 3);
$position = strpos($markup, "</p>");
$markup = substr_replace($markup, '', $position, 4);
} }
return $markup; return $Elements;
} }
# #

View File

@ -4,15 +4,15 @@ class UnsafeExtension extends Parsedown
{ {
protected function blockFencedCodeComplete($Block) protected function blockFencedCodeComplete($Block)
{ {
$text = $Block['element']['text']['text']; $text = $Block['element']['element']['text'];
unset($Block['element']['text']['text']); unset($Block['element']['element']['text']);
// WARNING: There is almost always a better way of doing things! // WARNING: There is almost always a better way of doing things!
// //
// This example is one of them, unsafe behaviour is NOT needed here. // This example is one of them, unsafe behaviour is NOT needed here.
// Only use this if you trust the input and have no idea what // Only use this if you trust the input and have no idea what
// the output HTML will look like (e.g. using an external parser). // the output HTML will look like (e.g. using an external parser).
$Block['element']['text']['rawHtml'] = "<p>$text</p>"; $Block['element']['element']['rawHtml'] = "<p>$text</p>";
return $Block; return $Block;
} }
@ -23,8 +23,8 @@ class TrustDelegatedExtension extends Parsedown
{ {
protected function blockFencedCodeComplete($Block) protected function blockFencedCodeComplete($Block)
{ {
$text = $Block['element']['text']['text']; $text = $Block['element']['element']['text'];
unset($Block['element']['text']['text']); unset($Block['element']['element']['text']);
// WARNING: There is almost always a better way of doing things! // WARNING: There is almost always a better way of doing things!
// //
@ -32,8 +32,8 @@ class TrustDelegatedExtension extends Parsedown
// Only use this if you are sure that the result being added into // Only use this if you are sure that the result being added into
// rawHtml is safe. // rawHtml is safe.
// (e.g. using an external parser with escaping capabilities). // (e.g. using an external parser with escaping capabilities).
$Block['element']['text']['rawHtml'] = "<p>$text</p>"; $Block['element']['element']['rawHtml'] = "<p>$text</p>";
$Block['element']['text']['allowRawHtmlInSafeMode'] = true; $Block['element']['element']['allowRawHtmlInSafeMode'] = true;
return $Block; return $Block;
} }

View File

@ -1,30 +1,30 @@
<ul> <ul>
<li>li <li>li<ul>
<ul> <li>li<ul>
<li>li
<ul>
<li>li</li> <li>li</li>
<li>li</li> <li>li</li>
</ul></li> </ul>
</li>
<li>li</li> <li>li</li>
</ul></li> </ul>
</li>
<li>li</li> <li>li</li>
</ul> </ul>
<hr /> <hr />
<ul> <ul>
<li>level 1 <li>level 1<ul>
<ul> <li>level 2<ul>
<li>level 2 <li>level 3<ul>
<ul> <li>level 4<ul>
<li>level 3
<ul>
<li>level 4
<ul>
<li>level 5</li> <li>level 5</li>
</ul></li> </ul>
</ul></li> </li>
</ul></li> </ul>
</ul></li> </li>
</ul>
</li>
</ul>
</li>
</ul> </ul>
<hr /> <hr />
<ul> <ul>