|
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 (https://bitly.com/commonmark-js) |
|
11
|
|
|
* - (c) John MacFarlane |
|
12
|
|
|
* |
|
13
|
|
|
* Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java) |
|
14
|
|
|
* - (c) Atlassian Pty Ltd |
|
15
|
|
|
* |
|
16
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
17
|
|
|
* file that was distributed with this source code. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace League\CommonMark\Extension\CommonMark\Delimiter\Processor; |
|
21
|
|
|
|
|
22
|
|
|
use League\CommonMark\Delimiter\DelimiterInterface; |
|
23
|
|
|
use League\CommonMark\Delimiter\Processor\CacheableDelimiterProcessorInterface; |
|
24
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis; |
|
25
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Inline\Strong; |
|
26
|
|
|
use League\CommonMark\Node\Inline\AbstractStringContainer; |
|
27
|
|
|
use League\Config\ConfigurationAwareInterface; |
|
28
|
|
|
use League\Config\ConfigurationInterface; |
|
29
|
|
|
|
|
30
|
|
|
final class EmphasisDelimiterProcessor implements CacheableDelimiterProcessorInterface, ConfigurationAwareInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @psalm-readonly */ |
|
33
|
|
|
private string $char; |
|
34
|
|
|
|
|
35
|
|
|
/** @psalm-readonly-allow-private-mutation */ |
|
36
|
|
|
private ConfigurationInterface $config; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $char The emphasis character to use (typically '*' or '_') |
|
40
|
|
|
*/ |
|
41
|
2310 |
|
public function __construct(string $char) |
|
42
|
|
|
{ |
|
43
|
2310 |
|
$this->char = $char; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2310 |
|
public function getOpeningCharacter(): string |
|
47
|
|
|
{ |
|
48
|
2310 |
|
return $this->char; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2310 |
|
public function getClosingCharacter(): string |
|
52
|
|
|
{ |
|
53
|
2310 |
|
return $this->char; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
488 |
|
public function getMinLength(): int |
|
57
|
|
|
{ |
|
58
|
488 |
|
return 1; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
350 |
|
public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int |
|
62
|
|
|
{ |
|
63
|
|
|
// "Multiple of 3" rule for internal delimiter runs |
|
64
|
350 |
|
if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) { |
|
65
|
16 |
|
return 0; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Calculate actual number of delimiters used from this closer |
|
69
|
350 |
|
if ($opener->getLength() >= 2 && $closer->getLength() >= 2) { |
|
70
|
188 |
|
if ($this->config->get('commonmark/enable_strong')) { |
|
71
|
186 |
|
return 2; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
248 |
|
if ($this->config->get('commonmark/enable_em')) { |
|
78
|
246 |
|
return 1; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
346 |
|
public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void |
|
85
|
|
|
{ |
|
86
|
346 |
|
if ($delimiterUse === 1) { |
|
87
|
246 |
|
$emphasis = new Emphasis($this->char); |
|
88
|
186 |
|
} elseif ($delimiterUse === 2) { |
|
89
|
186 |
|
$emphasis = new Strong($this->char . $this->char); |
|
90
|
|
|
} else { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
346 |
|
$next = $opener->next(); |
|
95
|
346 |
|
while ($next !== null && $next !== $closer) { |
|
96
|
346 |
|
$tmp = $next->next(); |
|
97
|
346 |
|
$emphasis->appendChild($next); |
|
98
|
346 |
|
$next = $tmp; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
346 |
|
$opener->insertAfter($emphasis); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2310 |
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
105
|
|
|
{ |
|
106
|
2310 |
|
$this->config = $configuration; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
404 |
|
public function getCacheKey(DelimiterInterface $closer): string |
|
110
|
|
|
{ |
|
111
|
404 |
|
return \sprintf( |
|
112
|
404 |
|
'%s-%s-%d-%d', |
|
113
|
404 |
|
$this->char, |
|
114
|
404 |
|
$closer->canOpen() ? 'canOpen' : 'cannotOpen', |
|
115
|
404 |
|
$closer->getOriginalLength() % 3, |
|
116
|
404 |
|
$closer->getLength(), |
|
117
|
404 |
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|