|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
9
|
|
|
* - (c) John MacFarlane |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace League\CommonMark\Inline\Parser; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Delimiter\Delimiter; |
|
18
|
|
|
use League\CommonMark\Environment; |
|
19
|
|
|
use League\CommonMark\EnvironmentAwareInterface; |
|
20
|
|
|
use League\CommonMark\Inline\Element\Text; |
|
21
|
|
|
use League\CommonMark\InlineParserContext; |
|
22
|
|
|
use League\CommonMark\Util\Configuration; |
|
23
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
24
|
|
|
|
|
25
|
|
|
class EmphasisParser extends AbstractInlineParser implements EnvironmentAwareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
protected $config; |
|
28
|
|
|
|
|
29
|
1941 |
|
public function __construct(array $newConfig = []) |
|
30
|
|
|
{ |
|
31
|
1941 |
|
$this->config = new Configuration([ |
|
32
|
1941 |
|
'use_asterisk' => true, |
|
33
|
647 |
|
'use_underscore' => true, |
|
34
|
647 |
|
'enable_em' => true, |
|
35
|
647 |
|
'enable_strong' => true, |
|
36
|
647 |
|
]); |
|
37
|
1941 |
|
$this->config->mergeConfig($newConfig); |
|
38
|
1941 |
|
} |
|
39
|
|
|
|
|
40
|
1941 |
|
public function setEnvironment(Environment $environment) |
|
41
|
|
|
{ |
|
42
|
1941 |
|
$this->config->mergeConfig($environment->getConfig()); |
|
43
|
1941 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string[] |
|
47
|
|
|
*/ |
|
48
|
1941 |
|
public function getCharacters() |
|
49
|
|
|
{ |
|
50
|
1941 |
|
if (!$this->config->getConfig('enable_em') && !$this->config->getConfig('enable_strong')) { |
|
51
|
|
|
return []; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1941 |
|
$chars = []; |
|
55
|
1941 |
|
if ($this->config->getConfig('use_asterisk')) { |
|
56
|
1941 |
|
$chars[] = '*'; |
|
57
|
647 |
|
} |
|
58
|
1941 |
|
if ($this->config->getConfig('use_underscore')) { |
|
59
|
1941 |
|
$chars[] = '_'; |
|
60
|
647 |
|
} |
|
61
|
|
|
|
|
62
|
1941 |
|
return $chars; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param InlineParserContext $inlineContext |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
519 |
|
public function parse(InlineParserContext $inlineContext) |
|
71
|
|
|
{ |
|
72
|
519 |
|
$character = $inlineContext->getCursor()->getCharacter(); |
|
73
|
519 |
|
if (!in_array($character, $this->getCharacters())) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
519 |
|
$numDelims = 0; |
|
78
|
|
|
|
|
79
|
519 |
|
$cursor = $inlineContext->getCursor(); |
|
80
|
519 |
|
$charBefore = $cursor->peek(-1); |
|
81
|
519 |
|
if ($charBefore === null) { |
|
82
|
348 |
|
$charBefore = "\n"; |
|
83
|
116 |
|
} |
|
84
|
|
|
|
|
85
|
519 |
|
while ($cursor->peek($numDelims) === $character) { |
|
86
|
519 |
|
++$numDelims; |
|
87
|
173 |
|
} |
|
88
|
|
|
|
|
89
|
519 |
|
if ($numDelims === 0) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Skip single delims if emphasis is disabled |
|
94
|
519 |
|
if ($numDelims === 1 && !$this->config->getConfig('enable_em')) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
519 |
|
$cursor->advanceBy($numDelims); |
|
99
|
|
|
|
|
100
|
519 |
|
$charAfter = $cursor->getCharacter(); |
|
101
|
519 |
|
if ($charAfter === null) { |
|
102
|
351 |
|
$charAfter = "\n"; |
|
103
|
117 |
|
} |
|
104
|
|
|
|
|
105
|
519 |
|
list($canOpen, $canClose) = $this->determineCanOpenOrClose($charBefore, $charAfter, $character); |
|
106
|
|
|
|
|
107
|
519 |
|
$node = new Text($cursor->getPreviousText(), [ |
|
108
|
519 |
|
'delim' => true, |
|
109
|
519 |
|
'emphasis_config' => $this->config, |
|
110
|
173 |
|
]); |
|
111
|
519 |
|
$inlineContext->getContainer()->appendChild($node); |
|
112
|
|
|
|
|
113
|
|
|
// Add entry to stack to this opener |
|
114
|
519 |
|
$delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose); |
|
115
|
519 |
|
$inlineContext->getDelimiterStack()->push($delimiter); |
|
116
|
|
|
|
|
117
|
519 |
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $charBefore |
|
122
|
|
|
* @param string $charAfter |
|
123
|
|
|
* @param string $character |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool[] |
|
126
|
|
|
*/ |
|
127
|
519 |
|
private function determineCanOpenOrClose($charBefore, $charAfter, $character) |
|
128
|
|
|
{ |
|
129
|
519 |
|
$afterIsWhitespace = preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter); |
|
130
|
519 |
|
$afterIsPunctuation = preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); |
|
131
|
519 |
|
$beforeIsWhitespace = preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore); |
|
132
|
519 |
|
$beforeIsPunctuation = preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); |
|
133
|
|
|
|
|
134
|
519 |
|
$leftFlanking = !$afterIsWhitespace && (!$afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation); |
|
135
|
519 |
|
$rightFlanking = !$beforeIsWhitespace && (!$beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation); |
|
136
|
|
|
|
|
137
|
519 |
|
if ($character === '_') { |
|
138
|
210 |
|
$canOpen = $leftFlanking && (!$rightFlanking || $beforeIsPunctuation); |
|
139
|
210 |
|
$canClose = $rightFlanking && (!$leftFlanking || $afterIsPunctuation); |
|
140
|
70 |
|
} else { |
|
141
|
339 |
|
$canOpen = $leftFlanking; |
|
142
|
339 |
|
$canClose = $rightFlanking; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
519 |
|
return [$canOpen, $canClose]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|