xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/Renderer/Inline/FootnoteRefRenderer.php (revision f46768a887e7e143524724311dbc994e6124236c)
1<?php
2
3/*
4 * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
5 *
6 * (c) Sungbin Jeon <clockoon@gmail.com>
7 *
8 * Original code based on the followings:
9 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
10 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
11 *
12 * For the full copyright and license information, please view the LICENSE
13 * file that was distributed with this source code.
14 */
15
16declare(strict_types=1);
17
18namespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline;
19
20use League\CommonMark\ElementRendererInterface;
21use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
22use League\CommonMark\Extension\Footnote\Node\Footnote;
23use League\CommonMark\Inline\Element\AbstractInline;
24use League\CommonMark\Inline\Renderer\InlineRendererInterface;
25use League\CommonMark\Util\ConfigurationAwareInterface;
26use League\CommonMark\Util\ConfigurationInterface;
27
28final class FootnoteRefRenderer implements InlineRendererInterface, ConfigurationAwareInterface
29{
30    /** @var ConfigurationInterface */
31    private $config;
32
33    public function render(AbstractInline $inline, ElementRendererInterface $DWRenderer)
34    {
35
36        if (!($inline instanceof FootnoteRef)) {
37            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
38        }
39
40        $attrs = $inline->getData('attributes', []);
41
42        $document = $inline->parent()->parent();
43        $walker = $document->walker();
44        $title = $inline->getReference()->getTitle();
45
46        while ($event = $walker->next()) {
47            $node = $event->getNode();
48            if ($node instanceof Footnote && $title == $node->getReference()->getLabel()) {
49                $text = $DWRenderer->renderBlock($node->children()[0]);
50                break;
51            }
52        }
53
54        $result = '(('. $text. '))';
55        return $result;
56
57    }
58
59    public function setConfiguration(ConfigurationInterface $configuration)
60    {
61        $this->config = $configuration;
62    }
63}
64