1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
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
13declare(strict_types=1);
14
15namespace League\CommonMark\Extension\Footnote\Renderer;
16
17use League\CommonMark\Block\Element\AbstractBlock;
18use League\CommonMark\Block\Renderer\BlockRendererInterface;
19use League\CommonMark\ElementRendererInterface;
20use League\CommonMark\Extension\Footnote\Node\FootnoteContainer;
21use League\CommonMark\HtmlElement;
22use League\CommonMark\Util\ConfigurationAwareInterface;
23use League\CommonMark\Util\ConfigurationInterface;
24
25final class FootnoteContainerRenderer implements BlockRendererInterface, ConfigurationAwareInterface
26{
27    /** @var ConfigurationInterface */
28    private $config;
29
30    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
31    {
32        if (!($block instanceof FootnoteContainer)) {
33            throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
34        }
35
36        $attrs = $block->getData('attributes', []);
37        $attrs['class'] = $attrs['class'] ?? $this->config->get('footnote/container_class', 'footnotes');
38        $attrs['role'] = 'doc-endnotes';
39
40        $contents = new HtmlElement('ol', [], $htmlRenderer->renderBlocks($block->children()));
41        if ($this->config->get('footnote/container_add_hr', true)) {
42            $contents = [new HtmlElement('hr', [], null, true), $contents];
43        }
44
45        return new HtmlElement('div', $attrs, $contents);
46    }
47
48    public function setConfiguration(ConfigurationInterface $configuration)
49    {
50        $this->config = $configuration;
51    }
52}
53