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
|
|
|
/** |
28
|
|
|
* @deprecated This constant is no longer used and will be removed in a future major release |
29
|
|
|
*/ |
30
|
|
|
public const DOUBLE_QUOTES = [Quote::DOUBLE_QUOTE, Quote::DOUBLE_QUOTE_OPENER, Quote::DOUBLE_QUOTE_CLOSER]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @deprecated This constant is no longer used and will be removed in a future major release |
34
|
|
|
*/ |
35
|
|
|
public const SINGLE_QUOTES = [Quote::SINGLE_QUOTE, Quote::SINGLE_QUOTE_OPENER, Quote::SINGLE_QUOTE_CLOSER]; |
36
|
|
|
|
37
|
40 |
|
public function getMatchDefinition(): InlineParserMatch |
38
|
|
|
{ |
39
|
40 |
|
return InlineParserMatch::oneOf(Quote::SINGLE_QUOTE, Quote::DOUBLE_QUOTE); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Normalizes any quote characters found and manually adds them to the delimiter stack |
44
|
|
|
*/ |
45
|
28 |
|
public function parse(InlineParserContext $inlineContext): bool |
46
|
|
|
{ |
47
|
28 |
|
$char = $inlineContext->getFullMatch(); |
48
|
28 |
|
$cursor = $inlineContext->getCursor(); |
49
|
|
|
|
50
|
28 |
|
$charBefore = $cursor->peek(-1); |
51
|
28 |
|
if ($charBefore === null) { |
52
|
22 |
|
$charBefore = "\n"; |
53
|
|
|
} |
54
|
|
|
|
55
|
28 |
|
$cursor->advance(); |
56
|
|
|
|
57
|
28 |
|
$charAfter = $cursor->getCurrentCharacter(); |
58
|
28 |
|
if ($charAfter === null) { |
59
|
16 |
|
$charAfter = "\n"; |
60
|
|
|
} |
61
|
|
|
|
62
|
28 |
|
[$leftFlanking, $rightFlanking] = $this->determineFlanking($charBefore, $charAfter); |
63
|
28 |
|
$canOpen = $leftFlanking && ! $rightFlanking; |
64
|
28 |
|
$canClose = $rightFlanking; |
65
|
|
|
|
66
|
28 |
|
$node = new Quote($char, ['delim' => true]); |
67
|
28 |
|
$inlineContext->getContainer()->appendChild($node); |
68
|
|
|
|
69
|
|
|
// Add entry to stack to this opener |
70
|
28 |
|
$inlineContext->getDelimiterStack()->push(new Delimiter($char, 1, $node, $canOpen, $canClose)); |
71
|
|
|
|
72
|
28 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool[] |
77
|
|
|
*/ |
78
|
28 |
|
private function determineFlanking(string $charBefore, string $charAfter): array |
79
|
|
|
{ |
80
|
28 |
|
$afterIsWhitespace = \preg_match('/\pZ|\s/u', $charAfter); |
81
|
28 |
|
$afterIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); |
82
|
28 |
|
$beforeIsWhitespace = \preg_match('/\pZ|\s/u', $charBefore); |
83
|
28 |
|
$beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); |
84
|
|
|
|
85
|
28 |
|
$leftFlanking = ! $afterIsWhitespace && |
86
|
28 |
|
! ($afterIsPunctuation && |
87
|
28 |
|
! $beforeIsWhitespace && |
88
|
28 |
|
! $beforeIsPunctuation); |
89
|
|
|
|
90
|
28 |
|
$rightFlanking = ! $beforeIsWhitespace && |
91
|
28 |
|
! ($beforeIsPunctuation && |
92
|
28 |
|
! $afterIsWhitespace && |
93
|
28 |
|
! $afterIsPunctuation); |
94
|
|
|
|
95
|
28 |
|
return [$leftFlanking, $rightFlanking]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|