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\Renderer; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteBackref; |
18
|
|
|
use League\CommonMark\Node\Node; |
19
|
|
|
use League\CommonMark\Renderer\ChildNodeRendererInterface; |
20
|
|
|
use League\CommonMark\Renderer\NodeRendererInterface; |
21
|
|
|
use League\CommonMark\Util\HtmlElement; |
22
|
|
|
use League\CommonMark\Xml\XmlNodeRendererInterface; |
23
|
|
|
use League\Config\ConfigurationAwareInterface; |
24
|
|
|
use League\Config\ConfigurationInterface; |
25
|
|
|
|
26
|
|
|
final class FootnoteBackrefRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface |
27
|
|
|
{ |
28
|
|
|
public const DEFAULT_SYMBOL = '↩'; |
29
|
|
|
|
30
|
|
|
/** @var ConfigurationInterface */ |
31
|
|
|
private $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param FootnoteBackref $node |
35
|
|
|
* |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
* |
38
|
|
|
* @psalm-suppress MoreSpecificImplementedParamType |
39
|
|
|
*/ |
40
|
105 |
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer) |
41
|
|
|
{ |
42
|
105 |
|
FootnoteBackref::assertInstanceOf($node); |
43
|
|
|
|
44
|
105 |
|
$attrs = $node->data->getData('attributes'); |
45
|
|
|
|
46
|
105 |
|
$attrs->append('class', $this->config->get('footnote/backref_class')); |
47
|
105 |
|
$attrs->set('rev', 'footnote'); |
48
|
105 |
|
$attrs->set('href', \mb_strtolower($node->getReference()->getDestination())); |
49
|
105 |
|
$attrs->set('role', 'doc-backlink'); |
50
|
|
|
|
51
|
105 |
|
$symbol = $this->config->get('footnote/backref_symbol'); |
52
|
|
|
\assert(\is_string($symbol)); |
53
|
|
|
|
54
|
105 |
|
return ' ' . new HtmlElement('a', $attrs->export(), \htmlspecialchars($symbol), true); |
55
|
|
|
} |
56
|
|
|
|
57
|
144 |
|
public function setConfiguration(ConfigurationInterface $configuration): void |
58
|
|
|
{ |
59
|
144 |
|
$this->config = $configuration; |
60
|
144 |
|
} |
61
|
|
|
|
62
|
27 |
|
public function getXmlTagName(Node $node): string |
63
|
|
|
{ |
64
|
27 |
|
return 'footnote_backref'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param FootnoteBackref $node |
69
|
|
|
* |
70
|
|
|
* @return array<string, scalar> |
71
|
|
|
* |
72
|
|
|
* @psalm-suppress MoreSpecificImplementedParamType |
73
|
|
|
*/ |
74
|
27 |
|
public function getXmlAttributes(Node $node): array |
75
|
|
|
{ |
76
|
27 |
|
FootnoteBackref::assertInstanceOf($node); |
77
|
|
|
|
78
|
|
|
return [ |
79
|
27 |
|
'reference' => $node->getReference()->getLabel(), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|