|
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\Extension\CommonMark\Delimiter\Processor; |
|
19
|
|
|
|
|
20
|
|
|
use League\CommonMark\Configuration\ConfigurationAwareInterface; |
|
21
|
|
|
use League\CommonMark\Configuration\ConfigurationInterface; |
|
22
|
|
|
use League\CommonMark\Delimiter\DelimiterInterface; |
|
23
|
|
|
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface; |
|
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
|
|
|
|
|
28
|
|
|
final class EmphasisDelimiterProcessor implements DelimiterProcessorInterface, ConfigurationAwareInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $char; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ConfigurationInterface|null */ |
|
34
|
|
|
private $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $char The emphasis character to use (typically '*' or '_') |
|
38
|
|
|
*/ |
|
39
|
2490 |
|
public function __construct(string $char) |
|
40
|
|
|
{ |
|
41
|
2490 |
|
$this->char = $char; |
|
42
|
2490 |
|
} |
|
43
|
|
|
|
|
44
|
2490 |
|
public function getOpeningCharacter(): string |
|
45
|
|
|
{ |
|
46
|
2490 |
|
return $this->char; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2490 |
|
public function getClosingCharacter(): string |
|
50
|
|
|
{ |
|
51
|
2490 |
|
return $this->char; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
639 |
|
public function getMinLength(): int |
|
55
|
|
|
{ |
|
56
|
639 |
|
return 1; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
429 |
|
public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int |
|
60
|
|
|
{ |
|
61
|
|
|
// "Multiple of 3" rule for internal delimiter runs |
|
62
|
429 |
|
if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) { |
|
63
|
12 |
|
return 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Calculate actual number of delimiters used from this closer |
|
67
|
429 |
|
if ($opener->getLength() >= 2 && $closer->getLength() >= 2) { |
|
68
|
186 |
|
if ($this->config && $this->config->get('enable_strong', true)) { |
|
69
|
186 |
|
return 2; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
324 |
|
if ($this->config && $this->config->get('enable_em', true)) { |
|
76
|
324 |
|
return 1; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return 0; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
429 |
|
public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void |
|
83
|
|
|
{ |
|
84
|
429 |
|
if ($delimiterUse === 1) { |
|
85
|
324 |
|
$emphasis = new Emphasis(); |
|
86
|
186 |
|
} elseif ($delimiterUse === 2) { |
|
87
|
186 |
|
$emphasis = new Strong(); |
|
88
|
|
|
} else { |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
429 |
|
$next = $opener->next(); |
|
93
|
429 |
|
while ($next !== null && $next !== $closer) { |
|
94
|
429 |
|
$tmp = $next->next(); |
|
95
|
429 |
|
$emphasis->appendChild($next); |
|
96
|
429 |
|
$next = $tmp; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
429 |
|
$opener->insertAfter($emphasis); |
|
100
|
429 |
|
} |
|
101
|
|
|
|
|
102
|
2490 |
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
103
|
|
|
{ |
|
104
|
2490 |
|
$this->config = $configuration; |
|
105
|
2490 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|