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