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        # get parents iteratively until get top-level document
43        $document = $inline->parent()->parent();
44        while (get_class($document)!='League\CommonMark\Block\Element\Document'){
45            $document = $document->parent();
46        }
47        $walker = $document->walker();
48        $title = $inline->getReference()->getLabel();
49
50        while ($event = $walker->next()) {
51            $node = $event->getNode();
52            if ($node instanceof Footnote && $title == $node->getReference()->getLabel()) {
53                $text = $DWRenderer->renderBlock($node->children()[0]);
54                break;
55            }
56        }
57
58        $result = '(('. $text. '))';
59        return $result;
60
61    }
62
63    public function setConfiguration(ConfigurationInterface $configuration)
64    {
65        $this->config = $configuration;
66    }
67}
68