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