|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace League\CommonMark\Extension\CommonMark\Parser\Block; |
|
13
|
|
|
|
|
14
|
|
|
use League\CommonMark\Parser\Block\BlockStart; |
|
15
|
|
|
use League\CommonMark\Parser\Block\BlockStartParserInterface; |
|
16
|
|
|
use League\CommonMark\Parser\Cursor; |
|
17
|
|
|
use League\CommonMark\Parser\MarkdownParserStateInterface; |
|
18
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
19
|
|
|
|
|
20
|
|
|
class HeadingStartParser implements BlockStartParserInterface |
|
21
|
|
|
{ |
|
22
|
2058 |
|
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
|
23
|
|
|
{ |
|
24
|
2058 |
|
if ($cursor->isIndented()) { |
|
25
|
183 |
|
return BlockStart::none(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1959 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
29
|
|
|
|
|
30
|
1959 |
|
if ($atxHeading = self::getAtxHeader($cursor)) { |
|
31
|
132 |
|
return BlockStart::of($atxHeading)->at($cursor); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1842 |
|
$setextHeadingLevel = self::getSetextHeadingLevel($cursor); |
|
35
|
1842 |
|
if ($setextHeadingLevel > 0) { |
|
36
|
132 |
|
$content = $parserState->getParagraphContent(); |
|
37
|
132 |
|
if ($content !== null) { |
|
38
|
66 |
|
$cursor->advanceToEnd(); |
|
39
|
|
|
|
|
40
|
66 |
|
return BlockStart::of(new HeadingParser($setextHeadingLevel, $content)) |
|
41
|
66 |
|
->at($cursor) |
|
42
|
66 |
|
->replaceActiveBlockParser(); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1800 |
|
return BlockStart::none(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1959 |
|
private static function getAtxHeader(Cursor $cursor): ?HeadingParser |
|
50
|
|
|
{ |
|
51
|
1959 |
|
$match = RegexHelper::matchAll('/^#{1,6}(?:[ \t]+|$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
|
52
|
1959 |
|
if (!$match) { |
|
53
|
1842 |
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
132 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
57
|
132 |
|
$cursor->advanceBy(\strlen($match[0])); |
|
58
|
|
|
|
|
59
|
132 |
|
$level = \strlen(\trim($match[0])); |
|
60
|
132 |
|
$str = $cursor->getRemainder(); |
|
61
|
|
|
/** @var string $str */ |
|
62
|
132 |
|
$str = \preg_replace('/^[ \t]*#+[ \t]*$/', '', $str); |
|
63
|
|
|
/** @var string $str */ |
|
64
|
132 |
|
$str = \preg_replace('/[ \t]+#+[ \t]*$/', '', $str); |
|
65
|
|
|
|
|
66
|
132 |
|
return new HeadingParser($level, $str); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1842 |
|
private static function getSetextHeadingLevel(Cursor $cursor): int |
|
70
|
|
|
{ |
|
71
|
1842 |
|
$match = RegexHelper::matchAll('/^(?:=+|-+)[ \t]*$/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
|
72
|
1842 |
|
if ($match === null) { |
|
73
|
1758 |
|
return 0; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
132 |
|
return $match[0][0] === '=' ? 1 : 2; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|