|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\CommonMark\Delimiter; |
|
6
|
|
|
|
|
7
|
|
|
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection; |
|
8
|
|
|
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface; |
|
9
|
|
|
use League\CommonMark\Node\Inline\Text; |
|
10
|
|
|
use League\CommonMark\Parser\Inline\InlineParserInterface; |
|
11
|
|
|
use League\CommonMark\Parser\Inline\InlineParserMatch; |
|
12
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
|
13
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Delimiter parsing is implemented as an Inline Parser with the lowest-possible priority |
|
17
|
|
|
* |
|
18
|
|
|
* @internal |
|
19
|
|
|
*/ |
|
20
|
|
|
final class DelimiterParser implements InlineParserInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var DelimiterProcessorCollection */ |
|
23
|
|
|
private $collection; |
|
24
|
|
|
|
|
25
|
2886 |
|
public function __construct(DelimiterProcessorCollection $collection) |
|
26
|
|
|
{ |
|
27
|
2886 |
|
$this->collection = $collection; |
|
28
|
2886 |
|
} |
|
29
|
|
|
|
|
30
|
2874 |
|
public function getMatchDefinition(): InlineParserMatch |
|
31
|
|
|
{ |
|
32
|
2874 |
|
return InlineParserMatch::oneOf(...$this->collection->getDelimiterCharacters()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
681 |
|
public function parse(string $match, InlineParserContext $inlineContext): bool |
|
36
|
|
|
{ |
|
37
|
681 |
|
$character = $match; |
|
38
|
681 |
|
$numDelims = 0; |
|
39
|
681 |
|
$cursor = $inlineContext->getCursor(); |
|
40
|
681 |
|
$processor = $this->collection->getDelimiterProcessor($character); |
|
41
|
|
|
|
|
42
|
681 |
|
if ($processor === null) { |
|
43
|
|
|
throw new \LogicException('Delimiter processor should never be null here'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
681 |
|
$charBefore = $cursor->peek(-1); |
|
47
|
681 |
|
if ($charBefore === null) { |
|
48
|
411 |
|
$charBefore = "\n"; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
681 |
|
while ($cursor->peek($numDelims) === $character) { |
|
52
|
681 |
|
++$numDelims; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
681 |
|
if ($numDelims < $processor->getMinLength()) { |
|
56
|
6 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
675 |
|
$cursor->advanceBy($numDelims); |
|
60
|
|
|
|
|
61
|
675 |
|
$charAfter = $cursor->getCharacter(); |
|
62
|
675 |
|
if ($charAfter === null) { |
|
63
|
420 |
|
$charAfter = "\n"; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
675 |
|
[$canOpen, $canClose] = self::determineCanOpenOrClose($charBefore, $charAfter, $character, $processor); |
|
67
|
|
|
|
|
68
|
675 |
|
$node = new Text(\str_repeat($character, $numDelims), [ |
|
69
|
675 |
|
'delim' => true, |
|
70
|
|
|
]); |
|
71
|
675 |
|
$inlineContext->getContainer()->appendChild($node); |
|
72
|
|
|
|
|
73
|
|
|
// Add entry to stack to this opener |
|
74
|
675 |
|
if ($canOpen || $canClose) { |
|
75
|
621 |
|
$delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose); |
|
76
|
621 |
|
$inlineContext->getDelimiterStack()->push($delimiter); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
675 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return bool[] |
|
84
|
|
|
*/ |
|
85
|
675 |
|
private static function determineCanOpenOrClose(string $charBefore, string $charAfter, string $character, DelimiterProcessorInterface $delimiterProcessor): array |
|
86
|
|
|
{ |
|
87
|
675 |
|
$afterIsWhitespace = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter); |
|
88
|
675 |
|
$afterIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); |
|
89
|
675 |
|
$beforeIsWhitespace = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore); |
|
90
|
675 |
|
$beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); |
|
91
|
|
|
|
|
92
|
675 |
|
$leftFlanking = ! $afterIsWhitespace && (! $afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation); |
|
93
|
675 |
|
$rightFlanking = ! $beforeIsWhitespace && (! $beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation); |
|
94
|
|
|
|
|
95
|
675 |
|
if ($character === '_') { |
|
96
|
228 |
|
$canOpen = $leftFlanking && (! $rightFlanking || $beforeIsPunctuation); |
|
97
|
228 |
|
$canClose = $rightFlanking && (! $leftFlanking || $afterIsPunctuation); |
|
98
|
|
|
} else { |
|
99
|
486 |
|
$canOpen = $leftFlanking && $character === $delimiterProcessor->getOpeningCharacter(); |
|
100
|
486 |
|
$canClose = $rightFlanking && $character === $delimiterProcessor->getClosingCharacter(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
675 |
|
return [$canOpen, $canClose]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|