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