xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/CommonmarkToDokuwikiExtension.php (revision c21fce6d54cbafdf43eabb6ffa7d497b74d7abe0)
1*c21fce6dSSungbin Jeon<?php
2*c21fce6dSSungbin Jeon/*
3*c21fce6dSSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
4*c21fce6dSSungbin Jeon *
5*c21fce6dSSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com>
6*c21fce6dSSungbin Jeon *
7*c21fce6dSSungbin Jeon * Original code based on the followings:
8*c21fce6dSSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
9*c21fce6dSSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
10*c21fce6dSSungbin Jeon *
11*c21fce6dSSungbin Jeon * For the full copyright and license information, please view the LICENSE
12*c21fce6dSSungbin Jeon * file that was distributed with this source code.
13*c21fce6dSSungbin Jeon */
14*c21fce6dSSungbin Jeon
15*c21fce6dSSungbin Jeonnamespace DokuWiki\Plugin\Commonmark\Extension;
16*c21fce6dSSungbin Jeon
17*c21fce6dSSungbin Jeonuse League\CommonMark\Extension\ExtensionInterface;
18*c21fce6dSSungbin Jeonuse League\CommonMark\ConfigurableEnvironmentInterface;
19*c21fce6dSSungbin Jeonuse League\CommonMark\Block\Element as BlockElement;
20*c21fce6dSSungbin Jeonuse League\CommonMark\Block\Parser as BlockParser;
21*c21fce6dSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block as BlockRenderer;
22*c21fce6dSSungbin Jeonuse League\CommonMark\Inline\Element as InlineElement;
23*c21fce6dSSungbin Jeonuse League\CommonMark\Inline\Parser as InlineParser;
24*c21fce6dSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline as InlineRenderer;
25*c21fce6dSSungbin Jeonuse League\CommonMark\Util\ConfigurationInterface;
26*c21fce6dSSungbin Jeonuse League\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor;
27*c21fce6dSSungbin Jeon
28*c21fce6dSSungbin Jeonfinal class CommonMarkToDokuWikiExtension implements ExtensionInterface {
29*c21fce6dSSungbin Jeon    public function register(ConfigurableEnvironmentInterface $environment) {
30*c21fce6dSSungbin Jeon        $environment
31*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\BlockQuoteParser(),      70)
32*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\ATXHeadingParser(),      60)
33*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\FencedCodeParser(),      50)
34*c21fce6dSSungbin Jeon            //->addBlockParser(new BlockParser\HtmlBlockParser(),       40) # No raw HTML processing on Commonmarkside
35*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\SetExtHeadingParser(),   30)
36*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\ThematicBreakParser(),   20)
37*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\ListParser(),            10)
38*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\IndentedCodeParser(),  -100)
39*c21fce6dSSungbin Jeon            ->addBlockParser(new BlockParser\LazyParagraphParser(), -200)
40*c21fce6dSSungbin Jeon
41*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\NewlineParser(),     200)
42*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BacktickParser(),    150)
43*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EscapableParser(),    80)
44*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\EntityParser(),       70)
45*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
46*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
47*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
48*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
49*c21fce6dSSungbin Jeon            ->addInlineParser(new InlineParser\BangParser(),         10)
50*c21fce6dSSungbin Jeon
51*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\BlockQuote::class,    new BlockRenderer\BlockQuoteRenderer(),    0)
52*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\Document::class,      new BlockRenderer\DocumentRenderer(),      0)
53*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\FencedCode::class,    new BlockRenderer\FencedCodeRenderer(),    0)
54*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\Heading::class,       new BlockRenderer\HeadingRenderer(),       0)
55*c21fce6dSSungbin Jeon            //->addBlockRenderer(BlockElement\HtmlBlock::class,     new BlockRenderer\HtmlBlockRenderer(),     0) # No raw HTML processing on Commonmarkside
56*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\IndentedCode::class,  new BlockRenderer\IndentedCodeRenderer(),  0)
57*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\ListBlock::class,     new BlockRenderer\ListBlockRenderer(),     0)
58*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\ListItem::class,      new BlockRenderer\ListItemRenderer(),      0)
59*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\Paragraph::class,     new BlockRenderer\ParagraphRenderer(),     0)
60*c21fce6dSSungbin Jeon            ->addBlockRenderer(BlockElement\ThematicBreak::class, new BlockRenderer\ThematicBreakRenderer(), 0)
61*c21fce6dSSungbin Jeon
62*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
63*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
64*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
65*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
66*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
67*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
68*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
69*c21fce6dSSungbin Jeon            ->addInlineRenderer(InlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
70*c21fce6dSSungbin Jeon        ;
71*c21fce6dSSungbin Jeon
72*c21fce6dSSungbin Jeon        $deprecatedUseAsterisk = $environment->getConfig('use_asterisk', ConfigurationInterface::MISSING);
73*c21fce6dSSungbin Jeon        if ($deprecatedUseAsterisk !== ConfigurationInterface::MISSING) {
74*c21fce6dSSungbin 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);
75*c21fce6dSSungbin Jeon        } else {
76*c21fce6dSSungbin Jeon            $deprecatedUseAsterisk = true;
77*c21fce6dSSungbin Jeon        }
78*c21fce6dSSungbin Jeon
79*c21fce6dSSungbin Jeon        if ($environment->getConfig('commonmark/use_asterisk', $deprecatedUseAsterisk)) {
80*c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
81*c21fce6dSSungbin Jeon        }
82*c21fce6dSSungbin Jeon
83*c21fce6dSSungbin Jeon        $deprecatedUseUnderscore = $environment->getConfig('use_underscore', ConfigurationInterface::MISSING);
84*c21fce6dSSungbin Jeon        if ($deprecatedUseUnderscore !== ConfigurationInterface::MISSING) {
85*c21fce6dSSungbin 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);
86*c21fce6dSSungbin Jeon        } else {
87*c21fce6dSSungbin Jeon            $deprecatedUseUnderscore = true;
88*c21fce6dSSungbin Jeon        }
89*c21fce6dSSungbin Jeon
90*c21fce6dSSungbin Jeon        if ($environment->getConfig('commonmark/use_underscore', $deprecatedUseUnderscore)) {
91*c21fce6dSSungbin Jeon            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
92*c21fce6dSSungbin Jeon        }
93*c21fce6dSSungbin Jeon    }
94*c21fce6dSSungbin Jeon}
95