xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/CommonmarkToDokuwikiExtension.php (revision 7dbe6b57b5a32b3566f6d58d153a75423fb11482)
1c21fce6dSSungbin Jeon<?php
2c21fce6dSSungbin Jeon/*
3c21fce6dSSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
4c21fce6dSSungbin Jeon *
5c21fce6dSSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com>
6c21fce6dSSungbin Jeon *
7c21fce6dSSungbin Jeon * Original code based on the followings:
8c21fce6dSSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
9c21fce6dSSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
10c21fce6dSSungbin Jeon *
11c21fce6dSSungbin Jeon * For the full copyright and license information, please view the LICENSE
12c21fce6dSSungbin Jeon * file that was distributed with this source code.
13c21fce6dSSungbin Jeon */
14c21fce6dSSungbin Jeon
15c21fce6dSSungbin Jeonnamespace DokuWiki\Plugin\Commonmark\Extension;
16c21fce6dSSungbin Jeon
17b0a36678SSungbin Jeonuse League\CommonMark\Extension\ConfigurableExtensionInterface;
1894a075eeSSungbin Jeonuse League\CommonMark\Environment\EnvironmentBuilderInterface;
1994a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Block as BlockElement;
2094a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Parser\Block as BlockParser;
21b0a36678SSungbin Jeonuse League\CommonMark\Node\Block as CoreBlockElement;
22b0a36678SSungbin Jeonuse League\CommonMark\Node\Inline as CoreInlineElement;
23*7dbe6b57SSungbin Jeonuse League\CommonMark\Node as CoreNode;
24*7dbe6b57SSungbin Jeonuse League\CommonMark\Parser as CoreParser;
25c21fce6dSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block as BlockRenderer;
2694a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Inline as InlineElement;
2794a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Parser\Inline as InlineParser;
28c21fce6dSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline as InlineRenderer;
29b0a36678SSungbin Jeonuse League\Config\ConfigurationBuilderInterface;
30b0a36678SSungbin Jeonuse League\CommonMark\Extension\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor;
31b0a36678SSungbin Jeonuse Nette\Schema\Expect;
32c21fce6dSSungbin Jeon
33b0a36678SSungbin Jeonfinal class CommonMarkToDokuWikiExtension implements ConfigurableExtensionInterface {
34b0a36678SSungbin Jeon
35b0a36678SSungbin Jeon    public function configureSchema(ConfigurationBuilderInterface $builder): void
36b0a36678SSungbin Jeon    {
37b0a36678SSungbin Jeon        $builder->addSchema('commonmark', Expect::structure([
38b0a36678SSungbin Jeon            'use_asterisk' => Expect::bool(true),
39b0a36678SSungbin Jeon            'use_underscore' => Expect::bool(true),
40b0a36678SSungbin Jeon            'enable_strong' => Expect::bool(true),
41b0a36678SSungbin Jeon            'enable_em' => Expect::bool(true),
42b0a36678SSungbin Jeon            'unordered_list_markers' => Expect::listOf('string')->min(1)->default(['*', '+', '-'])->mergeDefaults(false),
43b0a36678SSungbin Jeon        ]));
44b0a36678SSungbin Jeon    }
45b0a36678SSungbin Jeon
4694a075eeSSungbin Jeon    public function register(EnvironmentBuilderInterface $environment): void {
47c21fce6dSSungbin Jeon        $environment
4894a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\BlockQuoteStartParser(),      70)
4994a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\HeadingStartParser(),      60)
5094a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\FencedCodeStartParser(),      50)
5194a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\HtmlBlockParser(),       40) # No raw HTML processing on Commonmarkside
5294a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\SetExtHeadingParser(),   30)
5394a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\ThematicBreakStartParser(),   20)
5494a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\ListBlockStartParser(),            10)
5594a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\IndentedCodeStartParser(),  -100)
5694a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\LazyParagraphParser(), -200)
57c21fce6dSSungbin Jeon
58*7dbe6b57SSungbin Jeon            ->addInlineParser(new CoreParser\Inline\NewlineParser(),     200)
59c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BacktickParser(),    150)
60c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EscapableParser(),    80)
61c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EntityParser(),       70)
62c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
63c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
64c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
65c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
66c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BangParser(),         10)
67c21fce6dSSungbin Jeon
6894a075eeSSungbin Jeon            ->addRenderer(BlockElement\BlockQuote::class,    new BlockRenderer\BlockQuoteRenderer(),    0)
69b0a36678SSungbin Jeon            ->addRenderer(CoreBlockElement\Document::class,      new BlockRenderer\DocumentRenderer(),      0)
7094a075eeSSungbin Jeon            ->addRenderer(BlockElement\FencedCode::class,    new BlockRenderer\FencedCodeRenderer(),    0)
7194a075eeSSungbin Jeon            ->addRenderer(BlockElement\Heading::class,       new BlockRenderer\HeadingRenderer(),       0)
7294a075eeSSungbin Jeon            //->addRenderer(BlockElement\HtmlBlock::class,     new BlockRenderer\HtmlBlockRenderer(),     0) # No raw HTML processing on Commonmarkside
7394a075eeSSungbin Jeon            ->addRenderer(BlockElement\IndentedCode::class,  new BlockRenderer\IndentedCodeRenderer(),  0)
7494a075eeSSungbin Jeon            ->addRenderer(BlockElement\ListBlock::class,     new BlockRenderer\ListBlockRenderer(),     0)
7594a075eeSSungbin Jeon            ->addRenderer(BlockElement\ListItem::class,      new BlockRenderer\ListItemRenderer(),      0)
76b0a36678SSungbin Jeon            ->addRenderer(CoreBlockElement\Paragraph::class,     new BlockRenderer\ParagraphRenderer(),     0)
7794a075eeSSungbin Jeon            ->addRenderer(BlockElement\ThematicBreak::class, new BlockRenderer\ThematicBreakRenderer(), 0)
78c21fce6dSSungbin Jeon
7994a075eeSSungbin Jeon            ->addRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
8094a075eeSSungbin Jeon            ->addRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
8194a075eeSSungbin Jeon            ->addRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
8294a075eeSSungbin Jeon            ->addRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
8394a075eeSSungbin Jeon            ->addRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
84*7dbe6b57SSungbin Jeon            ->addRenderer(CoreNode\Inline\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
8594a075eeSSungbin Jeon            ->addRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
86b0a36678SSungbin Jeon            ->addRenderer(CoreInlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
87c21fce6dSSungbin Jeon        ;
88c21fce6dSSungbin Jeon
89b0a36678SSungbin Jeon        if ($environment->getConfiguration()->get('commonmark/use_asterisk')) {
90c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
91c21fce6dSSungbin Jeon        }
92c21fce6dSSungbin Jeon
93b0a36678SSungbin Jeon        if ($environment->getConfiguration()->get('commonmark/use_underscore')) {
94c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
95c21fce6dSSungbin Jeon        }
96c21fce6dSSungbin Jeon    }
97c21fce6dSSungbin Jeon}
98