1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Saito - The Threaded Web Forum |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
8
|
|
|
* @link https://github.com/Schlaefer/Saito |
9
|
|
|
* @license http://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BbcodeParser\Lib; |
13
|
|
|
|
14
|
|
|
use App\View\Helper\ParserHelper; |
15
|
|
|
use Saito\Markup\MarkupInterface; |
16
|
|
|
use Saito\Markup\MarkupSettings; |
17
|
|
|
|
18
|
|
|
class Markup implements MarkupInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \BbcodeParser\Lib\Editor|null |
22
|
|
|
*/ |
23
|
|
|
protected $editor; |
24
|
|
|
/** |
25
|
|
|
* @var \BbcodeParser\Lib\Parser|null |
26
|
|
|
*/ |
27
|
|
|
protected $parser; |
28
|
|
|
/** |
29
|
|
|
* @var \BbcodeParser\Lib\Preprocessor|null |
30
|
|
|
*/ |
31
|
|
|
protected $preproccesor; |
32
|
|
|
/** |
33
|
|
|
* @var \Saito\Markup\MarkupSettings|null |
34
|
|
|
*/ |
35
|
|
|
protected $settings; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function __construct(MarkupSettings $settings) |
41
|
|
|
{ |
42
|
|
|
$this->settings = $settings; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function getEditorHelp(ParserHelper $helper): string |
49
|
|
|
{ |
50
|
|
|
return $this->getEditor()->getEditorHelp($helper); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function getMarkupSet(): array |
57
|
|
|
{ |
58
|
|
|
return $this->getEditor()->getMarkupSet(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function parse(string $string, ParserHelper $helper, array $options = []): string |
65
|
|
|
{ |
66
|
|
|
if (!$this->parser) { |
67
|
|
|
$this->parser = new Parser($helper, $this->settings); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->parser->parse($string, $options); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function preprocess($string): string |
77
|
|
|
{ |
78
|
|
|
if (!$this->preproccesor) { |
79
|
|
|
$this->preproccesor = new Preprocessor($this->settings); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->preproccesor->process($string); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function citeText(string $string): string |
89
|
|
|
{ |
90
|
|
|
if (empty($string)) { |
91
|
|
|
return ''; |
92
|
|
|
} |
93
|
|
|
$quoteSymbol = $this->settings->get('quote_symbol'); |
94
|
|
|
$out = ''; |
95
|
|
|
// split already quoted lines |
96
|
|
|
$citeLines = preg_split( |
97
|
|
|
"/(^{$quoteSymbol}.*?$\n)/m", |
98
|
|
|
$string, |
99
|
|
|
-1, |
100
|
|
|
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY |
101
|
|
|
); |
102
|
|
|
foreach ($citeLines as $citeLine) { |
103
|
|
|
if (mb_strpos($citeLine, $quoteSymbol) === 0) { |
104
|
|
|
// already quoted lines need no further processing |
105
|
|
|
$out .= $citeLine; |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
// split [bbcode] |
109
|
|
|
$matches = preg_split( |
110
|
|
|
'`(\[(.+?)=?.*?\].+?\[/\2\])`', |
111
|
|
|
$citeLine, |
112
|
|
|
-1, |
113
|
|
|
PREG_SPLIT_DELIM_CAPTURE |
114
|
|
|
); |
115
|
|
|
$i = 0; |
116
|
|
|
$line = ''; |
117
|
|
|
foreach ($matches as $match) { |
118
|
|
|
// the [bbcode] preg_split uses a backreference \2 which is in the $matches |
119
|
|
|
// but is not needed in the results |
120
|
|
|
// @td @sm elegant solution |
121
|
|
|
$i++; |
122
|
|
|
if ($i % 3 == 0) { |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
// wrap long lines |
126
|
|
|
if (mb_strpos($match, '[') !== 0) { |
127
|
|
|
$line .= wordwrap($match); |
128
|
|
|
} else { |
129
|
|
|
$line .= $match; |
130
|
|
|
} |
131
|
|
|
// add newline to wrapped lines |
132
|
|
|
if (mb_strlen($line) > 60) { |
133
|
|
|
$out .= $line . "\n"; |
134
|
|
|
$line = ''; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
$out .= $line; |
138
|
|
|
} |
139
|
|
|
$out = preg_replace( |
140
|
|
|
"/^/m", |
141
|
|
|
$quoteSymbol . " ", |
142
|
|
|
$out |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $out; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get editor |
150
|
|
|
* |
151
|
|
|
* @return \BbcodeParser\Lib\Editor |
152
|
|
|
*/ |
153
|
|
|
protected function getEditor(): Editor |
154
|
|
|
{ |
155
|
|
|
if (!$this->editor) { |
156
|
|
|
$this->editor = new Editor(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->editor; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Reset class |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
protected function reset() |
168
|
|
|
{ |
169
|
|
|
$this->editor = null; |
170
|
|
|
$this->parser = null; |
171
|
|
|
$this->preproccesor = null; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|