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