Skip to content

Commit

Permalink
Merge pull request #439 from aidantwoods/patch-4
Browse files Browse the repository at this point in the history
Improve CommonMark mixed-marker list compliance
  • Loading branch information
aidantwoods authored Mar 27, 2018
2 parents a52d386 + d2a73f9 commit 2f291e0
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 20 deletions.
76 changes: 61 additions & 15 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,31 @@ protected function blockHeader($Line)

protected function blockList($Line)
{
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]{1,9}[.\)]');

if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
if (preg_match('/^('.$pattern.'([ ]+|$))(.*)/', $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] .= ' ';
}

$Block = array(
'indent' => $Line['indent'],
'pattern' => $pattern,
'data' => array(
'type' => $name,
'marker' => $matches[1],
'markerType' => ($name === 'ul' ? strstr($matches[1], ' ', true) : substr(strstr($matches[1], ' ', true), -1)),
),
'element' => array(
'name' => $name,
'handler' => 'elements',
Expand All @@ -539,7 +557,7 @@ protected function blockList($Line)

if($name === 'ol')
{
$listStart = stristr($matches[0], '.', true);
$listStart = ltrim(strstr($matches[1], $Block['data']['markerType'], true), '0') ?: '0';

if($listStart !== '1')
{
Expand All @@ -550,9 +568,7 @@ protected function blockList($Line)
$Block['li'] = array(
'name' => 'li',
'handler' => 'li',
'text' => array(
$matches[2],
),
'text' => !empty($matches[3]) ? array($matches[3]) : array(),
);

$Block['element']['text'] []= & $Block['li'];
Expand All @@ -563,7 +579,28 @@ protected function blockList($Line)

protected function blockListContinue($Line, array $Block)
{
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches))
if (isset($Block['interrupted']) and empty($Block['li']['text']))
{
return null;
}

$requiredIndent = ($Block['indent'] + strlen($Block['data']['marker']));

if (
$Line['indent'] < $requiredIndent
and
(
(
$Block['data']['type'] === 'ol'
and preg_match('/^[0-9]+'.preg_quote($Block['data']['markerType']).'(?:[ ]+(.*)|$)/', $Line['text'], $matches)
)
or
(
$Block['data']['type'] === 'ul'
and preg_match('/^'.preg_quote($Block['data']['markerType']).'(?:[ ]+(.*)|$)/', $Line['text'], $matches)
)
)
)
{
if (isset($Block['interrupted']))
{
Expand All @@ -578,6 +615,8 @@ protected function blockListContinue($Line, array $Block)

$text = isset($matches[1]) ? $matches[1] : '';

$Block['indent'] = $Line['indent'];

$Block['li'] = array(
'name' => 'li',
'handler' => 'li',
Expand All @@ -590,31 +629,38 @@ protected function blockListContinue($Line, array $Block)

return $Block;
}
elseif ($Line['indent'] < $requiredIndent and $this->blockList($Line))
{
return null;
}

if ($Line['text'][0] === '[' and $this->blockReference($Line))
{
return $Block;
}

if ( ! isset($Block['interrupted']))
if ($Line['indent'] >= $requiredIndent)
{
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
if (isset($Block['interrupted']))
{
$Block['li']['text'] []= '';

unset($Block['interrupted']);
}

$text = substr($Line['body'], $requiredIndent);

$Block['li']['text'] []= $text;

return $Block;
}

if ($Line['indent'] > 0)
if ( ! isset($Block['interrupted']))
{
$Block['li']['text'] []= '';

$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
$text = preg_replace('/^[ ]{0,'.$requiredIndent.'}/', '', $Line['body']);

$Block['li']['text'] []= $text;

unset($Block['interrupted']);

return $Block;
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/data/deeply_nested_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,32 @@
<li>li</li>
</ul></li>
<li>li</li>
</ul>
<hr />
<ul>
<li>level 1
<ul>
<li>level 2
<ul>
<li>level 3
<ul>
<li>level 4
<ul>
<li>level 5</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>
<hr />
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
<li>i</li>
</ul>
22 changes: 21 additions & 1 deletion test/data/deeply_nested_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,24 @@
- li
- li
- li
- li
- li

---

- level 1
- level 2
- level 3
- level 4
- level 5

---

- a
- b
- c
- d
- e
- f
- g
- h
- i
16 changes: 14 additions & 2 deletions test/data/unordered_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
<li>li</li>
<li>li</li>
</ul>
<p>mixed markers:</p>
<p>mixed unordered markers:</p>
<ul>
<li>li</li>
</ul>
<ul>
<li>li</li>
</ul>
<ul>
<li>li</li>
</ul>
</ul>
<p>mixed ordered markers:</p>
<ol>
<li>starting at 1, list one</li>
<li>number 2, list one</li>
</ol>
<ol start="3">
<li>starting at 3, list two</li>
</ol>
10 changes: 8 additions & 2 deletions test/data/unordered_list.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
- li
- li

mixed markers:
mixed unordered markers:

* li
+ li
- li
- li

mixed ordered markers:

1. starting at 1, list one
2. number 2, list one
3) starting at 3, list two

0 comments on commit 2f291e0

Please sign in to comment.