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\FencedCode;
19use League\CommonMark\Node\Node;
20use League\CommonMark\Renderer\ChildNodeRendererInterface;
21use League\CommonMark\Renderer\NodeRendererInterface;
22use League\CommonMark\Util\Xml;
23use League\CommonMark\Xml\XmlNodeRendererInterface;
24
25final class FencedCodeRenderer implements NodeRendererInterface
26{
27    /**
28     * @param FencedCode               $block
29     * @param ChildNodeRendererInterface $DWRenderer
30     * @param bool                     $inTightList
31     *
32     * @return string
33     */
34    public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string
35    {
36        FencedCode::assertInstanceOf($node);
37
38        $attrs = $node->data->getData('attributes');
39
40        $infoWords = $node->getInfoWords();
41
42        # for default value not specifying infoword
43        $entertag = 'code';
44        $exittag = 'code';
45
46        if (\count($infoWords) !== 0 && \strlen($infoWords[0]) !== 0) {
47            switch($infoWords[0]) {
48                case 'html':
49                    # only supports html block; it is not possible for express html inline span in Commonmark syntax
50                    $entertag = 'HTML';
51                    $exittag = 'HTML';
52                    break;
53                case 'nowiki':
54                    # DW <nowiki> syntax
55                    $entertag = $infoWords[0];
56                    $exittag = $infoWords[0];
57                    break;
58                case 'dokuwiki':
59                    # passing DW codes (e.g. tag, struct, etc.)
60                    $entertag = '';
61                    $exittag = '';
62                    break;
63                default:
64                    $entertag = 'code ' . $infoWords[0];
65                    $exittag = 'code';
66            }
67        }
68
69        # Do not escape code block; BELIEVE DOKUWIKI!
70        #$result = Xml::escape($node->getStringContent());
71        $result = $node->getLiteral();
72        if ($entertag):
73            $result = '<' . $entertag . ">\n" . $result . "</" . $exittag . ">";
74        endif;
75        return $result;
76    }
77}
78