|
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
|
|
|
private DelimiterProcessorCollection $collection; |
|
23
|
|
|
|
|
24
|
2148 |
|
public function __construct(DelimiterProcessorCollection $collection) |
|
25
|
|
|
{ |
|
26
|
2148 |
|
$this->collection = $collection; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2138 |
|
public function getMatchDefinition(): InlineParserMatch |
|
30
|
|
|
{ |
|
31
|
2138 |
|
return InlineParserMatch::oneOf(...$this->collection->getDelimiterCharacters()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
514 |
|
public function parse(InlineParserContext $inlineContext): bool |
|
35
|
|
|
{ |
|
36
|
514 |
|
$character = $inlineContext->getFullMatch(); |
|
37
|
514 |
|
$numDelims = 0; |
|
38
|
514 |
|
$cursor = $inlineContext->getCursor(); |
|
39
|
514 |
|
$processor = $this->collection->getDelimiterProcessor($character); |
|
40
|
|
|
|
|
41
|
514 |
|
if ($processor === null) { |
|
42
|
|
|
throw new \LogicException('Delimiter processor should never be null here'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
514 |
|
$charBefore = $cursor->peek(-1); |
|
46
|
514 |
|
if ($charBefore === null) { |
|
47
|
306 |
|
$charBefore = "\n"; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
514 |
|
while ($cursor->peek($numDelims) === $character) { |
|
51
|
514 |
|
++$numDelims; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
514 |
|
if ($numDelims < $processor->getMinLength()) { |
|
55
|
4 |
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
510 |
|
$cursor->advanceBy($numDelims); |
|
59
|
|
|
|
|
60
|
510 |
|
$charAfter = $cursor->getCurrentCharacter(); |
|
61
|
510 |
|
if ($charAfter === null) { |
|
62
|
318 |
|
$charAfter = "\n"; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
510 |
|
[$canOpen, $canClose] = self::determineCanOpenOrClose($charBefore, $charAfter, $character, $processor); |
|
66
|
|
|
|
|
67
|
510 |
|
$node = new Text(\str_repeat($character, $numDelims), [ |
|
68
|
|
|
'delim' => true, |
|
69
|
|
|
]); |
|
70
|
510 |
|
$inlineContext->getContainer()->appendChild($node); |
|
71
|
|
|
|
|
72
|
|
|
// Add entry to stack to this opener |
|
73
|
510 |
|
if ($canOpen || $canClose) { |
|
74
|
472 |
|
$delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose); |
|
75
|
472 |
|
$inlineContext->getDelimiterStack()->push($delimiter); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
510 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return bool[] |
|
83
|
|
|
*/ |
|
84
|
510 |
|
private static function determineCanOpenOrClose(string $charBefore, string $charAfter, string $character, DelimiterProcessorInterface $delimiterProcessor): array |
|
85
|
|
|
{ |
|
86
|
510 |
|
$afterIsWhitespace = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter); |
|
87
|
510 |
|
$afterIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); |
|
88
|
510 |
|
$beforeIsWhitespace = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore); |
|
89
|
510 |
|
$beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); |
|
90
|
|
|
|
|
91
|
510 |
|
$leftFlanking = ! $afterIsWhitespace && (! $afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation); |
|
92
|
510 |
|
$rightFlanking = ! $beforeIsWhitespace && (! $beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation); |
|
93
|
|
|
|
|
94
|
510 |
|
if ($character === '_') { |
|
95
|
166 |
|
$canOpen = $leftFlanking && (! $rightFlanking || $beforeIsPunctuation); |
|
96
|
166 |
|
$canClose = $rightFlanking && (! $leftFlanking || $afterIsPunctuation); |
|
97
|
|
|
} else { |
|
98
|
374 |
|
$canOpen = $leftFlanking && $character === $delimiterProcessor->getOpeningCharacter(); |
|
99
|
374 |
|
$canClose = $rightFlanking && $character === $delimiterProcessor->getClosingCharacter(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
510 |
|
return [$canOpen, $canClose]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|