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