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\Block\Element\Paragraph; |
18
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
19
|
|
|
use League\CommonMark\Extension\Footnote\Node\Footnote; |
20
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteBackref; |
21
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteRef; |
22
|
|
|
use League\CommonMark\Inline\Element\Text; |
23
|
|
|
use League\CommonMark\Reference\Reference; |
24
|
|
|
|
25
|
|
|
final class AnonymousFootnotesListener |
26
|
|
|
{ |
27
|
42 |
|
public function onDocumentParsed(DocumentParsedEvent $event): void |
28
|
|
|
{ |
29
|
42 |
|
$document = $event->getDocument(); |
30
|
42 |
|
$walker = $document->walker(); |
31
|
|
|
|
32
|
42 |
|
while ($event = $walker->next()) { |
33
|
42 |
|
$node = $event->getNode(); |
34
|
42 |
|
if ($node instanceof FootnoteRef && $event->isEntering() && null !== $text = $node->getContent()) { |
35
|
|
|
// Anonymous footnote needs to create a footnote from its content |
36
|
18 |
|
$existingReference = $node->getReference(); |
37
|
18 |
|
$reference = new Reference( |
38
|
18 |
|
$existingReference->getLabel(), |
39
|
18 |
|
'#fnref:' . $existingReference->getLabel(), |
40
|
18 |
|
$existingReference->getTitle() |
41
|
|
|
); |
42
|
18 |
|
$footnote = new Footnote($reference); |
43
|
18 |
|
$footnote->addBackref(new FootnoteBackref($reference)); |
44
|
18 |
|
$paragraph = new Paragraph(); |
45
|
18 |
|
$paragraph->appendChild(new Text($text)); |
46
|
18 |
|
$footnote->appendChild($paragraph); |
47
|
18 |
|
$document->appendChild($footnote); |
48
|
|
|
} |
49
|
|
|
} |
50
|
42 |
|
} |
51
|
|
|
} |
52
|
|
|
|