-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbcode_geshi.php
More file actions
351 lines (297 loc) · 12.2 KB
/
Copy pathbbcode_geshi.php
File metadata and controls
351 lines (297 loc) · 12.2 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
if (!defined('PHORUM')) return;
define('GESHI_PATH', './mods/bbcode_geshi/geshi');
define('GESHI_LANG_PATH', GESHI_PATH.'/geshi');
include_once('./mods/bbcode_geshi/defaults.php');
// Register the CSS code that is provided by GeSHi and the additional CSS
// code for adding / overriding some definitions from our module templates.
function phorum_mod_bbcode_geshi_css_register($data)
{
global $PHORUM;
// A cache key part for the activated languages.
$languages = empty($PHORUM['mod_bbcode_geshi']['languages'])
? ''
: md5(implode(":", $PHORUM['mod_bbcode_geshi']['languages']));
$data['register'][] = array(
"module" => "bbcode_geshi",
"where" => "after",
"source" => "function(mod_bbcode_geshi_css)",
"cache_key" => filemtime(GESHI_PATH."/geshi.php") .
filemtime(__FILE__) .
$languages
);
$data['register'][] = array(
"module" => "bbcode_geshi",
"where" => "after",
"source" => "template(bbcode_geshi::css)"
);
return $data;
}
// A function that is used as the GeSHi CSS source (as registered in
// the above hook function).
function mod_bbcode_geshi_css()
{
global $PHORUM;
// No languages available? Should not happen, but let's be prepared.
if (empty($PHORUM['mod_bbcode_geshi']['languages'])) {
return '';
}
// We use GeSHi to generate the required stylesheet code.
include_once(GESHI_PATH . '/geshi.php');
$geshi = new GeSHi();
// The "@MARK@" is used below to prepend our #phorum id in the CSS selectors.
$geshi->set_overall_class('@MARK@bbcode_geshi');
$css = '';
foreach ($PHORUM['mod_bbcode_geshi']['languages'] as $language)
{
$geshi->set_language($language);
$add_css = $geshi->get_stylesheet(false);
$add_css = preg_replace('/^\/\*\*.*?\*\//s', '', $add_css);
$add_css = preg_replace('/\.([^\.]+)\.@MARK@/', '#phorum .\\1.', $add_css);
$css .= $add_css;
}
return $css;
}
// Register the JavaScript for this module, which implements the
// overrid behavior for the [code] editor tool button.
function phorum_mod_bbcode_geshi_javascript_register($data)
{
// Only load the javascript code if the editor tool support is enabled.
if (!empty($GLOBALS['PHORUM']['mod_bbcode_geshi']['enable_editor_tool']))
{
$data[] = array(
'module' => 'bbcode_geshi',
'source' => 'file(mods/bbcode_geshi/bbcode_geshi.js)'
);
}
return $data;
}
// In the before_editor hook, we can be sure that we are displaying
// an editor on the screen. Here we add a list of languages to the
// page header (in a JavaScript object), which should be shown in
// the editor tool [code] dropdown.
function phorum_mod_bbcode_geshi_before_editor($message)
{
global $PHORUM;
$lang = $PHORUM['DATA']['LANG']['mod_bbcode_geshi'];
// No work for us here, unless the editor tool option is enabled.
if (empty($PHORUM['mod_bbcode_geshi']['enable_editor_tool'])) {
return $message;
}
// No languages available? Should not happen, but let's be prepared.
if (empty($PHORUM['mod_bbcode_geshi']['languages'])) {
return $message;
}
// Add available languages, translation strings and options for javascript.
$js = "<script type=\"text/javascript\">\n" .
"//<![CDATA[\n" .
"editor_tools_lang['ShowLineNumbers']='$lang[ShowLineNumbers]';\n" .
"editor_tools_lang['StartAtNumber']='$lang[StartAtNumber]';\n" .
"var editor_tools_geshi_enable_line_numbers = " .
(empty($PHORUM['mod_bbcode_geshi']['enable_line_numbers']) ? 0 : 1) .
";\n" .
"var editor_tools_geshi_languages = {\n";
$js .= "'NULL':'".addslashes($lang['PlainText'])."'";
foreach ($PHORUM['mod_bbcode_geshi']['languages'] as $id => $lang) {
$id = addslashes($id);
$lang = addslashes($lang);
$js .= ",\n'$id':'$lang'";
}
$js .="\n};\n" .
"//]]>\n" .
"</script>\n";
$PHORUM['DATA']['HEAD_TAGS'] .= $js;
return $message;
}
// First stage formatting: strip out all the valid code blocks and replace
// them with temporary placeholders.
function phorum_mod_bbcode_geshi_format($messages)
{
$PHORUM = $GLOBALS['PHORUM'];
// Regexps for matching code block start and end tags. The start tag is
// not fully qualified here. This is only used for a first tokenizing
// pass over the message body.
static $start = '\[(?:code|syntax)[\s=][^\]]+\]';
static $end = '\[\/(?:code|syntax)\]';
foreach ($messages as $id => $message)
{
// Skip formatting if bbcode formatting was disabled for the post
// (this is a feature of the BBcode module that we should honor).
if (!empty($PHORUM["mod_bbcode"]["allow_disable_per_post"]) &&
!empty($message['meta']['disable_bbcode'])) {
continue;
}
// Skipping this message if there is no body or a body
// without a syntax tag in it.
if (!isset($message['body']) ||
!preg_match('!\[(?:syntax|code)!', $message['body'])) continue;
// Split into tokens.
$tokens = preg_split(
"/($start|$end)/", $message['body'], -1,
PREG_SPLIT_DELIM_CAPTURE
);
// Process the tokens to find the syntax blocks.
$newbody = '';
$blocks = array();
$block_idx = 0;
$block_id = NULL;
$block_tag = NULL;
foreach ($tokens as $token)
{
// Not in a syntax block.
if ($block_id === NULL)
{
// See if we have a valid syntax starting block.
if (preg_match('/\[(code|syntax)[\s=]([^\]]+)\]/', $token, $t))
{
// Clean up quoting and spacing.
$options = str_replace(
array('"', '"', "'"),
'', trim($t[2])
);
// We can have multiple options.
$options = preg_split('/\s+/', $options);
// The first option always represents the language.
$lang = strtolower(array_shift($options));
// Check if the language name is valid.
if (preg_match('/^[\w_-]+$/', $lang))
{
// Check if the language exists.
$lang_file = GESHI_LANG_PATH.'/'.$lang.'.php';
if (file_exists($lang_file))
{
// Valid block start found.
// Initialize a new syntax block.
$block_idx ++;
$block_id = md5(microtime() .':'. $block_idx);
$temp_tag = "[bbcode_syntax $block_id]";
$block_tag = $t[1];
$newbody .= $temp_tag;
$blocks[$block_id] = array(
'', // block code storage
$lang, // the requested language
$temp_tag, // the temporary replacement tag
$options // additional options in the bbcode
);
continue;
}
}
}
// No start of block found. Add the token to the new body.
$newbody .= $token;
}
// In a syntax block.
else
{
// End of block found.
if ($token == '[/'.$block_tag.']')
{
$block_id = NULL;
continue;
}
// No end of block found. Add the token to the syntax block
$blocks[$block_id][0] .= $token;
}
}
$messages[$id]['body'] = $newbody;
$messages[$id]['bbcode_geshi'] = $blocks;
}
return $messages;
}
// Second stage formatting: replace all temporary placeholders with
// syntax highlighted code.
function phorum_mod_bbcode_geshi_format_fixup($messages)
{
$PHORUM = $GLOBALS['PHORUM'];
static $geshi = NULL;
foreach ($messages as $id => $message)
{
if (!empty($message['bbcode_geshi']))
{
// Initialize the GeSHi parser.
if ($geshi === NULL)
{
// Create the GeSHi object.
include_once(GESHI_PATH . '/geshi.php');
$geshi = new GeSHi('', 'php');
// We use the classes based output, for making the code
// less bloated.
$geshi->enable_classes();
}
foreach ($message['bbcode_geshi'] as $block)
{
list ($code, $lang, $temp_tag, $options) = $block;
// Remove Phorum's break tags.
$code = str_replace('<phorum break>', '', $code);
// Remove trailing white space and leading white lines.
$code = preg_replace('/^\s*[\r\n]+|\s+$/', '', $code);
// Unescape special HTML characters. GeSHi expects
// them to be not HTML escaped.
$code = str_replace(
array('<', '>', '"', ''', '&'),
array('<', '>', '"', "'", '&'),
$code
);
// Initialize the code and language for highlighting.
$geshi->set_source($code);
$geshi->set_language($lang);
$geshi->enable_classes(TRUE);
$geshi->set_overall_class('bbcode_geshi');
$geshi->set_header_content(
"Language: ".htmlspecialchars($geshi->get_language_name())
);
// Parse extra bbcode options.
$parsed_options = array(
'number' => 0,
'strict' => 0,
'nostrict' => 0
);
if (!empty($options))
{
foreach ($options as $option)
{
if (strstr($option, '=')) {
list ($key, $value) = explode('=', $option, 2);
} else {
$value = 1;
$key = $option;
}
$parsed_options[$key] = (int) $value;
}
}
// Enable line numbers if enabled in the settings and
// requested from the tag options.
if (!empty($PHORUM['mod_bbcode_geshi']['enable_line_numbers'])){
if (!empty($parsed_options['number'])) {
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$geshi->start_line_numbers_at($parsed_options['number']);
} else {
$geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS);
}
}
// Enable strict parsing mode if requested from the settings
// and not disabled by the tag options. The tag options are
// not on public display currently, because I think these
// would be too difficult to understand for the regular
// forum user.
if ((!empty($PHORUM['mod_bbcode_geshi']['enable_strict_mode'])||
!empty($parsed_options['strict'])) &&
empty($parsed_options['nostrict'])) {
$geshi->enable_strict_mode(TRUE);
} else {
$geshi->enable_strict_mode(FALSE);
}
// Let GeSHi highlight the code block.
$highlighted_code = $geshi->parse_code();
// Replace the temporary tag with the highlighted code block.
$messages[$id]['body'] = str_replace(
$temp_tag,
$highlighted_code,
$messages[$id]['body']
);
}
}
}
return $messages;
}
?>