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\Event; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
18
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteRef; |
19
|
|
|
use League\CommonMark\Reference\Reference; |
20
|
|
|
|
21
|
|
|
final class NumberFootnotesListener |
22
|
|
|
{ |
23
|
42 |
|
public function onDocumentParsed(DocumentParsedEvent $event): void |
24
|
|
|
{ |
25
|
42 |
|
$document = $event->getDocument(); |
26
|
42 |
|
$walker = $document->walker(); |
27
|
42 |
|
$nextCounter = 1; |
28
|
42 |
|
$usedLabels = []; |
29
|
42 |
|
$usedCounters = []; |
30
|
|
|
|
31
|
42 |
|
while ($event = $walker->next()) { |
32
|
42 |
|
if (!$event->isEntering()) { |
33
|
42 |
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
42 |
|
$node = $event->getNode(); |
37
|
|
|
|
38
|
42 |
|
if (!$node instanceof FootnoteRef) { |
39
|
42 |
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
36 |
|
$existingReference = $node->getReference(); |
43
|
36 |
|
$label = $existingReference->getLabel(); |
44
|
36 |
|
$counter = $nextCounter; |
45
|
36 |
|
$canIncrementCounter = true; |
46
|
|
|
|
47
|
36 |
|
if (\array_key_exists($label, $usedLabels)) { |
48
|
|
|
/* |
49
|
|
|
* Reference is used again, we need to point |
50
|
|
|
* to the same footnote. But with a different ID |
51
|
|
|
*/ |
52
|
6 |
|
$counter = $usedCounters[$label]; |
53
|
6 |
|
$label = $label . '__' . ++$usedLabels[$label]; |
54
|
6 |
|
$canIncrementCounter = false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// rewrite reference title to use a numeric link |
58
|
36 |
|
$newReference = new Reference( |
59
|
36 |
|
$label, |
60
|
36 |
|
$existingReference->getDestination(), |
61
|
36 |
|
(string) $counter |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
// Override reference with numeric link |
65
|
36 |
|
$node->setReference($newReference); |
66
|
36 |
|
$document->getReferenceMap()->addReference($newReference); |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* Store created references in document for |
70
|
|
|
* creating FootnoteBackrefs |
71
|
|
|
*/ |
72
|
36 |
|
if (false === $document->getData($existingReference->getDestination(), false)) { |
73
|
36 |
|
$document->data[$existingReference->getDestination()] = []; |
74
|
|
|
} |
75
|
|
|
|
76
|
36 |
|
$document->data[$existingReference->getDestination()][] = $newReference; |
77
|
|
|
|
78
|
36 |
|
$usedLabels[$label] = 1; |
79
|
36 |
|
$usedCounters[$label] = $nextCounter; |
80
|
|
|
|
81
|
36 |
|
if ($canIncrementCounter) { |
82
|
36 |
|
$nextCounter++; |
83
|
|
|
} |
84
|
|
|
} |
85
|
42 |
|
} |
86
|
|
|
} |
87
|
|
|
|