xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/Renderer/Block/FencedCodeRenderer.php (revision b0a36678775785ae4bed10dd2dcf9b3c90beb0c1)
18ec9a8f2SSungbin Jeon<?php
28ec9a8f2SSungbin Jeon
38ec9a8f2SSungbin Jeon/*
48ec9a8f2SSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
58ec9a8f2SSungbin Jeon *
68ec9a8f2SSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com>
78ec9a8f2SSungbin Jeon *
88ec9a8f2SSungbin Jeon * Original code based on the followings:
98ec9a8f2SSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
108ec9a8f2SSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
118ec9a8f2SSungbin Jeon *
128ec9a8f2SSungbin Jeon * For the full copyright and license information, please view the LICENSE
138ec9a8f2SSungbin Jeon * file that was distributed with this source code.
148ec9a8f2SSungbin Jeon */
158ec9a8f2SSungbin Jeon
168ec9a8f2SSungbin Jeonnamespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Block;
178ec9a8f2SSungbin Jeon
1894a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
1994a075eeSSungbin Jeonuse League\CommonMark\Node\Node;
2094a075eeSSungbin Jeonuse League\CommonMark\Renderer\ChildNodeRendererInterface;
2194a075eeSSungbin Jeonuse League\CommonMark\Renderer\NodeRendererInterface;
228ec9a8f2SSungbin Jeonuse League\CommonMark\Util\Xml;
2394a075eeSSungbin Jeonuse League\CommonMark\Xml\XmlNodeRendererInterface;
248ec9a8f2SSungbin Jeon
2594a075eeSSungbin Jeonfinal class FencedCodeRenderer implements NodeRendererInterface
268ec9a8f2SSungbin Jeon{
278ec9a8f2SSungbin Jeon    /**
288ec9a8f2SSungbin Jeon     * @param FencedCode               $block
29*b0a36678SSungbin Jeon     * @param ChildNodeRendererInterface $DWRenderer
308ec9a8f2SSungbin Jeon     * @param bool                     $inTightList
318ec9a8f2SSungbin Jeon     *
3204312fe3SSungbin Jeon     * @return string
338ec9a8f2SSungbin Jeon     */
34*b0a36678SSungbin Jeon    public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string
358ec9a8f2SSungbin Jeon    {
3694a075eeSSungbin Jeon        FencedCode::assertInstanceOf($node);
378ec9a8f2SSungbin Jeon
3894a075eeSSungbin Jeon        $attrs = $node->data->getData('attributes');
398ec9a8f2SSungbin Jeon
4094a075eeSSungbin Jeon        $infoWords = $node->getInfoWords();
418ec9a8f2SSungbin Jeon
42962c1a8eSSungbin Jeon        # for default value not specifying infoword
43962c1a8eSSungbin Jeon        $entertag = 'code';
44962c1a8eSSungbin Jeon        $exittag = 'code';
45962c1a8eSSungbin Jeon
468ec9a8f2SSungbin Jeon        if (\count($infoWords) !== 0 && \strlen($infoWords[0]) !== 0) {
4704312fe3SSungbin Jeon            switch($infoWords[0]) {
4804312fe3SSungbin Jeon                case 'html':
498ec9a8f2SSungbin Jeon                    # only supports html block; it is not possible for express html inline span in Commonmark syntax
508ec9a8f2SSungbin Jeon                    $entertag = 'HTML';
518ec9a8f2SSungbin Jeon                    $exittag = 'HTML';
5204312fe3SSungbin Jeon                    break;
5304312fe3SSungbin Jeon                case 'nowiki':
5404312fe3SSungbin Jeon                    # DW <nowiki> syntax
558ec9a8f2SSungbin Jeon                    $entertag = $infoWords[0];
568ec9a8f2SSungbin Jeon                    $exittag = $infoWords[0];
5704312fe3SSungbin Jeon                    break;
5804312fe3SSungbin Jeon                case 'dokuwiki':
5904312fe3SSungbin Jeon                    # passing DW codes (e.g. tag, struct, etc.)
6004312fe3SSungbin Jeon                    $entertag = '';
6104312fe3SSungbin Jeon                    $exittag = '';
6204312fe3SSungbin Jeon                    break;
6304312fe3SSungbin Jeon                default:
648ec9a8f2SSungbin Jeon                    $entertag = 'code ' . $infoWords[0];
658ec9a8f2SSungbin Jeon                    $exittag = 'code';
668ec9a8f2SSungbin Jeon            }
678ec9a8f2SSungbin Jeon        }
68962c1a8eSSungbin Jeon
696d4c93f0SSungbin Jeon        # Do not escape code block; BELIEVE DOKUWIKI!
70*b0a36678SSungbin Jeon        #$result = Xml::escape($node->getStringContent());
71*b0a36678SSungbin Jeon        $result = $node->getLiteral();
7204312fe3SSungbin Jeon        if ($entertag):
7304312fe3SSungbin Jeon            $result = '<' . $entertag . ">\n" . $result . "</" . $exittag . ">";
7436bc9d52SSungbin Jeon        endif;
758ec9a8f2SSungbin Jeon        return $result;
768ec9a8f2SSungbin Jeon    }
778ec9a8f2SSungbin Jeon}
78