1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
7 *
8 * (c) Sungbin Jeon <clockoon@gmail.com>
9 *
10 * Original code based on the followings:
11 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
12 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
13 * - Commonmark Table extension  (c) Martin Hasoň <martin.hason@gmail.com>, Webuni s.r.o. <info@webuni.cz>, Colin O'Dell <colinodell@gmail.com>
14 *
15 * For the full copyright and license information, please view the LICENSE
16 * file that was distributed with this source code.
17 */
18
19namespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Block;
20
21use League\CommonMark\Node\Node;
22use League\CommonMark\Renderer\NodeRendererInterface;
23use League\CommonMark\Renderer\ChildNodeRendererInterface;
24use League\CommonMark\Extension\Table\TableCell;
25
26final class TableCellRenderer implements NodeRendererInterface
27{
28    public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string
29    {
30        TableCell::assertInstanceOf($node);
31
32        # block type indicator on DW
33        $separator = $node->getType() === TableCell::TYPE_HEADER ? '^' : '|';
34
35        # align indicator on DW
36        $lmargin = ' ';
37        $rmargin = ' ';
38        switch($node->getAlign()) {
39            case "right":
40                $lmargin = '  ';
41                break;
42            case "center":
43                $lmargin = '  ';
44                $rmargin = '  ';
45                break;
46        }
47
48        $result = $separator . $lmargin . $DWRenderer->renderNodes($node->children()) . $rmargin;
49        return $result;
50
51    }
52}
53