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
|
57 |
|
public function parse(InlineParserContext $inlineContext): bool |
34
|
|
|
{ |
35
|
57 |
|
$cursor = $inlineContext->getCursor(); |
36
|
57 |
|
$charBefore = $cursor->peek(-1); |
37
|
57 |
|
if ($charBefore === null) { |
38
|
6 |
|
$charBefore = "\n"; |
39
|
|
|
} |
40
|
|
|
|
41
|
57 |
|
$tildes = $cursor->match('/^~~+/'); |
42
|
57 |
|
if ($tildes === null) { |
43
|
6 |
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
51 |
|
$charAfter = $cursor->getCharacter(); |
47
|
51 |
|
if ($charAfter === null) { |
48
|
12 |
|
$charAfter = "\n"; |
49
|
|
|
} |
50
|
|
|
|
51
|
51 |
|
list($canOpen, $canClose) = EmphasisParser::determineCanOpenOrClose($charBefore, $charAfter, '~'); |
52
|
|
|
|
53
|
51 |
|
$node = new Text($cursor->getPreviousText(), [ |
54
|
51 |
|
'delim' => true, |
55
|
|
|
]); |
56
|
51 |
|
$inlineContext->getContainer()->appendChild($node); |
57
|
|
|
|
58
|
|
|
// Add entry to stack to this opener |
59
|
51 |
|
$delimiter = new Delimiter('~', \mb_strlen($tildes, $cursor->getEncoding()), $node, $canOpen, $canClose); |
60
|
51 |
|
$inlineContext->getDelimiterStack()->push($delimiter); |
61
|
|
|
|
62
|
51 |
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|