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\ExtensionInterface;
18use League\CommonMark\ConfigurableEnvironmentInterface;
19use League\CommonMark\Block\Element as BlockElement;
20use League\CommonMark\Block\Parser as BlockParser;
21use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Block as BlockRenderer;
22use League\CommonMark\Inline\Element as InlineElement;
23use League\CommonMark\Inline\Parser as InlineParser;
24use Dokuwiki\Plugin\Commonmark\Extension\Renderer\Inline as InlineRenderer;
25use League\CommonMark\Util\ConfigurationInterface;
26use League\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor;
27
28final class CommonMarkToDokuWikiExtension implements ExtensionInterface {
29    public function register(ConfigurableEnvironmentInterface $environment) {
30        $environment
31            ->addBlockParser(new BlockParser\BlockQuoteParser(),      70)
32            ->addBlockParser(new BlockParser\ATXHeadingParser(),      60)
33            ->addBlockParser(new BlockParser\FencedCodeParser(),      50)
34            //->addBlockParser(new BlockParser\HtmlBlockParser(),       40) # No raw HTML processing on Commonmarkside
35            ->addBlockParser(new BlockParser\SetExtHeadingParser(),   30)
36            ->addBlockParser(new BlockParser\ThematicBreakParser(),   20)
37            ->addBlockParser(new BlockParser\ListParser(),            10)
38            ->addBlockParser(new BlockParser\IndentedCodeParser(),  -100)
39            ->addBlockParser(new BlockParser\LazyParagraphParser(), -200)
40
41            ->addInlineParser(new InlineParser\NewlineParser(),     200)
42            ->addInlineParser(new InlineParser\BacktickParser(),    150)
43            ->addInlineParser(new InlineParser\EscapableParser(),    80)
44            ->addInlineParser(new InlineParser\EntityParser(),       70)
45            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
46            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
47            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
48            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
49            ->addInlineParser(new InlineParser\BangParser(),         10)
50
51            ->addBlockRenderer(BlockElement\BlockQuote::class,    new BlockRenderer\BlockQuoteRenderer(),    0)
52            ->addBlockRenderer(BlockElement\Document::class,      new BlockRenderer\DocumentRenderer(),      0)
53            ->addBlockRenderer(BlockElement\FencedCode::class,    new BlockRenderer\FencedCodeRenderer(),    0)
54            ->addBlockRenderer(BlockElement\Heading::class,       new BlockRenderer\HeadingRenderer(),       0)
55            //->addBlockRenderer(BlockElement\HtmlBlock::class,     new BlockRenderer\HtmlBlockRenderer(),     0) # No raw HTML processing on Commonmarkside
56            ->addBlockRenderer(BlockElement\IndentedCode::class,  new BlockRenderer\IndentedCodeRenderer(),  0)
57            ->addBlockRenderer(BlockElement\ListBlock::class,     new BlockRenderer\ListBlockRenderer(),     0)
58            ->addBlockRenderer(BlockElement\ListItem::class,      new BlockRenderer\ListItemRenderer(),      0)
59            ->addBlockRenderer(BlockElement\Paragraph::class,     new BlockRenderer\ParagraphRenderer(),     0)
60            ->addBlockRenderer(BlockElement\ThematicBreak::class, new BlockRenderer\ThematicBreakRenderer(), 0)
61
62            ->addInlineRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
63            ->addInlineRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
64            ->addInlineRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
65            ->addInlineRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
66            ->addInlineRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
67            ->addInlineRenderer(InlineElement\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
68            ->addInlineRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
69            ->addInlineRenderer(InlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
70        ;
71
72        $deprecatedUseAsterisk = $environment->getConfig('use_asterisk', ConfigurationInterface::MISSING);
73        if ($deprecatedUseAsterisk !== ConfigurationInterface::MISSING) {
74            @\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        } else {
76            $deprecatedUseAsterisk = true;
77        }
78
79        if ($environment->getConfig('commonmark/use_asterisk', $deprecatedUseAsterisk)) {
80            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
81        }
82
83        $deprecatedUseUnderscore = $environment->getConfig('use_underscore', ConfigurationInterface::MISSING);
84        if ($deprecatedUseUnderscore !== ConfigurationInterface::MISSING) {
85            @\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        } else {
87            $deprecatedUseUnderscore = true;
88        }
89
90        if ($environment->getConfig('commonmark/use_underscore', $deprecatedUseUnderscore)) {
91            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
92        }
93    }
94}
95