xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/CommonmarkToDokuwikiExtension.php (revision 94a075ee9c5821074e0d7b2c07e7e230fefe6cd9)
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
17c21fce6dSSungbin Jeonuse League\CommonMark\Extension\ExtensionInterface;
18*94a075eeSSungbin Jeonuse League\CommonMark\Environment\EnvironmentBuilderInterface;
19*94a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Block as BlockElement;
20*94a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Parser\Block as BlockParser;
21c21fce6dSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block as BlockRenderer;
22*94a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Inline as InlineElement;
23*94a075eeSSungbin Jeonuse League\CommonMark\Extension\CommonMark\Parser\Inline as InlineParser;
24c21fce6dSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline as InlineRenderer;
25c21fce6dSSungbin Jeonuse League\CommonMark\Util\ConfigurationInterface;
26c21fce6dSSungbin Jeonuse League\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor;
27c21fce6dSSungbin Jeon
28c21fce6dSSungbin Jeonfinal class CommonMarkToDokuWikiExtension implements ExtensionInterface {
29*94a075eeSSungbin Jeon    public function register(EnvironmentBuilderInterface $environment): void {
30c21fce6dSSungbin Jeon        $environment
31*94a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\BlockQuoteStartParser(),      70)
32*94a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\HeadingStartParser(),      60)
33*94a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\FencedCodeStartParser(),      50)
34*94a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\HtmlBlockParser(),       40) # No raw HTML processing on Commonmarkside
35*94a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\SetExtHeadingParser(),   30)
36*94a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\ThematicBreakStartParser(),   20)
37*94a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\ListBlockStartParser(),            10)
38*94a075eeSSungbin Jeon            ->addBlockStartParser(new BlockParser\IndentedCodeStartParser(),  -100)
39*94a075eeSSungbin Jeon            //->addBlockStartParser(new BlockParser\LazyParagraphParser(), -200)
40c21fce6dSSungbin Jeon
41*94a075eeSSungbin Jeon            //->addInlineParser(new InlineParser\NewlineParser(),     200)
42c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BacktickParser(),    150)
43c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EscapableParser(),    80)
44c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EntityParser(),       70)
45c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
46c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
47c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
48c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
49c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BangParser(),         10)
50c21fce6dSSungbin Jeon
51*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\BlockQuote::class,    new BlockRenderer\BlockQuoteRenderer(),    0)
52*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\Document::class,      new BlockRenderer\DocumentRenderer(),      0)
53*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\FencedCode::class,    new BlockRenderer\FencedCodeRenderer(),    0)
54*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\Heading::class,       new BlockRenderer\HeadingRenderer(),       0)
55*94a075eeSSungbin Jeon            //->addRenderer(BlockElement\HtmlBlock::class,     new BlockRenderer\HtmlBlockRenderer(),     0) # No raw HTML processing on Commonmarkside
56*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\IndentedCode::class,  new BlockRenderer\IndentedCodeRenderer(),  0)
57*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\ListBlock::class,     new BlockRenderer\ListBlockRenderer(),     0)
58*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\ListItem::class,      new BlockRenderer\ListItemRenderer(),      0)
59*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\Paragraph::class,     new BlockRenderer\ParagraphRenderer(),     0)
60*94a075eeSSungbin Jeon            ->addRenderer(BlockElement\ThematicBreak::class, new BlockRenderer\ThematicBreakRenderer(), 0)
61c21fce6dSSungbin Jeon
62*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
63*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
64*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
65*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
66*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
67*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
68*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
69*94a075eeSSungbin Jeon            ->addRenderer(InlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
70c21fce6dSSungbin Jeon        ;
71c21fce6dSSungbin Jeon
72c21fce6dSSungbin Jeon        $deprecatedUseAsterisk = $environment->getConfig('use_asterisk', ConfigurationInterface::MISSING);
73c21fce6dSSungbin Jeon        if ($deprecatedUseAsterisk !== ConfigurationInterface::MISSING) {
74c21fce6dSSungbin Jeon            @\trigger_error('The "use_asterisk" configuration option is deprecated in league/commonmark 1.6 and will be replaced with "commonmark > use_asterisk" in 2.0', \E_USER_DEPRECATED);
75c21fce6dSSungbin Jeon        } else {
76c21fce6dSSungbin Jeon            $deprecatedUseAsterisk = true;
77c21fce6dSSungbin Jeon        }
78c21fce6dSSungbin Jeon
79c21fce6dSSungbin Jeon        if ($environment->getConfig('commonmark/use_asterisk', $deprecatedUseAsterisk)) {
80c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
81c21fce6dSSungbin Jeon        }
82c21fce6dSSungbin Jeon
83c21fce6dSSungbin Jeon        $deprecatedUseUnderscore = $environment->getConfig('use_underscore', ConfigurationInterface::MISSING);
84c21fce6dSSungbin Jeon        if ($deprecatedUseUnderscore !== ConfigurationInterface::MISSING) {
85c21fce6dSSungbin Jeon            @\trigger_error('The "use_underscore" configuration option is deprecated in league/commonmark 1.6 and will be replaced with "commonmark > use_underscore" in 2.0', \E_USER_DEPRECATED);
86c21fce6dSSungbin Jeon        } else {
87c21fce6dSSungbin Jeon            $deprecatedUseUnderscore = true;
88c21fce6dSSungbin Jeon        }
89c21fce6dSSungbin Jeon
90c21fce6dSSungbin Jeon        if ($environment->getConfig('commonmark/use_underscore', $deprecatedUseUnderscore)) {
91c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
92c21fce6dSSungbin Jeon        }
93c21fce6dSSungbin Jeon    }
94c21fce6dSSungbin Jeon}
95