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\Configuration\ConfigurationAwareInterface; |
18
|
|
|
use League\CommonMark\Configuration\ConfigurationInterface; |
19
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteRef; |
20
|
|
|
use League\CommonMark\Normalizer\SlugNormalizer; |
21
|
|
|
use League\CommonMark\Normalizer\TextNormalizerInterface; |
22
|
|
|
use League\CommonMark\Parser\Inline\InlineParserInterface; |
23
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
24
|
|
|
use League\CommonMark\Reference\Reference; |
25
|
|
|
|
26
|
|
|
final class AnonymousFootnoteRefParser implements InlineParserInterface, ConfigurationAwareInterface |
27
|
|
|
{ |
28
|
|
|
/** @var ConfigurationInterface */ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TextNormalizerInterface |
33
|
|
|
* |
34
|
|
|
* @psalm-readonly |
35
|
|
|
*/ |
36
|
|
|
private $slugNormalizer; |
37
|
|
|
|
38
|
96 |
|
public function __construct() |
39
|
|
|
{ |
40
|
96 |
|
$this->slugNormalizer = new SlugNormalizer(); |
41
|
96 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
96 |
|
public function getCharacters(): array |
47
|
|
|
{ |
48
|
96 |
|
return ['^']; |
49
|
|
|
} |
50
|
|
|
|
51
|
27 |
|
public function parse(InlineParserContext $inlineContext): bool |
52
|
|
|
{ |
53
|
27 |
|
$container = $inlineContext->getContainer(); |
54
|
27 |
|
$cursor = $inlineContext->getCursor(); |
55
|
27 |
|
$nextChar = $cursor->peek(); |
56
|
27 |
|
if ($nextChar !== '[') { |
57
|
6 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
21 |
|
$state = $cursor->saveState(); |
61
|
|
|
|
62
|
21 |
|
$m = $cursor->match('#\^\[[^\]]+\]#'); |
63
|
21 |
|
if ($m !== null) { |
64
|
21 |
|
if (\preg_match('#\^\[([^\]]+)\]#', $m, $matches) > 0) { |
65
|
21 |
|
$reference = $this->createReference($matches[1]); |
66
|
21 |
|
$container->appendChild(new FootnoteRef($reference, $matches[1])); |
67
|
|
|
|
68
|
21 |
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$cursor->restoreState($state); |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @psalm-immutable |
79
|
|
|
*/ |
80
|
21 |
|
private function createReference(string $label): Reference |
81
|
|
|
{ |
82
|
21 |
|
$refLabel = $this->slugNormalizer->normalize($label); |
83
|
21 |
|
$refLabel = \mb_substr($refLabel, 0, 20); |
84
|
|
|
|
85
|
21 |
|
return new Reference( |
86
|
14 |
|
$refLabel, |
87
|
21 |
|
'#' . $this->config->get('footnote/footnote_id_prefix', 'fn:') . $refLabel, |
88
|
7 |
|
$label |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
96 |
|
public function setConfiguration(ConfigurationInterface $config): void |
93
|
|
|
{ |
94
|
96 |
|
$this->config = $config; |
95
|
96 |
|
} |
96
|
|
|
} |
97
|
|
|
|