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 (http://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\Block\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Block\Element\Heading; |
18
|
|
|
use League\CommonMark\ContextInterface; |
19
|
|
|
use League\CommonMark\Cursor; |
20
|
|
|
use League\CommonMark\Util\RegexHelper; |
21
|
|
|
|
22
|
|
|
class ATXHeadingParser extends AbstractBlockParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param ContextInterface $context |
26
|
|
|
* @param Cursor $cursor |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
1872 |
|
public function parse(ContextInterface $context, Cursor $cursor) |
31
|
|
|
{ |
32
|
1872 |
|
if ($cursor->isIndented()) { |
33
|
177 |
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
1818 |
|
$match = RegexHelper::matchAll('/^#{1,6}(?: +|$)/', $cursor->getLine(), $cursor->getFirstNonSpacePosition()); |
37
|
1818 |
|
if (!$match) { |
38
|
1779 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
75 |
|
$cursor->advanceToFirstNonSpace(); |
42
|
|
|
|
43
|
75 |
|
$cursor->advanceBy(strlen($match[0])); |
44
|
|
|
|
45
|
75 |
|
$level = strlen(trim($match[0])); |
46
|
75 |
|
$str = $cursor->getRemainder(); |
47
|
75 |
|
$str = preg_replace('/^ *#+ *$/', '', $str); |
48
|
75 |
|
$str = preg_replace('/ +#+ *$/', '', $str); |
49
|
|
|
|
50
|
75 |
|
$context->addBlock(new Heading($level, $str)); |
51
|
75 |
|
$context->setBlocksParsed(true); |
52
|
|
|
|
53
|
75 |
|
return true; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|