xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/CommonmarkToDokuwikiExtension.php (revision 0f46309a0ac22fdc4d1c91b56eae39d6eedaaf00)
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;
237dbe6b57SSungbin Jeonuse League\CommonMark\Node as CoreNode;
247dbe6b57SSungbin 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),
43*0f46309aSChalix            'hard_break' => Expect::string("\\\\ "),
44b0a36678SSungbin Jeon        ]));
45b0a36678SSungbin Jeon    }
46b0a36678SSungbin Jeon
4794a075eeSSungbin Jeon    public function register(EnvironmentBuilderInterface $environment): void {
48c21fce6dSSungbin Jeon        $environment
4994a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\BlockQuoteStartParser(),      70)
5094a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\HeadingStartParser(),      60)
5194a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\FencedCodeStartParser(),      50)
5294a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\HtmlBlockParser(),       40) # No raw HTML processing on Commonmarkside
5394a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\SetExtHeadingParser(),   30)
5494a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\ThematicBreakStartParser(),   20)
5594a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\ListBlockStartParser(),            10)
5694a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\IndentedCodeStartParser(),  -100)
5794a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\LazyParagraphParser(), -200)
58c21fce6dSSungbin Jeon
597dbe6b57SSungbin Jeon            ->addInlineParser(new CoreParser\Inline\NewlineParser(),     200)
60c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BacktickParser(),    150)
61c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EscapableParser(),    80)
62c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EntityParser(),       70)
63c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
64c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
65c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
66c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
67c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BangParser(),         10)
68c21fce6dSSungbin Jeon
6994a075eeSSungbin Jeon            ->addRenderer(BlockElement\BlockQuote::class,    new BlockRenderer\BlockQuoteRenderer(),    0)
70b0a36678SSungbin Jeon            ->addRenderer(CoreBlockElement\Document::class,      new BlockRenderer\DocumentRenderer(),      0)
7194a075eeSSungbin Jeon            ->addRenderer(BlockElement\FencedCode::class,    new BlockRenderer\FencedCodeRenderer(),    0)
7294a075eeSSungbin Jeon            ->addRenderer(BlockElement\Heading::class,       new BlockRenderer\HeadingRenderer(),       0)
7394a075eeSSungbin Jeon            //->addRenderer(BlockElement\HtmlBlock::class,     new BlockRenderer\HtmlBlockRenderer(),     0) # No raw HTML processing on Commonmarkside
7494a075eeSSungbin Jeon            ->addRenderer(BlockElement\IndentedCode::class,  new BlockRenderer\IndentedCodeRenderer(),  0)
7594a075eeSSungbin Jeon            ->addRenderer(BlockElement\ListBlock::class,     new BlockRenderer\ListBlockRenderer(),     0)
7694a075eeSSungbin Jeon            ->addRenderer(BlockElement\ListItem::class,      new BlockRenderer\ListItemRenderer(),      0)
77b0a36678SSungbin Jeon            ->addRenderer(CoreBlockElement\Paragraph::class,     new BlockRenderer\ParagraphRenderer(),     0)
7894a075eeSSungbin Jeon            ->addRenderer(BlockElement\ThematicBreak::class, new BlockRenderer\ThematicBreakRenderer(), 0)
79c21fce6dSSungbin Jeon
8094a075eeSSungbin Jeon            ->addRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
8194a075eeSSungbin Jeon            ->addRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
8294a075eeSSungbin Jeon            ->addRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
8394a075eeSSungbin Jeon            ->addRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
8494a075eeSSungbin Jeon            ->addRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
857dbe6b57SSungbin Jeon            ->addRenderer(CoreNode\Inline\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
8694a075eeSSungbin Jeon            ->addRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
87b0a36678SSungbin Jeon            ->addRenderer(CoreInlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
88c21fce6dSSungbin Jeon        ;
89c21fce6dSSungbin Jeon
90b0a36678SSungbin Jeon        if ($environment->getConfiguration()->get('commonmark/use_asterisk')) {
91c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
92c21fce6dSSungbin Jeon        }
93c21fce6dSSungbin Jeon
94b0a36678SSungbin Jeon        if ($environment->getConfiguration()->get('commonmark/use_underscore')) {
95c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
96c21fce6dSSungbin Jeon        }
97c21fce6dSSungbin Jeon    }
98c21fce6dSSungbin Jeon}
99