Completed
Push — master ( 9461f7...c86ba2 )
by Colin
03:59 queued 39s
created

SetExtHeadingParser::parse()   B

Complexity

Conditions 5
Paths 5

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 22
Code Lines 12

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 8.6738
cc 5
eloc 12
nc 5
nop 2
crap 5
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (http://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
15
namespace League\CommonMark\Block\Parser;
16
17
use League\CommonMark\Block\Element\Heading;
18
use League\CommonMark\Block\Element\Paragraph;
19
use League\CommonMark\ContextInterface;
20
use League\CommonMark\Cursor;
21
use League\CommonMark\Util\RegexHelper;
22
23
class SetExtHeadingParser extends AbstractBlockParser
24
{
25
    /**
26
     * @param ContextInterface $context
27
     * @param Cursor           $cursor
28
     *
29
     * @return bool
30
     */
31 1704
    public function parse(ContextInterface $context, Cursor $cursor)
32
    {
33 1704
        if ($cursor->isIndented()) {
34 177
            return false;
35
        }
36
37 1650
        if (!($context->getContainer() instanceof Paragraph)) {
38 1650
            return false;
39
        }
40
41 303
        $match = RegexHelper::matchAll('/^(?:=+|-+) *$/', $cursor->getLine(), $cursor->getFirstNonSpacePosition());
42 303
        if ($match === null) {
43 261
            return false;
44
        }
45
46 48
        $level = $match[0][0] === '=' ? 1 : 2;
47 48
        $strings = $context->getContainer()->getStrings();
48
49 48
        $context->replaceContainerBlock(new Heading($level, $strings));
50
51 48
        return true;
52
    }
53
}
54