1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace League\CommonMark\Extension\CommonMark\Parser\Block; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; |
17
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListData; |
18
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; |
19
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
20
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
21
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
22
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
23
|
|
|
use League\CommonMark\Parser\Cursor; |
24
|
|
|
|
25
|
|
|
final class ListBlockParser extends AbstractBlockContinueParser |
26
|
|
|
{ |
27
|
|
|
/** @psalm-readonly */ |
28
|
|
|
private ListBlock $block; |
29
|
|
|
|
30
|
216 |
|
public function __construct(ListData $listData) |
31
|
|
|
{ |
32
|
216 |
|
$this->block = new ListBlock($listData); |
33
|
|
|
} |
34
|
|
|
|
35
|
216 |
|
public function getBlock(): ListBlock |
36
|
|
|
{ |
37
|
216 |
|
return $this->block; |
38
|
|
|
} |
39
|
|
|
|
40
|
204 |
|
public function isContainer(): bool |
41
|
|
|
{ |
42
|
204 |
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
204 |
|
public function canContain(AbstractBlock $childBlock): bool |
46
|
|
|
{ |
47
|
204 |
|
return $childBlock instanceof ListItem; |
48
|
|
|
} |
49
|
|
|
|
50
|
176 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): BlockContinue|null |
51
|
|
|
{ |
52
|
|
|
// List blocks themselves don't have any markers, only list items. So try to stay in the list. |
53
|
|
|
// If there is a block start other than list item, canContain makes sure that this list is closed. |
54
|
176 |
|
return BlockContinue::at($cursor); |
55
|
|
|
} |
56
|
|
|
|
57
|
204 |
|
public function closeBlock(): void |
58
|
|
|
{ |
59
|
204 |
|
$item = $this->block->firstChild(); |
60
|
204 |
|
while ($item) { |
61
|
|
|
// check for non-final list item ending with blank line: |
62
|
204 |
|
if ($item->next() !== null && self::endsWithBlankLine($item)) { |
63
|
20 |
|
$this->block->setTight(false); |
64
|
20 |
|
break; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// recurse into children of list item, to see if there are spaces between any of them |
68
|
196 |
|
$subitem = $item->firstChild(); |
69
|
196 |
|
while ($subitem) { |
70
|
192 |
|
if ($subitem->next() && self::endsWithBlankLine($subitem)) { |
71
|
58 |
|
$this->block->setTight(false); |
72
|
58 |
|
break 2; |
73
|
|
|
} |
74
|
|
|
|
75
|
150 |
|
$subitem = $subitem->next(); |
76
|
|
|
} |
77
|
|
|
|
78
|
154 |
|
$item = $item->next(); |
79
|
|
|
} |
80
|
|
|
|
81
|
204 |
|
$this->block->setEndLine($this->block->lastChild()->getEndLine()); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
146 |
|
private static function endsWithBlankLine(AbstractBlock $block): bool |
85
|
|
|
{ |
86
|
146 |
|
return $block->next() !== null && $block->getEndLine() !== $block->next()->getStartLine() - 1; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|