/
HtmlOutputNodePre.class.php
256 lines (226 loc) · 7.62 KB
/
HtmlOutputNodePre.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace wcf\system\html\output\node;
use wcf\system\bbcode\highlighter\BashHighlighter;
use wcf\system\bbcode\highlighter\CHighlighter;
use wcf\system\bbcode\highlighter\DiffHighlighter;
use wcf\system\bbcode\highlighter\HtmlHighlighter;
use wcf\system\bbcode\highlighter\JavaHighlighter;
use wcf\system\bbcode\highlighter\JsHighlighter;
use wcf\system\bbcode\highlighter\PerlHighlighter;
use wcf\system\bbcode\highlighter\PhpHighlighter;
use wcf\system\bbcode\highlighter\PlainHighlighter;
use wcf\system\bbcode\highlighter\PythonHighlighter;
use wcf\system\bbcode\highlighter\SqlHighlighter;
use wcf\system\bbcode\highlighter\TexHighlighter;
use wcf\system\bbcode\highlighter\XmlHighlighter;
use wcf\system\html\node\AbstractHtmlNodeProcessor;
use wcf\system\Regex;
use wcf\system\WCF;
use wcf\util\StringUtil;
/**
* Processes code listings.
*
* @author Alexander Ebert
* @copyright 2001-2017 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package WoltLabSuite\Core\System\Html\Output\Node
* @since 3.0
*/
class HtmlOutputNodePre extends AbstractHtmlOutputNode {
/**
* @inheritDoc
*/
protected $tagName = 'pre';
/**
* already used ids for line numbers to prevent duplicate ids in the output
* @var string[]
*/
private static $codeIDs = [];
/**
* @inheritDoc
*/
public function process(array $elements, AbstractHtmlNodeProcessor $htmlNodeProcessor) {
/** @var \DOMElement $element */
foreach ($elements as $element) {
if ($element->getAttribute('class') === 'woltlabHtml') {
$nodeIdentifier = StringUtil::getRandomID();
$htmlNodeProcessor->addNodeData($this, $nodeIdentifier, ['rawHTML' => $element->textContent]);
$htmlNodeProcessor->renameTag($element, 'wcfNode-' . $nodeIdentifier);
continue;
}
switch ($this->outputType) {
case 'text/html':
$nodeIdentifier = StringUtil::getRandomID();
$htmlNodeProcessor->addNodeData($this, $nodeIdentifier, [
'content' => $element->textContent,
'file' => $element->getAttribute('data-file'),
'highlighter' => $element->getAttribute('data-highlighter'),
'line' => $element->hasAttribute('data-line') ? $element->getAttribute('data-line') : 1,
'skipInnerContent' => true
]);
$htmlNodeProcessor->renameTag($element, 'wcfNode-' . $nodeIdentifier);
break;
case 'text/simplified-html':
case 'text/plain':
$htmlNodeProcessor->replaceElementWithText(
$element,
WCF::getLanguage()->getDynamicVariable('wcf.bbcode.code.simplified', ['lines' => substr_count($element->nodeValue, "\n") + 1]),
true
);
break;
}
}
}
/**
* @inheritDoc
*/
public function replaceTag(array $data) {
// HTML bbcode
if (isset($data['rawHTML'])) {
return $data['rawHTML'];
}
$content = preg_replace('/^\s*\n/', '', $data['content']);
$content = preg_replace('/\n\s*$/', '', $content);
$file = $data['file'];
$highlighter = $data['highlighter'];
$line = ($data['line'] < 1) ? 1 : $data['line'];
// fetch highlighter-classname
$className = PlainHighlighter::class;
// no highlighting for strings over a certain size, to prevent DoS
// this serves as a safety net in case one of the regular expressions
// in a highlighter causes PCRE to exhaust resources, such as the stack
if (strlen($content) < 16384) {
if ($highlighter) {
$className = '\wcf\system\bbcode\highlighter\\'.StringUtil::firstCharToUpperCase(mb_strtolower($highlighter)).'Highlighter';
switch (mb_substr($className, strlen('\wcf\system\bbcode\highlighter\\'))) {
case 'ShellHighlighter':
$className = BashHighlighter::class;
break;
case 'C++Highlighter':
$className = CHighlighter::class;
break;
case 'JavascriptHighlighter':
$className = JsHighlighter::class;
break;
case 'LatexHighlighter':
$className = TexHighlighter::class;
break;
}
}
else {
// try to guess highlighter
if (mb_strpos($content, '<?php') !== false) {
$className = PhpHighlighter::class;
}
else if (mb_strpos($content, '<html') !== false) {
$className = HtmlHighlighter::class;
}
else if (mb_strpos($content, '<?xml') === 0) {
$className = XmlHighlighter::class;
}
else if ( mb_strpos($content, 'SELECT') === 0
|| mb_strpos($content, 'UPDATE') === 0
|| mb_strpos($content, 'INSERT') === 0
|| mb_strpos($content, 'DELETE') === 0) {
$className = SqlHighlighter::class;
}
else if (mb_strpos($content, 'import java.') !== false) {
$className = JavaHighlighter::class;
}
else if ( mb_strpos($content, "---") !== false
&& mb_strpos($content, "\n+++") !== false) {
$className = DiffHighlighter::class;
}
else if (mb_strpos($content, "\n#include ") !== false) {
$className = CHighlighter::class;
}
else if (mb_strpos($content, '#!/usr/bin/perl') === 0) {
$className = PerlHighlighter::class;
}
else if (mb_strpos($content, 'def __init__(self') !== false) {
$className = PythonHighlighter::class;
}
else if (Regex::compile('^#!/bin/(ba|z)?sh')->match($content)) {
$className = BashHighlighter::class;
}
else if (mb_strpos($content, '\\documentclass') !== false) {
$className = TexHighlighter::class;
}
}
}
if (!class_exists($className)) {
$className = PlainHighlighter::class;
}
/** @noinspection PhpUndefinedMethodInspection */
$highlightedContent = $this->fixMarkup(explode("\n", $className::getInstance()->highlight($content)));
// show template
/** @noinspection PhpUndefinedMethodInspection */
WCF::getTPL()->assign([
'lineNumbers' => $this->makeLineNumbers($content, $line),
'startLineNumber' => $line,
'content' => $highlightedContent,
'highlighter' => $className::getInstance(),
'filename' => $file,
'lines' => substr_count($content, "\n") + 1
]);
return WCF::getTPL()->fetch('codeMetaCode');
}
/**
* Returns a string with all line numbers
*
* @param string $code
* @param integer $start
* @param string $split
* @return string[]
*/
protected function makeLineNumbers($code, $start, $split = "\n") {
$lines = explode($split, $code);
$lineNumbers = [];
$i = -1;
// find an unused codeID
do {
$codeID = mb_substr(StringUtil::getHash($code), 0, 6).(++$i ? '_'.$i : '');
}
while (isset(self::$codeIDs[$codeID]));
// mark codeID as used
self::$codeIDs[$codeID] = true;
for ($i = $start, $j = count($lines) + $start; $i < $j; $i++) {
$lineNumbers[$i] = 'codeLine_'.$i.'_'.$codeID;
}
return $lineNumbers;
}
/**
* Fixes markup that every line has proper number of opening and closing tags
*
* @param string[] $lines
* @return string[]
*/
protected function fixMarkup(array $lines) {
static $spanRegex = null;
static $emptyTagRegex = null;
if ($spanRegex === null) {
$spanRegex = new Regex('(?:<span(?: class="(?:[^"])*")?>|</span>)');
$emptyTagRegex = new Regex('<span(?: class="(?:[^"])*")?></span>');
}
$openTags = [];
foreach ($lines as &$line) {
$spanRegex->match($line, true);
// open all tags again
$line = implode('', $openTags).$line;
$matches = $spanRegex->getMatches();
// parse opening and closing spans
foreach ($matches[0] as $match) {
if ($match === '</span>') array_pop($openTags);
else {
array_push($openTags, $match);
}
}
// close all tags
$line .= str_repeat('</span>', count($openTags));
// remove empty tags to avoid cluttering the output
$line = $emptyTagRegex->replace($line, '');
}
unset($line);
return $lines;
}
}