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(string $match, InlineParserContext $inlineContext): bool |
39
|
|
|
{ |
40
|
33 |
|
$cursor = $inlineContext->getCursor(); |
41
|
|
|
|
42
|
33 |
|
$normalizedCharacter = $this->getNormalizedQuoteCharacter($match); |
43
|
|
|
|
44
|
33 |
|
$charBefore = $cursor->peek(-1); |
45
|
33 |
|
if ($charBefore === null) { |
46
|
27 |
|
$charBefore = "\n"; |
47
|
|
|
} |
48
|
|
|
|
49
|
33 |
|
$cursor->advance(); |
50
|
|
|
|
51
|
33 |
|
$charAfter = $cursor->getCharacter(); |
52
|
33 |
|
if ($charAfter === null) { |
53
|
21 |
|
$charAfter = "\n"; |
54
|
|
|
} |
55
|
|
|
|
56
|
33 |
|
[$leftFlanking, $rightFlanking] = $this->determineFlanking($charBefore, $charAfter); |
57
|
33 |
|
$canOpen = $leftFlanking && ! $rightFlanking; |
58
|
33 |
|
$canClose = $rightFlanking; |
59
|
|
|
|
60
|
33 |
|
$node = new Quote($normalizedCharacter, ['delim' => true]); |
61
|
33 |
|
$inlineContext->getContainer()->appendChild($node); |
62
|
|
|
|
63
|
|
|
// Add entry to stack to this opener |
64
|
33 |
|
$inlineContext->getDelimiterStack()->push(new Delimiter($normalizedCharacter, 1, $node, $canOpen, $canClose)); |
65
|
|
|
|
66
|
33 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
33 |
|
private function getNormalizedQuoteCharacter(string $character): string |
70
|
|
|
{ |
71
|
33 |
|
if (\in_array($character, self::DOUBLE_QUOTES, true)) { |
72
|
18 |
|
return Quote::DOUBLE_QUOTE; |
73
|
|
|
} |
74
|
|
|
|
75
|
30 |
|
if (\in_array($character, self::SINGLE_QUOTES, true)) { |
76
|
30 |
|
return Quote::SINGLE_QUOTE; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $character; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool[] |
84
|
|
|
*/ |
85
|
33 |
|
private function determineFlanking(string $charBefore, string $charAfter): array |
86
|
|
|
{ |
87
|
33 |
|
$afterIsWhitespace = \preg_match('/\pZ|\s/u', $charAfter); |
88
|
33 |
|
$afterIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); |
89
|
33 |
|
$beforeIsWhitespace = \preg_match('/\pZ|\s/u', $charBefore); |
90
|
33 |
|
$beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); |
91
|
|
|
|
92
|
33 |
|
$leftFlanking = ! $afterIsWhitespace && |
93
|
33 |
|
! ($afterIsPunctuation && |
94
|
33 |
|
! $beforeIsWhitespace && |
95
|
33 |
|
! $beforeIsPunctuation); |
96
|
|
|
|
97
|
33 |
|
$rightFlanking = ! $beforeIsWhitespace && |
98
|
33 |
|
! ($beforeIsPunctuation && |
99
|
33 |
|
! $afterIsWhitespace && |
100
|
33 |
|
! $afterIsPunctuation); |
101
|
|
|
|
102
|
33 |
|
return [$leftFlanking, $rightFlanking]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|