1<?php
2
3declare(strict_types=1);
4
5/*
6 * This is part of the league/commonmark package.
7 *
8 * (c) Martin Hasoň <martin.hason@gmail.com>
9 * (c) Webuni s.r.o. <info@webuni.cz>
10 * (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 League\CommonMark\Extension\Table;
17
18use League\CommonMark\Environment\EnvironmentBuilderInterface;
19use League\CommonMark\Extension\ConfigurableExtensionInterface;
20use League\CommonMark\Renderer\HtmlDecorator;
21use League\Config\ConfigurationBuilderInterface;
22use Nette\Schema\Expect;
23
24final class TableExtension implements ConfigurableExtensionInterface
25{
26    public function configureSchema(ConfigurationBuilderInterface $builder): void
27    {
28        $attributeArraySchema = Expect::arrayOf(
29            Expect::type('string|string[]|bool'), // attribute value(s)
30            'string' // attribute name
31        )->mergeDefaults(false);
32
33        $builder->addSchema('table', Expect::structure([
34            'wrap' => Expect::structure([
35                'enabled' => Expect::bool()->default(false),
36                'tag' => Expect::string()->default('div'),
37                'attributes' => Expect::arrayOf(Expect::string()),
38            ]),
39            'alignment_attributes' => Expect::structure([
40                'left' => (clone $attributeArraySchema)->default(['align' => 'left']),
41                'center' => (clone $attributeArraySchema)->default(['align' => 'center']),
42                'right' => (clone $attributeArraySchema)->default(['align' => 'right']),
43            ]),
44        ]));
45    }
46
47    public function register(EnvironmentBuilderInterface $environment): void
48    {
49        $tableRenderer = new TableRenderer();
50        if ($environment->getConfiguration()->get('table/wrap/enabled')) {
51            $tableRenderer = new HtmlDecorator($tableRenderer, $environment->getConfiguration()->get('table/wrap/tag'), $environment->getConfiguration()->get('table/wrap/attributes'));
52        }
53
54        $environment
55            ->addBlockStartParser(new TableStartParser())
56
57            ->addRenderer(Table::class, $tableRenderer)
58            ->addRenderer(TableSection::class, new TableSectionRenderer())
59            ->addRenderer(TableRow::class, new TableRowRenderer())
60            ->addRenderer(TableCell::class, new TableCellRenderer($environment->getConfiguration()->get('table/alignment_attributes')));
61    }
62}
63