1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
9
|
|
|
* - (c) John MacFarlane |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\CommonMark\Parser\Block; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\Heading; |
18
|
|
|
use League\CommonMark\Parser\Block\BlockParserInterface; |
19
|
|
|
use League\CommonMark\Parser\ContextInterface; |
20
|
|
|
use League\CommonMark\Parser\Cursor; |
21
|
|
|
use League\CommonMark\Util\RegexHelper; |
22
|
|
|
|
23
|
|
|
final class ATXHeadingParser implements BlockParserInterface |
24
|
|
|
{ |
25
|
2484 |
|
public function parse(ContextInterface $context, Cursor $cursor): bool |
26
|
|
|
{ |
27
|
2484 |
|
if ($cursor->isIndented()) { |
28
|
183 |
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
2427 |
|
$match = RegexHelper::matchAll('/^#{1,6}(?:[ \t]+|$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
32
|
2427 |
|
if (!$match) { |
33
|
2355 |
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
132 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
37
|
|
|
|
38
|
132 |
|
$cursor->advanceBy(\strlen($match[0])); |
39
|
|
|
|
40
|
132 |
|
$level = \strlen(\trim($match[0])); |
41
|
132 |
|
$str = $cursor->getRemainder(); |
42
|
|
|
/** @var string $str */ |
43
|
132 |
|
$str = \preg_replace('/^[ \t]*#+[ \t]*$/', '', $str); |
44
|
|
|
/** @var string $str */ |
45
|
132 |
|
$str = \preg_replace('/[ \t]+#+[ \t]*$/', '', $str); |
46
|
|
|
|
47
|
132 |
|
$context->addBlock(new Heading($level, $str)); |
48
|
132 |
|
$context->setBlocksParsed(true); |
49
|
|
|
|
50
|
132 |
|
return true; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|