|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* (c) Rezo Zero / Ambroise Maupate |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\Footnote\Parser; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteRef; |
|
18
|
|
|
use League\CommonMark\Inline\Parser\InlineParserInterface; |
|
19
|
|
|
use League\CommonMark\InlineParserContext; |
|
20
|
|
|
use League\CommonMark\Normalizer\SlugNormalizer; |
|
21
|
|
|
use League\CommonMark\Normalizer\TextNormalizerInterface; |
|
22
|
|
|
use League\CommonMark\Reference\Reference; |
|
23
|
|
|
|
|
24
|
|
|
final class AnonymousFootnoteRefParser implements InlineParserInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var TextNormalizerInterface */ |
|
27
|
|
|
private $slugNormalizer; |
|
28
|
|
|
|
|
29
|
42 |
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
42 |
|
$this->slugNormalizer = new SlugNormalizer(); |
|
32
|
42 |
|
} |
|
33
|
|
|
|
|
34
|
42 |
|
public function getCharacters(): array |
|
35
|
|
|
{ |
|
36
|
42 |
|
return ['^']; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
18 |
|
public function parse(InlineParserContext $inlineContext): bool |
|
40
|
|
|
{ |
|
41
|
18 |
|
$container = $inlineContext->getContainer(); |
|
42
|
18 |
|
$cursor = $inlineContext->getCursor(); |
|
43
|
18 |
|
$nextChar = $cursor->peek(); |
|
44
|
18 |
|
if ($nextChar !== '[') { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
18 |
|
$state = $cursor->saveState(); |
|
48
|
|
|
|
|
49
|
18 |
|
$m = $cursor->match('/\^\[[^\n^\]]+\]/'); |
|
50
|
18 |
|
if ($m !== null) { |
|
51
|
18 |
|
if (\preg_match('#\^\[([^\]]+)\]#', $m, $matches) > 0) { |
|
52
|
18 |
|
$reference = $this->createReference($matches[1]); |
|
53
|
18 |
|
$container->appendChild(new FootnoteRef($reference, $matches[1])); |
|
54
|
|
|
|
|
55
|
18 |
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$cursor->restoreState($state); |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
18 |
|
private function createReference(string $label): Reference |
|
65
|
|
|
{ |
|
66
|
18 |
|
$refLabel = $this->slugNormalizer->normalize($label); |
|
67
|
18 |
|
$refLabel = \mb_substr($refLabel, 0, 20); |
|
68
|
|
|
|
|
69
|
18 |
|
return new Reference($refLabel, '#fn:' . $refLabel, $label); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|