1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the league/commonmark package.
7 *
8 * (c) Colin O'Dell <colinodell@gmail.com>
9 *
10 * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js)
11 *  - (c) John MacFarlane
12 *
13 * For the full copyright and license information, please view the LICENSE
14 * file that was distributed with this source code.
15 */
16
17namespace League\CommonMark\Extension\SmartPunct;
18
19use League\CommonMark\Environment\EnvironmentBuilderInterface;
20use League\CommonMark\Event\DocumentParsedEvent;
21use League\CommonMark\Extension\ConfigurableExtensionInterface;
22use League\CommonMark\Node\Block\Document;
23use League\CommonMark\Node\Block\Paragraph;
24use League\CommonMark\Node\Inline\Text;
25use League\CommonMark\Renderer\Block as CoreBlockRenderer;
26use League\CommonMark\Renderer\Inline as CoreInlineRenderer;
27use League\Config\ConfigurationBuilderInterface;
28use Nette\Schema\Expect;
29
30final class SmartPunctExtension implements ConfigurableExtensionInterface
31{
32    public function configureSchema(ConfigurationBuilderInterface $builder): void
33    {
34        $builder->addSchema('smartpunct', Expect::structure([
35            'double_quote_opener' => Expect::string(Quote::DOUBLE_QUOTE_OPENER),
36            'double_quote_closer' => Expect::string(Quote::DOUBLE_QUOTE_CLOSER),
37            'single_quote_opener' => Expect::string(Quote::SINGLE_QUOTE_OPENER),
38            'single_quote_closer' => Expect::string(Quote::SINGLE_QUOTE_CLOSER),
39        ]));
40    }
41
42    public function register(EnvironmentBuilderInterface $environment): void
43    {
44        $environment
45            ->addInlineParser(new QuoteParser(), 10)
46            ->addInlineParser(new DashParser(), 0)
47            ->addInlineParser(new EllipsesParser(), 0)
48
49            ->addDelimiterProcessor(QuoteProcessor::createDoubleQuoteProcessor(
50                $environment->getConfiguration()->get('smartpunct/double_quote_opener'),
51                $environment->getConfiguration()->get('smartpunct/double_quote_closer')
52            ))
53            ->addDelimiterProcessor(QuoteProcessor::createSingleQuoteProcessor(
54                $environment->getConfiguration()->get('smartpunct/single_quote_opener'),
55                $environment->getConfiguration()->get('smartpunct/single_quote_closer')
56            ))
57
58            ->addEventListener(DocumentParsedEvent::class, new ReplaceUnpairedQuotesListener())
59
60            ->addRenderer(Document::class, new CoreBlockRenderer\DocumentRenderer(), 0)
61            ->addRenderer(Paragraph::class, new CoreBlockRenderer\ParagraphRenderer(), 0)
62            ->addRenderer(Text::class, new CoreInlineRenderer\TextRenderer(), 0);
63    }
64}
65