-
Notifications
You must be signed in to change notification settings - Fork 1
/
SyntaxHighlight.body.php
34 lines (29 loc) · 1.19 KB
/
SyntaxHighlight.body.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
<?php
class SyntaxHighlight {
public static function wfSyntaxHighlight_Setup( Parser &$parser ) {
$parser->setHook('syntaxhighlight', array('SyntaxHighlight', 'wfSyntaxHighlight'));
return true;
}
public static function wfSyntaxHighlight( $text, array $args, Parser $parser, PPFrame $frame ) {
$code_classes = '';
if ( isset($args['lang']) && $args['lang'] ) {
$lang = $args['lang'];
$lang = strtolower($lang);
$code_classes = 'language-'.$lang;
}
if ( isset($args['class']) && $args['class'] ) {
$code_classes .= ' '.$args['class'];
}
// Replace all '&', '<,' and '>' with their HTML entitites. Order is important. You have to do '&' first.
$text = str_replace('&', '&', $text);
$text = str_replace('<', '<', $text);
$text = str_replace('>', '>', $text);
// Strip whitespace (or other characters) from the end of a string
$text = rtrim($text);
return '<pre class="mw-hljs"><code class="'.$code_classes.'">'.$text.'</code></pre>';
}
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
$out->addModules('ext.SyntaxHighlight');
return true;
}
}