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\Extension\CommonMark\Node\Block\ListBlock;
19use League\CommonMark\Node\Node;
20use League\CommonMark\Renderer\ChildNodeRendererInterface;
21use League\CommonMark\Renderer\NodeRendererInterface;
22use League\CommonMark\Xml\XmlNodeRendererInterface;
23
24final class ListBlockRenderer implements NodeRendererInterface
25{
26    /**
27     * @param ListBlock                $block
28     * @param ChildNodeRendererInterface $DWRenderer
29     * @param bool                     $inTightList
30     *
31     * @return string
32     */
33    public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string
34    {
35        ListBlock::assertInstanceOf($node);
36
37        $listData = $node->getListData();
38        $tag = $listData->type === ListBlock::TYPE_BULLET ? "* " : "- ";
39
40        $attrs = $node->data->get('attributes');
41
42        if ($listData->start !== null && $listData->start !== 1) {
43            $attrs['start'] = (string) $listData->start;
44        }
45
46        $result =
47                $DWRenderer->renderNodes(
48                    $node->children(),
49                    $node->isTight()
50                );
51
52        $result = preg_replace("/\n/", "\n  ", $result); # add two-space indentation
53        $result = preg_replace("/\n(\s\s)+\n/", "\n", $result); # remove unwanted newline
54        $result = preg_replace("/<li>/", $tag, $result); # add DW list bullet
55        return "  " . $result;
56
57    }
58}
59