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