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\Reference\Reference; |
21
|
|
|
|
22
|
|
|
final class AnonymousFootnoteRefParser implements InlineParserInterface |
23
|
|
|
{ |
24
|
42 |
|
public function getCharacters(): array |
25
|
|
|
{ |
26
|
42 |
|
return ['^']; |
27
|
|
|
} |
28
|
|
|
|
29
|
18 |
|
public function parse(InlineParserContext $inlineContext): bool |
30
|
|
|
{ |
31
|
18 |
|
$container = $inlineContext->getContainer(); |
32
|
18 |
|
$cursor = $inlineContext->getCursor(); |
33
|
18 |
|
$nextChar = $cursor->peek(); |
34
|
18 |
|
if ($nextChar !== '[') { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
18 |
|
$state = $cursor->saveState(); |
38
|
|
|
|
39
|
18 |
|
$m = $cursor->match('/\^\[[^\n^\]]+\]/'); |
40
|
18 |
|
if ($m !== null) { |
41
|
18 |
|
if (\preg_match('#\^\[([^\]]+)\]#', $m, $matches) > 0) { |
42
|
18 |
|
$reference = $this->createReference($matches[1]); |
43
|
18 |
|
$container->appendChild(new FootnoteRef($reference, $matches[1])); |
44
|
|
|
|
45
|
18 |
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$cursor->restoreState($state); |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
18 |
|
private function createReference(string $label): Reference |
55
|
|
|
{ |
56
|
18 |
|
$refLabel = Reference::normalizeReference($label); |
57
|
18 |
|
$refLabel = \mb_strtolower(\str_replace(' ', '-', $refLabel)); |
58
|
18 |
|
$refLabel = \substr($refLabel, 0, 20); |
59
|
|
|
|
60
|
18 |
|
return new Reference($refLabel, '#fn:' . $refLabel, $label); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|