|
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\Parser\Block\BlockStart; |
|
19
|
|
|
use League\CommonMark\Parser\Block\BlockStartParserInterface; |
|
20
|
|
|
use League\CommonMark\Parser\Cursor; |
|
21
|
|
|
use League\CommonMark\Parser\MarkdownParserStateInterface; |
|
22
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
23
|
|
|
use League\Config\ConfigurationAwareInterface; |
|
24
|
|
|
use League\Config\ConfigurationInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class ListBlockStartParser implements BlockStartParserInterface, ConfigurationAwareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
29
|
|
|
private ?ConfigurationInterface $config = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @psalm-var non-empty-string|null |
|
33
|
|
|
* |
|
34
|
|
|
* @psalm-readonly-allow-private-mutation |
|
35
|
|
|
*/ |
|
36
|
|
|
private ?string $listMarkerRegex = null; |
|
37
|
|
|
|
|
38
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->config = $configuration; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
|
44
|
|
|
{ |
|
45
|
|
|
if ($cursor->isIndented()) { |
|
46
|
|
|
return BlockStart::none(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$listData = $this->parseList($cursor, $parserState->getParagraphContent() !== null); |
|
50
|
|
|
if ($listData === null) { |
|
51
|
|
|
return BlockStart::none(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$listItemParser = new ListItemParser($listData); |
|
55
|
|
|
|
|
56
|
|
|
// prepend the list block if needed |
|
57
|
|
|
$matched = $parserState->getLastMatchedBlockParser(); |
|
58
|
|
|
if (! ($matched instanceof ListBlockParser) || ! $listData->equals($matched->getBlock()->getListData())) { |
|
59
|
|
|
$listBlockParser = new ListBlockParser($listData); |
|
60
|
|
|
// We start out with assuming a list is tight. If we find a blank line, we set it to loose later. |
|
61
|
|
|
// TODO for 3.0: Just make them tight by default in the block so we can remove this call |
|
62
|
|
|
$listBlockParser->getBlock()->setTight(true); |
|
63
|
|
|
|
|
64
|
|
|
return BlockStart::of($listBlockParser, $listItemParser)->at($cursor); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return BlockStart::of($listItemParser)->at($cursor); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function parseList(Cursor $cursor, bool $inParagraph): ?ListData |
|
71
|
|
|
{ |
|
72
|
|
|
$indent = $cursor->getIndent(); |
|
73
|
|
|
|
|
74
|
|
|
$tmpCursor = clone $cursor; |
|
75
|
|
|
$tmpCursor->advanceToNextNonSpaceOrTab(); |
|
76
|
|
|
$rest = $tmpCursor->getRemainder(); |
|
77
|
|
|
|
|
78
|
|
|
if (\preg_match($this->listMarkerRegex ?? $this->generateListMarkerRegex(), $rest) === 1) { |
|
79
|
|
|
$data = new ListData(); |
|
80
|
|
|
$data->markerOffset = $indent; |
|
81
|
|
|
$data->type = ListBlock::TYPE_BULLET; |
|
82
|
|
|
$data->delimiter = null; |
|
83
|
|
|
$data->bulletChar = $rest[0]; |
|
84
|
|
|
$markerLength = 1; |
|
85
|
|
|
} elseif (($matches = RegexHelper::matchFirst('/^(\d{1,9})([.)])/', $rest)) && (! $inParagraph || $matches[1] === '1')) { |
|
86
|
|
|
$data = new ListData(); |
|
87
|
|
|
$data->markerOffset = $indent; |
|
88
|
|
|
$data->type = ListBlock::TYPE_ORDERED; |
|
89
|
|
|
$data->start = (int) $matches[1]; |
|
90
|
|
|
$data->delimiter = $matches[2] === '.' ? ListBlock::DELIM_PERIOD : ListBlock::DELIM_PAREN; |
|
91
|
|
|
$data->bulletChar = null; |
|
92
|
|
|
$markerLength = \strlen($matches[0]); |
|
93
|
|
|
} else { |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Make sure we have spaces after |
|
98
|
|
|
$nextChar = $tmpCursor->peek($markerLength); |
|
99
|
|
|
if (! ($nextChar === null || $nextChar === "\t" || $nextChar === ' ')) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// If it interrupts paragraph, make sure first line isn't blank |
|
104
|
|
|
if ($inParagraph && ! RegexHelper::matchAt(RegexHelper::REGEX_NON_SPACE, $rest, $markerLength)) { |
|
|
|
|
|
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$cursor->advanceToNextNonSpaceOrTab(); // to start of marker |
|
109
|
|
|
$cursor->advanceBy($markerLength, true); // to end of marker |
|
110
|
|
|
$data->padding = self::calculateListMarkerPadding($cursor, $markerLength); |
|
111
|
|
|
|
|
112
|
|
|
return $data; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private static function calculateListMarkerPadding(Cursor $cursor, int $markerLength): int |
|
116
|
|
|
{ |
|
117
|
|
|
$start = $cursor->saveState(); |
|
118
|
|
|
$spacesStartCol = $cursor->getColumn(); |
|
119
|
|
|
|
|
120
|
|
|
while ($cursor->getColumn() - $spacesStartCol < 5) { |
|
121
|
|
|
if (! $cursor->advanceBySpaceOrTab()) { |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$blankItem = $cursor->peek() === null; |
|
127
|
|
|
$spacesAfterMarker = $cursor->getColumn() - $spacesStartCol; |
|
128
|
|
|
|
|
129
|
|
|
if ($spacesAfterMarker >= 5 || $spacesAfterMarker < 1 || $blankItem) { |
|
130
|
|
|
$cursor->restoreState($start); |
|
131
|
|
|
$cursor->advanceBySpaceOrTab(); |
|
132
|
|
|
|
|
133
|
|
|
return $markerLength + 1; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $markerLength + $spacesAfterMarker; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @psalm-return non-empty-string |
|
141
|
|
|
*/ |
|
142
|
|
|
private function generateListMarkerRegex(): string |
|
143
|
|
|
{ |
|
144
|
|
|
// No configuration given - use the defaults |
|
145
|
|
|
if ($this->config === null) { |
|
146
|
|
|
return $this->listMarkerRegex = '/^[*+-]/'; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$markers = $this->config->get('commonmark/unordered_list_markers'); |
|
150
|
|
|
\assert(\is_array($markers)); |
|
151
|
|
|
|
|
152
|
|
|
return $this->listMarkerRegex = '/^[' . \preg_quote(\implode('', $markers), '/') . ']/'; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: