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\Block\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Block\Element\Heading; |
18
|
|
|
use League\CommonMark\Block\Element\Paragraph; |
19
|
|
|
use League\CommonMark\ContextInterface; |
20
|
|
|
use League\CommonMark\Cursor; |
21
|
|
|
use League\CommonMark\ReferenceParser; |
22
|
|
|
use League\CommonMark\Util\RegexHelper; |
23
|
|
|
|
24
|
|
|
class SetExtHeadingParser implements BlockParserInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param ContextInterface $context |
28
|
|
|
* @param Cursor $cursor |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
1833 |
|
public function parse(ContextInterface $context, Cursor $cursor): bool |
33
|
|
|
{ |
34
|
1833 |
|
if ($cursor->isIndented()) { |
35
|
180 |
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
1776 |
|
if (!($context->getContainer() instanceof Paragraph)) { |
39
|
1776 |
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
333 |
|
$match = RegexHelper::matchAll('/^(?:=+|-+)[ \t]*$/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
43
|
333 |
|
if ($match === null) { |
44
|
288 |
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
60 |
|
$level = $match[0][0] === '=' ? 1 : 2; |
48
|
60 |
|
$strings = $context->getContainer()->getStrings(); |
49
|
|
|
|
50
|
60 |
|
$strings = $this->resolveReferenceLinkDefinitions($strings, $cursor->getEncoding(), $context->getReferenceParser()); |
51
|
60 |
|
if (empty($strings)) { |
52
|
3 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
57 |
|
$context->replaceContainerBlock(new Heading($level, $strings)); |
56
|
|
|
|
57
|
57 |
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Resolve reference link definition |
62
|
|
|
* |
63
|
|
|
* @see https://github.com/commonmark/commonmark.js/commit/993bbe335931af847460effa99b2411eb643577d |
64
|
|
|
* |
65
|
|
|
* @param string[] $strings |
66
|
|
|
* @param string $encoding |
67
|
|
|
* @param ReferenceParser $referenceParser |
68
|
|
|
* |
69
|
|
|
* @return string[] |
70
|
|
|
*/ |
71
|
60 |
|
private function resolveReferenceLinkDefinitions(array $strings, string $encoding, ReferenceParser $referenceParser): array |
72
|
|
|
{ |
73
|
60 |
|
foreach ($strings as &$string) { |
74
|
60 |
|
$cursor = new Cursor($string, $encoding); |
75
|
60 |
|
while ($cursor->getCharacter() === '[' && $referenceParser->parse($cursor)) { |
76
|
6 |
|
$string = $cursor->getRemainder(); |
77
|
|
|
} |
78
|
|
|
|
79
|
60 |
|
if ($string !== '') { |
80
|
58 |
|
break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return \array_filter($strings, function ($s) { |
85
|
60 |
|
return $s !== ''; |
86
|
60 |
|
}); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|