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 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace League\CommonMark\Extension\InlinesOnly;
13
14use League\CommonMark\Block\Element\Document;
15use League\CommonMark\Block\Element\Paragraph;
16use League\CommonMark\Block\Parser as BlockParser;
17use League\CommonMark\ConfigurableEnvironmentInterface;
18use League\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor;
19use League\CommonMark\Extension\ExtensionInterface;
20use League\CommonMark\Inline\Element as InlineElement;
21use League\CommonMark\Inline\Parser as InlineParser;
22use League\CommonMark\Inline\Renderer as InlineRenderer;
23
24final class InlinesOnlyExtension implements ExtensionInterface
25{
26    public function register(ConfigurableEnvironmentInterface $environment)
27    {
28        $childRenderer = new ChildRenderer();
29
30        $environment
31            ->addBlockParser(new BlockParser\LazyParagraphParser(), -200)
32
33            ->addInlineParser(new InlineParser\NewlineParser(),     200)
34            ->addInlineParser(new InlineParser\BacktickParser(),    150)
35            ->addInlineParser(new InlineParser\EscapableParser(),    80)
36            ->addInlineParser(new InlineParser\EntityParser(),       70)
37            ->addInlineParser(new InlineParser\AutolinkParser(),     50)
38            ->addInlineParser(new InlineParser\HtmlInlineParser(),   40)
39            ->addInlineParser(new InlineParser\CloseBracketParser(), 30)
40            ->addInlineParser(new InlineParser\OpenBracketParser(),  20)
41            ->addInlineParser(new InlineParser\BangParser(),         10)
42
43            ->addBlockRenderer(Document::class, $childRenderer, 0)
44            ->addBlockRenderer(Paragraph::class, $childRenderer, 0)
45
46            ->addInlineRenderer(InlineElement\Code::class,       new InlineRenderer\CodeRenderer(),       0)
47            ->addInlineRenderer(InlineElement\Emphasis::class,   new InlineRenderer\EmphasisRenderer(),   0)
48            ->addInlineRenderer(InlineElement\HtmlInline::class, new InlineRenderer\HtmlInlineRenderer(), 0)
49            ->addInlineRenderer(InlineElement\Image::class,      new InlineRenderer\ImageRenderer(),      0)
50            ->addInlineRenderer(InlineElement\Link::class,       new InlineRenderer\LinkRenderer(),       0)
51            ->addInlineRenderer(InlineElement\Newline::class,    new InlineRenderer\NewlineRenderer(),    0)
52            ->addInlineRenderer(InlineElement\Strong::class,     new InlineRenderer\StrongRenderer(),     0)
53            ->addInlineRenderer(InlineElement\Text::class,       new InlineRenderer\TextRenderer(),       0)
54        ;
55
56        if ($environment->getConfig('use_asterisk', true)) {
57            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*'));
58        }
59        if ($environment->getConfig('use_underscore', true)) {
60            $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('_'));
61        }
62    }
63}
64