1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js) |
11
|
|
|
* - (c) John MacFarlane |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace League\CommonMark\Extension\SmartPunct; |
18
|
|
|
|
19
|
|
|
use League\CommonMark\Delimiter\Delimiter; |
20
|
|
|
use League\CommonMark\Parser\Inline\InlineParserInterface; |
21
|
|
|
use League\CommonMark\Parser\Inline\InlineParserMatch; |
22
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
23
|
|
|
use League\CommonMark\Util\RegexHelper; |
24
|
|
|
|
25
|
|
|
final class QuoteParser implements InlineParserInterface |
26
|
|
|
{ |
27
|
|
|
public const DOUBLE_QUOTES = [Quote::DOUBLE_QUOTE, Quote::DOUBLE_QUOTE_OPENER, Quote::DOUBLE_QUOTE_CLOSER]; |
28
|
|
|
public const SINGLE_QUOTES = [Quote::SINGLE_QUOTE, Quote::SINGLE_QUOTE_OPENER, Quote::SINGLE_QUOTE_CLOSER]; |
29
|
|
|
|
30
|
51 |
|
public function getMatchDefinition(): InlineParserMatch |
31
|
|
|
{ |
32
|
51 |
|
return InlineParserMatch::oneOf(...\array_merge(self::DOUBLE_QUOTES, self::SINGLE_QUOTES)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Normalizes any quote characters found and manually adds them to the delimiter stack |
37
|
|
|
*/ |
38
|
33 |
|
public function parse(InlineParserContext $inlineContext): bool |
39
|
|
|
{ |
40
|
33 |
|
$char = $inlineContext->getFullMatch(); |
41
|
33 |
|
$cursor = $inlineContext->getCursor(); |
42
|
|
|
|
43
|
33 |
|
$normalizedCharacter = $this->getNormalizedQuoteCharacter($char); |
44
|
|
|
|
45
|
33 |
|
$charBefore = $cursor->peek(-1); |
46
|
33 |
|
if ($charBefore === null) { |
47
|
27 |
|
$charBefore = "\n"; |
48
|
|
|
} |
49
|
|
|
|
50
|
33 |
|
$cursor->advance(); |
51
|
|
|
|
52
|
33 |
|
$charAfter = $cursor->getCharacter(); |
53
|
33 |
|
if ($charAfter === null) { |
54
|
21 |
|
$charAfter = "\n"; |
55
|
|
|
} |
56
|
|
|
|
57
|
33 |
|
[$leftFlanking, $rightFlanking] = $this->determineFlanking($charBefore, $charAfter); |
58
|
33 |
|
$canOpen = $leftFlanking && ! $rightFlanking; |
59
|
33 |
|
$canClose = $rightFlanking; |
60
|
|
|
|
61
|
33 |
|
$node = new Quote($normalizedCharacter, ['delim' => true]); |
62
|
33 |
|
$inlineContext->getContainer()->appendChild($node); |
63
|
|
|
|
64
|
|
|
// Add entry to stack to this opener |
65
|
33 |
|
$inlineContext->getDelimiterStack()->push(new Delimiter($normalizedCharacter, 1, $node, $canOpen, $canClose)); |
66
|
|
|
|
67
|
33 |
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
33 |
|
private function getNormalizedQuoteCharacter(string $character): string |
71
|
|
|
{ |
72
|
33 |
|
if (\in_array($character, self::DOUBLE_QUOTES, true)) { |
73
|
18 |
|
return Quote::DOUBLE_QUOTE; |
74
|
|
|
} |
75
|
|
|
|
76
|
30 |
|
if (\in_array($character, self::SINGLE_QUOTES, true)) { |
77
|
30 |
|
return Quote::SINGLE_QUOTE; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $character; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool[] |
85
|
|
|
*/ |
86
|
33 |
|
private function determineFlanking(string $charBefore, string $charAfter): array |
87
|
|
|
{ |
88
|
33 |
|
$afterIsWhitespace = \preg_match('/\pZ|\s/u', $charAfter); |
89
|
33 |
|
$afterIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); |
90
|
33 |
|
$beforeIsWhitespace = \preg_match('/\pZ|\s/u', $charBefore); |
91
|
33 |
|
$beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); |
92
|
|
|
|
93
|
33 |
|
$leftFlanking = ! $afterIsWhitespace && |
94
|
33 |
|
! ($afterIsPunctuation && |
95
|
33 |
|
! $beforeIsWhitespace && |
96
|
33 |
|
! $beforeIsPunctuation); |
97
|
|
|
|
98
|
33 |
|
$rightFlanking = ! $beforeIsWhitespace && |
99
|
33 |
|
! ($beforeIsPunctuation && |
100
|
33 |
|
! $afterIsWhitespace && |
101
|
33 |
|
! $afterIsPunctuation); |
102
|
|
|
|
103
|
33 |
|
return [$leftFlanking, $rightFlanking]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|