Completed
Pull Request — master (#2)
by Colin
02:24
created

StrikethroughParser::parse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
ccs 18
cts 18
cp 1
rs 9.424
cc 4
nc 6
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark-ext-strikethrough package.
5
 *
6
 * (c) Colin O'Dell <[email protected]> and uAfrica.com (http://uafrica.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
12
namespace League\CommonMark\Ext\Strikethrough;
13
14
use League\CommonMark\Delimiter\Delimiter;
15
use League\CommonMark\Inline\Element\Text;
16
use League\CommonMark\Inline\Parser\EmphasisParser;
17
use League\CommonMark\Inline\Parser\InlineParserInterface;
18
use League\CommonMark\InlineParserContext;
19
20
final class StrikethroughParser implements InlineParserInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 51
    public function getCharacters(): array
26
    {
27 51
        return ['~'];
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 48
    public function parse(InlineParserContext $inlineContext): bool
34
    {
35 48
        $cursor = $inlineContext->getCursor();
36 48
        $charBefore = $cursor->peek(-1);
37 48
        if ($charBefore === null) {
38 6
            $charBefore = "\n";
39
        }
40
41 48
        $tildes = $cursor->match('/^~~+/');
42 48
        if ($tildes === null) {
43 6
            return false;
44
        }
45
46 42
        $charAfter = $cursor->getCharacter();
47 42
        if ($charAfter === null) {
48 12
            $charAfter = "\n";
49
        }
50
51 42
        list($canOpen, $canClose) = EmphasisParser::determineCanOpenOrClose($charBefore, $charAfter, '~');
52
53 42
        $node = new Text($cursor->getPreviousText(), [
54 42
            'delim' => true,
55
        ]);
56 42
        $inlineContext->getContainer()->appendChild($node);
57
58
        // Add entry to stack to this opener
59 42
        $delimiter = new Delimiter('~', \mb_strlen($tildes, $cursor->getEncoding()), $node, $canOpen, $canClose);
60 42
        $inlineContext->getDelimiterStack()->push($delimiter);
61
62 42
        return true;
63
    }
64
}
65