Completed
Push — toc-changes ( e65cac...16e29a )
by Colin
02:29
created

TableOfContentsPlaceholderParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\TableOfContents;
13
14
use League\CommonMark\Configuration\ConfigurationAwareInterface;
15
use League\CommonMark\Configuration\ConfigurationInterface;
16
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
17
use League\CommonMark\Node\Block\AbstractBlock;
18
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
19
use League\CommonMark\Parser\Block\BlockContinue;
20
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
21
use League\CommonMark\Parser\Block\BlockStart;
22
use League\CommonMark\Parser\Block\BlockStartParserInterface;
23
use League\CommonMark\Parser\Cursor;
24
use League\CommonMark\Parser\MarkdownParserStateInterface;
25
26
final class TableOfContentsPlaceholderParser extends AbstractBlockContinueParser
27
{
28
    /** @var TableOfContentsPlaceholder */
29
    private $block;
30
31 3
    public function __construct()
32
    {
33 3
        $this->block = new TableOfContentsPlaceholder();
34 3
    }
35
36 3
    public function getBlock(): AbstractBlock
37
    {
38 3
        return $this->block;
39
    }
40
41 3
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
42
    {
43 3
        return BlockContinue::none();
44
    }
45
46
    public static function blockStartParser(): BlockStartParserInterface
47
    {
48
        return new class() implements BlockStartParserInterface, ConfigurationAwareInterface {
49
            /** @var ConfigurationInterface */
50
            private $config;
51
52 3
            public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
53
            {
54 3
                $placeholder = $this->config->get('table_of_contents/placeholder');
55 3
                if ($placeholder === null) {
56
                    return BlockStart::none();
57
                }
58
59
                // The placeholder must be the only thing on the line
60 3
                if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) {
61 3
                    return BlockStart::none();
62
                }
63
64 3
                return BlockStart::of(new TableOfContentsPlaceholderParser())->at($cursor);
65
            }
66
67 3
            public function setConfiguration(ConfigurationInterface $configuration): void
68
            {
69 3
                $this->config = $configuration;
70 3
            }
71
        };
72
    }
73
}
74