1<?php
2/*
3 * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
4 *
5 * (c) Sungbin Jeon <clockoon@gmail.com>
6 *
7 * Original code based on the followings:
8 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
9 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
10 *
11 * For the full copyright and license information, please view the LICENSE
12 * file that was distributed with this source code.
13 */
14
15namespace DokuWiki\Plugin\Commonmark\Extension;
16
17use League\CommonMark\Extension\ConfigurableExtensionInterface;
18use League\CommonMark\Environment\EnvironmentBuilderInterface;
19use League\CommonMark\Extension\CommonMark\Node\Block as BlockElement;
20use League\CommonMark\Extension\CommonMark\Parser\Block as BlockParser;
21use League\CommonMark\Node\Block as CoreBlockElement;
22use League\CommonMark\Node\Inline as CoreInlineElement;
23use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block as BlockRenderer;
24use League\CommonMark\Extension\CommonMark\Node\Inline as InlineElement;
25use League\CommonMark\Extension\CommonMark\Parser\Inline as InlineParser;
26use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline as InlineRenderer;
27use League\Config\ConfigurationBuilderInterface;
28use League\CommonMark\Extension\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor;
29use Nette\Schema\Expect;
30
31final class CommonMarkToDokuWikiExtension implements ConfigurableExtensionInterface {
32
33    public function configureSchema(ConfigurationBuilderInterface $builder): void
34    {
35        $builder->addSchema('commonmark', Expect::structure([
36            'use_asterisk' => Expect::bool(true),
37            'use_underscore' => Expect::bool(true),
38            'enable_strong' => Expect::bool(true),
39            'enable_em' => Expect::bool(true),
40            'unordered_list_markers' => Expect::listOf('string')->min(1)->default(['*', '+', '-'])->mergeDefaults(false),
41        ]));
42    }
43
44    public function register(EnvironmentBuilderInterface $environment): void {
45        $environment
46            ->addBlockStartParser(new BlockParser\BlockQuoteStartParser(),      70)
47            ->addBlockStartParser(new BlockParser\HeadingStartParser(),      60)
48            ->addBlockStartParser(new BlockParser\FencedCodeStartParser(),      50)
49            //->addBlockStartParser(new BlockParser\HtmlBlockParser(),       40) # No raw HTML processing on Commonmarkside
50            //->addBlockStartParser(new BlockParser\SetExtHeadingParser(),   30)
51            ->addBlockStartParser(new BlockParser\ThematicBreakStartParser(),   20)
52            ->addBlockStartParser(new BlockParser\ListBlockStartParser(),            10)
53            ->addBlockStartParser(new BlockParser\IndentedCodeStartParser(),  -100)
54            //->addBlockStartParser(new BlockParser\LazyParagraphParser(), -200)
55
56            //->addInlineParser(new InlineParser\NewlineParser(),     200)
57            ->addInlineParser(new InlineParser\BacktickParser(),    150)
58            ->addInlineParser(new InlineParser\EscapableParser(),    80)
59            ->addInlineParser(new InlineParser\EntityParser(),       70)
60            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
61            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
62            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
63            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
64            ->addInlineParser(new InlineParser\BangParser(),         10)
65
66            ->addRenderer(BlockElement\BlockQuote::class,    new BlockRenderer\BlockQuoteRenderer(),    0)
67            ->addRenderer(CoreBlockElement\Document::class,      new BlockRenderer\DocumentRenderer(),      0)
68            ->addRenderer(BlockElement\FencedCode::class,    new BlockRenderer\FencedCodeRenderer(),    0)
69            ->addRenderer(BlockElement\Heading::class,       new BlockRenderer\HeadingRenderer(),       0)
70            //->addRenderer(BlockElement\HtmlBlock::class,     new BlockRenderer\HtmlBlockRenderer(),     0) # No raw HTML processing on Commonmarkside
71            ->addRenderer(BlockElement\IndentedCode::class,  new BlockRenderer\IndentedCodeRenderer(),  0)
72            ->addRenderer(BlockElement\ListBlock::class,     new BlockRenderer\ListBlockRenderer(),     0)
73            ->addRenderer(BlockElement\ListItem::class,      new BlockRenderer\ListItemRenderer(),      0)
74            ->addRenderer(CoreBlockElement\Paragraph::class,     new BlockRenderer\ParagraphRenderer(),     0)
75            ->addRenderer(BlockElement\ThematicBreak::class, new BlockRenderer\ThematicBreakRenderer(), 0)
76
77            ->addRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
78            ->addRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
79            ->addRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
80            ->addRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
81            ->addRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
82            ->addRenderer(InlineElement\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
83            ->addRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
84            ->addRenderer(CoreInlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
85        ;
86
87        if ($environment->getConfiguration()->get('commonmark/use_asterisk')) {
88            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
89        }
90
91        if ($environment->getConfiguration()->get('commonmark/use_underscore')) {
92            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
93        }
94    }
95}
96