mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement List
This commit is contained in:
@@ -334,163 +334,6 @@ class Parsedown
|
||||
return $Block;
|
||||
}
|
||||
|
||||
#
|
||||
# List
|
||||
|
||||
protected function blockList(Context $Context, array $CurrentBlock = null)
|
||||
{
|
||||
list($name, $pattern) = $Context->line()->text()[0] <= '-' ? ['ul', '[*+-]'] : ['ol', '[0-9]{1,9}+[.\)]'];
|
||||
|
||||
if (\preg_match('/^('.$pattern.'([ ]++|$))(.*+)/', $Context->line()->text(), $matches)) {
|
||||
$contentIndent = \strlen($matches[2]);
|
||||
|
||||
if ($contentIndent >= 5) {
|
||||
$contentIndent -= 1;
|
||||
$matches[1] = \substr($matches[1], 0, -$contentIndent);
|
||||
$matches[3] = \str_repeat(' ', $contentIndent) . $matches[3];
|
||||
} elseif ($contentIndent === 0) {
|
||||
$matches[1] .= ' ';
|
||||
}
|
||||
|
||||
$markerWithoutWhitespace = \strstr($matches[1], ' ', true);
|
||||
|
||||
$Block = [
|
||||
'indent' => $Context->line()->indent(),
|
||||
'pattern' => $pattern,
|
||||
'data' => [
|
||||
'type' => $name,
|
||||
'marker' => $matches[1],
|
||||
'markerType' => ($name === 'ul' ? $markerWithoutWhitespace : \substr($markerWithoutWhitespace, -1)),
|
||||
],
|
||||
'element' => [
|
||||
'name' => $name,
|
||||
'elements' => [],
|
||||
],
|
||||
];
|
||||
$Block['data']['markerTypeRegex'] = \preg_quote($Block['data']['markerType'], '/');
|
||||
|
||||
if ($name === 'ol') {
|
||||
$listStart = \ltrim(\strstr($matches[1], $Block['data']['markerType'], true), '0') ?: '0';
|
||||
|
||||
if ($listStart !== '1') {
|
||||
if (
|
||||
isset($CurrentBlock)
|
||||
and $CurrentBlock['type'] === 'Paragraph'
|
||||
and ! $Context->previousEmptyLines() > 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$Block['element']['attributes'] = ['start' => $listStart];
|
||||
}
|
||||
}
|
||||
|
||||
$Block['li'] = [
|
||||
'name' => 'li',
|
||||
'handler' => [
|
||||
'function' => 'li',
|
||||
'argument' => !empty($matches[3]) ? Lines::fromTextLines($matches[3], 0) : Lines::none(),
|
||||
'destination' => 'elements'
|
||||
]
|
||||
];
|
||||
|
||||
$Block['element']['elements'] []= & $Block['li'];
|
||||
|
||||
return $Block;
|
||||
}
|
||||
}
|
||||
|
||||
protected function blockListContinue(Context $Context, array $Block)
|
||||
{
|
||||
if ($Context->previousEmptyLines() > 0 and $Block['li']['handler']['argument']->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$requiredIndent = ($Block['indent'] + \strlen($Block['data']['marker']));
|
||||
|
||||
if ($Context->line()->indent() < $requiredIndent
|
||||
and (
|
||||
(
|
||||
$Block['data']['type'] === 'ol'
|
||||
and \preg_match('/^[0-9]++'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Context->line()->text(), $matches)
|
||||
) or (
|
||||
$Block['data']['type'] === 'ul'
|
||||
and \preg_match('/^'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Context->line()->text(), $matches)
|
||||
)
|
||||
)
|
||||
) {
|
||||
if ($Context->previousEmptyLines() > 0) {
|
||||
$Block['li']['handler']['argument'] = $Block['li']['handler']['argument']->appendingBlankLines(1);
|
||||
|
||||
$Block['loose'] = true;
|
||||
|
||||
unset($Block['interrupted']);
|
||||
}
|
||||
|
||||
unset($Block['li']);
|
||||
|
||||
$text = isset($matches[1]) ? $matches[1] : '';
|
||||
|
||||
$Block['indent'] = $Context->line()->indent();
|
||||
|
||||
$Block['li'] = [
|
||||
'name' => 'li',
|
||||
'handler' => [
|
||||
'function' => 'li',
|
||||
'argument' => Lines::fromTextLines($text, 0),
|
||||
'destination' => 'elements'
|
||||
]
|
||||
];
|
||||
|
||||
$Block['element']['elements'] []= & $Block['li'];
|
||||
|
||||
return $Block;
|
||||
} elseif ($Context->line()->indent() < $requiredIndent and $this->blockList($Context)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($Context->line()->text()[0] === '[' and $this->blockReference($Context)) {
|
||||
return $Block;
|
||||
}
|
||||
|
||||
if ($Context->line()->indent() >= $requiredIndent) {
|
||||
if ($Context->previousEmptyLines() > 0) {
|
||||
$Block['li']['handler']['argument'] = $Block['li']['handler']['argument']->appendingBlankLines(1);
|
||||
|
||||
$Block['loose'] = true;
|
||||
|
||||
unset($Block['interrupted']);
|
||||
}
|
||||
|
||||
$text = $Context->line()->ltrimBodyUpto($requiredIndent);
|
||||
|
||||
$Block['li']['handler']['argument'] = $Block['li']['handler']['argument']->appendingTextLines($text, 0);
|
||||
|
||||
return $Block;
|
||||
}
|
||||
|
||||
if (! $Context->previousEmptyLines() > 0) {
|
||||
$text = $Context->line()->ltrimBodyUpto($requiredIndent);
|
||||
|
||||
$Block['li']['handler']['argument'] = $Block['li']['handler']['argument']->appendingTextLines($text, 0);
|
||||
|
||||
return $Block;
|
||||
}
|
||||
}
|
||||
|
||||
protected function blockListComplete(array $Block)
|
||||
{
|
||||
if (isset($Block['loose'])) {
|
||||
foreach ($Block['element']['elements'] as &$li) {
|
||||
if ($li['handler']['argument']->trailingBlankLines() === 0) {
|
||||
$li['handler']['argument'] = $li['handler']['argument']->appendingBlankLines(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $Block;
|
||||
}
|
||||
|
||||
#
|
||||
# Rule
|
||||
|
||||
|
||||
Reference in New Issue
Block a user