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\ListData; |
17
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; |
18
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
19
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
20
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
21
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
22
|
|
|
use League\CommonMark\Parser\Cursor; |
23
|
|
|
|
24
|
|
|
final class ListItemParser extends AbstractBlockContinueParser |
25
|
|
|
{ |
26
|
|
|
/** @psalm-readonly */ |
27
|
|
|
private ListItem $block; |
28
|
|
|
|
29
|
216 |
|
public function __construct(ListData $listData) |
30
|
|
|
{ |
31
|
216 |
|
$this->block = new ListItem($listData); |
32
|
|
|
} |
33
|
|
|
|
34
|
216 |
|
public function getBlock(): ListItem |
35
|
|
|
{ |
36
|
216 |
|
return $this->block; |
37
|
|
|
} |
38
|
|
|
|
39
|
204 |
|
public function isContainer(): bool |
40
|
|
|
{ |
41
|
204 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
200 |
|
public function canContain(AbstractBlock $childBlock): bool |
45
|
|
|
{ |
46
|
200 |
|
return ! $childBlock instanceof ListItem; |
47
|
|
|
} |
48
|
|
|
|
49
|
176 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
50
|
|
|
{ |
51
|
176 |
|
if ($cursor->isBlank()) { |
52
|
108 |
|
if ($this->block->firstChild() === null) { |
53
|
|
|
// Blank line after empty list item |
54
|
4 |
|
return BlockContinue::none(); |
55
|
|
|
} |
56
|
|
|
|
57
|
104 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
58
|
|
|
|
59
|
104 |
|
return BlockContinue::at($cursor); |
60
|
|
|
} |
61
|
|
|
|
62
|
174 |
|
$contentIndent = $this->block->getListData()->markerOffset + $this->getBlock()->getListData()->padding; |
63
|
174 |
|
if ($cursor->getIndent() >= $contentIndent) { |
64
|
100 |
|
$cursor->advanceBy($contentIndent, true); |
65
|
|
|
|
66
|
100 |
|
return BlockContinue::at($cursor); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Note: We'll hit this case for lazy continuation lines, they will get added later. |
70
|
118 |
|
return BlockContinue::none(); |
71
|
|
|
} |
72
|
|
|
|
73
|
204 |
|
public function closeBlock(): void |
74
|
|
|
{ |
75
|
204 |
|
if ($this->block->lastChild() !== null) { |
76
|
200 |
|
$this->block->setEndLine($this->block->lastChild()->getEndLine()); |
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
// Empty list item |
79
|
12 |
|
$this->block->setEndLine($this->block->getStartLine()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|