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\ListBlock; 20use League\CommonMark\ElementRendererInterface; 21use League\CommonMark\HtmlElement; 22use League\CommonMark\Block\Renderer\BlockRendererInterface; 23 24final class ListBlockRenderer implements BlockRendererInterface 25{ 26 /** 27 * @param ListBlock $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 ListBlock)) { 36 throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block)); 37 } 38 39 $listData = $block->getListData(); 40 41 $tag = $listData->type === ListBlock::TYPE_BULLET ? "* " : "- "; 42 43 $attrs = $block->getData('attributes', []); 44 45 if ($listData->start !== null && $listData->start !== 1) { 46 $attrs['start'] = (string) $listData->start; 47 } 48 49 $result = 50 $DWRenderer->renderBlocks( 51 $block->children(), 52 $block->isTight() 53 ); 54 55 $result = preg_replace("/\n/", "\n ", $result); # add two-space indentation 56 $result = preg_replace("/\n(\s\s)+\n/", "\n", $result); # remove unwanted newline 57 $result = preg_replace("/<li>/", $tag, $result); # add DW list bullet 58 return " " . $result; 59 60 } 61} 62