|
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\Parser\Inline\InlineParserInterface; |
|
21
|
|
|
use League\CommonMark\Parser\Inline\InlineParserMatch; |
|
22
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
|
23
|
|
|
use League\CommonMark\Reference\Reference; |
|
24
|
|
|
|
|
25
|
|
|
final class FootnoteRefParser implements InlineParserInterface, ConfigurationAwareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var ConfigurationInterface */ |
|
28
|
|
|
private $config; |
|
29
|
|
|
|
|
30
|
105 |
|
public function getMatchDefinition(): InlineParserMatch |
|
31
|
|
|
{ |
|
32
|
105 |
|
return InlineParserMatch::regex('\[\^([^\s\]]+)\]'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
75 |
|
public function parse(string $match, InlineParserContext $inlineContext): bool |
|
36
|
|
|
{ |
|
37
|
75 |
|
if (\preg_match('#\[\^([^\s\]]+)\]#', $match, $matches) <= 0) { |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
75 |
|
$inlineContext->getCursor()->advanceBy(\mb_strlen($match)); |
|
42
|
75 |
|
$inlineContext->getContainer()->appendChild(new FootnoteRef($this->createReference($matches[1]))); |
|
43
|
|
|
|
|
44
|
75 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
75 |
|
private function createReference(string $label): Reference |
|
48
|
|
|
{ |
|
49
|
75 |
|
return new Reference( |
|
50
|
50 |
|
$label, |
|
51
|
75 |
|
'#' . $this->config->get('footnote/footnote_id_prefix', 'fn:') . $label, |
|
52
|
25 |
|
$label |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
105 |
|
public function setConfiguration(ConfigurationInterface $config): void |
|
57
|
|
|
{ |
|
58
|
105 |
|
$this->config = $config; |
|
59
|
105 |
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|