xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/Renderer/Block/BlockQuoteRenderer.php (revision 8ec9a8f2fa8d03e80395a040a2626880092b4ddf)
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
16namespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Block;
17
18use League\CommonMark\Block\Element\AbstractBlock;
19use League\CommonMark\Block\Element\BlockQuote;
20use League\CommonMark\ElementRendererInterface;
21use League\CommonMark\HtmlElement;
22use League\CommonMark\Block\Renderer\BlockRendererInterface;
23
24final class BlockQuoteRenderer implements BlockRendererInterface
25{
26    /**
27     * @param BlockQuote               $block
28     * @param ElementRendererInterface $htmlRenderer
29     * @param bool                     $inTightList
30     *
31     * @return HtmlElement
32     */
33    public function render(AbstractBlock $block, ElementRendererInterface $DWRenderer, bool $inTightList = false)
34    {
35        if (!($block instanceof BlockQuote)) {
36            throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
37        }
38
39        $attrs = $block->getData('attributes', []);
40
41        $filling = $DWRenderer->renderBlocks($block->children());
42        $filling = preg_replace('/\n/', "\n>", $filling);
43
44        if ($filling === '') {
45            return '>' . $DWRenderer->getOption('inner_separator', "\n");
46            //return new HtmlElement('blockquote', $attrs, $DWRenderer->getOption('inner_separator', "\n"));
47        }
48
49        return '>' . $filling . $DWRenderer->getOption('inner_separator', "\n");
50        //return new HtmlElement(
51        //    'blockquote',
52        //    $attrs,
53        //    $DWRenderer->getOption('inner_separator', "\n") . $filling . $DWRenderer->getOption('inner_separator', "\n")
54        //);
55    }
56}
57