xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/Renderer/Inline/FootnoteRefRenderer.php (revision b0a36678775785ae4bed10dd2dcf9b3c90beb0c1)
1f46768a8SSungbin Jeon<?php
2f46768a8SSungbin Jeon
3f46768a8SSungbin Jeon/*
4f46768a8SSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
5f46768a8SSungbin Jeon *
6f46768a8SSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com>
7f46768a8SSungbin Jeon *
8f46768a8SSungbin Jeon * Original code based on the followings:
9f46768a8SSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
10f46768a8SSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
11f46768a8SSungbin Jeon *
12f46768a8SSungbin Jeon * For the full copyright and license information, please view the LICENSE
13f46768a8SSungbin Jeon * file that was distributed with this source code.
14f46768a8SSungbin Jeon */
15f46768a8SSungbin Jeon
16f46768a8SSungbin Jeondeclare(strict_types=1);
17f46768a8SSungbin Jeon
18f46768a8SSungbin Jeonnamespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline;
19f46768a8SSungbin Jeon
20*b0a36678SSungbin Jeonuse League\CommonMark\Renderer\ChildNodeRendererInterface;
21f46768a8SSungbin Jeonuse League\CommonMark\Extension\Footnote\Node\FootnoteRef;
22f46768a8SSungbin Jeonuse League\CommonMark\Extension\Footnote\Node\Footnote;
23*b0a36678SSungbin Jeonuse League\CommonMark\Node\Node;
2494a075eeSSungbin Jeonuse League\CommonMark\Renderer\NodeRendererInterface;
25*b0a36678SSungbin Jeonuse League\Config\ConfigurationAwareInterface;
26*b0a36678SSungbin Jeonuse League\Config\ConfigurationInterface;
27f46768a8SSungbin Jeon
2894a075eeSSungbin Jeonfinal class FootnoteRefRenderer implements NodeRendererInterface, ConfigurationAwareInterface
29f46768a8SSungbin Jeon{
30f46768a8SSungbin Jeon    /** @var ConfigurationInterface */
31*b0a36678SSungbin Jeon    private ConfigurationInterface $config;
32f46768a8SSungbin Jeon
33*b0a36678SSungbin Jeon    public function render(Node $node, ChildNodeRendererInterface $DWRenderer)
34f46768a8SSungbin Jeon    {
35*b0a36678SSungbin Jeon        FootnoteRef::assertInstanceOf($node);
36f46768a8SSungbin Jeon
37*b0a36678SSungbin Jeon        $attrs = $node->data->getData('attributes');
38f46768a8SSungbin Jeon
39eff27c68SSungbin Jeon        # get parents iteratively until get top-level document
40*b0a36678SSungbin Jeon        $document = $node->parent()->parent();
41*b0a36678SSungbin Jeon        while (get_class($document)!='League\CommonMark\Node\Block\Document'){
42eff27c68SSungbin Jeon            $document = $document->parent();
43eff27c68SSungbin Jeon        }
44f46768a8SSungbin Jeon        $walker = $document->walker();
45*b0a36678SSungbin Jeon        $title = $node->getReference()->getLabel();
46f46768a8SSungbin Jeon
47f46768a8SSungbin Jeon        while ($event = $walker->next()) {
48f46768a8SSungbin Jeon            $node = $event->getNode();
49f46768a8SSungbin Jeon            if ($node instanceof Footnote && $title == $node->getReference()->getLabel()) {
50*b0a36678SSungbin Jeon                $text = $DWRenderer->renderNode($node->children()[0]);
51f46768a8SSungbin Jeon                break;
52f46768a8SSungbin Jeon            }
53f46768a8SSungbin Jeon        }
54f46768a8SSungbin Jeon
55f46768a8SSungbin Jeon        $result = '(('. $text. '))';
56f46768a8SSungbin Jeon        return $result;
57f46768a8SSungbin Jeon
58f46768a8SSungbin Jeon    }
59f46768a8SSungbin Jeon
60*b0a36678SSungbin Jeon    public function setConfiguration(ConfigurationInterface $configuration): void
61f46768a8SSungbin Jeon    {
62f46768a8SSungbin Jeon        $this->config = $configuration;
63f46768a8SSungbin Jeon    }
64f46768a8SSungbin Jeon}
65