|
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\Configuration\ConfigurationAwareInterface; |
|
18
|
|
|
use League\CommonMark\Configuration\ConfigurationInterface; |
|
19
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
|
20
|
|
|
use League\CommonMark\Extension\Footnote\Node\Footnote; |
|
21
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteBackref; |
|
22
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteRef; |
|
23
|
|
|
use League\CommonMark\Node\Block\Paragraph; |
|
24
|
|
|
use League\CommonMark\Node\Inline\Text; |
|
25
|
|
|
use League\CommonMark\Reference\Reference; |
|
26
|
|
|
|
|
27
|
|
|
final class AnonymousFootnotesListener implements ConfigurationAwareInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var ConfigurationInterface */ |
|
30
|
|
|
private $config; |
|
31
|
|
|
|
|
32
|
96 |
|
public function onDocumentParsed(DocumentParsedEvent $event): void |
|
33
|
|
|
{ |
|
34
|
96 |
|
$document = $event->getDocument(); |
|
35
|
96 |
|
$walker = $document->walker(); |
|
36
|
|
|
|
|
37
|
96 |
|
while ($event = $walker->next()) { |
|
38
|
96 |
|
if (! $event->isEntering()) { |
|
39
|
96 |
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
96 |
|
$node = $event->getNode(); |
|
43
|
96 |
|
if (! $node instanceof FootnoteRef || ($text = $node->getContent()) === null) { |
|
44
|
96 |
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Anonymous footnote needs to create a footnote from its content |
|
48
|
21 |
|
$existingReference = $node->getReference(); |
|
49
|
21 |
|
$newReference = new Reference( |
|
50
|
21 |
|
$existingReference->getLabel(), |
|
51
|
21 |
|
'#' . $this->config->get('footnote/ref_id_prefix', 'fnref:') . $existingReference->getLabel(), |
|
52
|
21 |
|
$existingReference->getTitle() |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
21 |
|
$paragraph = new Paragraph(); |
|
56
|
21 |
|
$paragraph->appendChild(new Text($text)); |
|
57
|
21 |
|
$paragraph->appendChild(new FootnoteBackref($newReference)); |
|
58
|
|
|
|
|
59
|
21 |
|
$footnote = new Footnote($newReference); |
|
60
|
21 |
|
$footnote->appendChild($paragraph); |
|
61
|
|
|
|
|
62
|
21 |
|
$document->appendChild($footnote); |
|
63
|
|
|
} |
|
64
|
96 |
|
} |
|
65
|
|
|
|
|
66
|
96 |
|
public function setConfiguration(ConfigurationInterface $config): void |
|
67
|
|
|
{ |
|
68
|
96 |
|
$this->config = $config; |
|
69
|
96 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|