1 |
<?php |
2 |
/** |
3 |
* Syntax-highlight extension for MediaWiki 1.9 using Andre Simon's 'highlight' |
4 |
* Copyright (C) 2007 John Pye <john@curioussymbols.com> |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License along |
17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
18 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
* http://www.gnu.org/copyleft/gpl.html |
20 |
*/ |
21 |
|
22 |
/** |
23 |
* @addtogroup Extensions |
24 |
* @author John Pye |
25 |
* |
26 |
* This extension wraps the Andre Simon highlighter: http://www.andre-simon.de/ |
27 |
* |
28 |
* Modelled closely on the sourcecode for the GeSHi highlighter for |
29 |
* mediawiki, by Brion Vibber http://www.mediawiki.org/ |
30 |
* |
31 |
* A language is specified like: <source lang="c">void main() {}</source> |
32 |
* If you forget, or give an unsupported value, the extension spits out |
33 |
* some help text and a list of all supported languages. |
34 |
*/ |
35 |
|
36 |
if( !defined( 'MEDIAWIKI' ) ) |
37 |
die(); |
38 |
|
39 |
$wgExtensionFunctions[] = 'ashighlightSetup'; |
40 |
$wgExtensionCredits['parserhook']['ashighlight'] = array( |
41 |
'name' => 'ashighlight', |
42 |
'author' => 'John Pye', |
43 |
'description' => "Provides syntax highlighting using [http://www.andre-simon.de/ Andre Simon's 'highlight']", |
44 |
); |
45 |
$wgHooks['LoadAllMessages'][] = 'ashighlightLoadMessages'; |
46 |
|
47 |
$ASH_CSS=array(); |
48 |
|
49 |
function ashighlightSetup() { |
50 |
global $wgParser; |
51 |
$wgParser->setHook( 'source', 'ashighlightHook' ); |
52 |
$wgHooks['SkinTemplateSetupPageCss'][]='ashighlightCss'; |
53 |
} |
54 |
|
55 |
function ashighlightLoadMessages() { |
56 |
static $loaded = false; |
57 |
if ( $loaded ) { |
58 |
return; |
59 |
} |
60 |
global $wgMessageCache; |
61 |
require_once( dirname( __FILE__ ) . '/ashighlight.i18n.php' ); |
62 |
foreach( efashighlightMessages() as $lang => $messages ) |
63 |
$wgMessageCache->addMessages( $messages, $lang ); |
64 |
} |
65 |
|
66 |
// called anytime a 'source' tag is seen in the wikitext... |
67 |
function ashighlightHook( $text, $params = array(), $parser ) { |
68 |
if ( !class_exists( 'ASHighlight' ) ) { |
69 |
require( 'ashighlight.class.php' ); |
70 |
} |
71 |
ashighlightLoadMessages(); |
72 |
return isset( $params['lang'] ) |
73 |
? ashighlightFormat( trim( $text ), $params, $parser ) |
74 |
: ashighlightHelp(); |
75 |
} |
76 |
|
77 |
// format the passed $text in the language $params['lang'] |
78 |
function ashighlightFormat( $text, $params, $parser ) { |
79 |
$lang = $params['lang']; |
80 |
if ( !preg_match( '/^[A-Za-z_0-9-]*$/', $lang ) ) { |
81 |
return ashighlightHelp( wfMsgHtml( 'ashighlight-err-language' ) ); |
82 |
} |
83 |
|
84 |
$ash = new ASHighlight(); |
85 |
|
86 |
$ash->set_encoding('UTF-8'); |
87 |
|
88 |
if(isset($params['tabwidth'])){ |
89 |
$ash->set_tab_width($params['tabwidth']); |
90 |
} |
91 |
|
92 |
if(isset( $params['line'] ) ) { |
93 |
$ash->enable_line_numbers(); |
94 |
} |
95 |
|
96 |
if(isset( $params['start'] ) ) { |
97 |
$ash->start_line_numbers_at( $params['start'] ); |
98 |
} |
99 |
|
100 |
$out = $ash->parse_code($text,$lang); |
101 |
|
102 |
if($ash->error){ |
103 |
return ashighlightHelp($ash->errmsg); |
104 |
}else{ |
105 |
// Per-language class for stylesheet |
106 |
//$ASH_CSS[]= |
107 |
$css = |
108 |
"<style type=\"text/css\">/*<![CDATA[*/\n".$ash->get_stylesheet()."/*]]>*/</style>\n"; |
109 |
$out = $css . "<pre>$out</pre>"; |
110 |
return $out; |
111 |
} |
112 |
} |
113 |
|
114 |
/** |
115 |
Return a syntax help message |
116 |
@param string $error HTML error message |
117 |
*/ |
118 |
function ashighlightHelp( $error = false ) { |
119 |
return ashighlightError( |
120 |
( $error ? "<p>$error</p>" : '' ) . |
121 |
'<p>' . wfMsg( 'ashighlight-specify' ) . ' ' . |
122 |
'<samp><source lang="html">...</source></samp></p>' . |
123 |
'<p>' . wfMsg( 'ashighlight-supported' ) . '</p>' . |
124 |
ashighlightFormatList( ashighlightLanguageList() ) ); |
125 |
} |
126 |
|
127 |
/** |
128 |
* Put a red-bordered div around an HTML message |
129 |
* @param string $contents HTML error message |
130 |
* @return HTML |
131 |
*/ |
132 |
function ashighlightError( $contents ) { |
133 |
return "<div style=\"border:solid red 1px; padding:.5em;\">$contents</div>"; |
134 |
} |
135 |
|
136 |
function ashighlightFormatList( $list ) { |
137 |
return empty( $list ) |
138 |
? wfMsg( 'ashighlight-err-loading' ) |
139 |
: '<p style="padding:0em 1em;">' . |
140 |
implode( ', ', array_map( 'ashighlightListItem', $list ) ) . |
141 |
'</p>'; |
142 |
} |
143 |
|
144 |
function ashighlightListItem( $item ) { |
145 |
return "<samp>" . htmlspecialchars( $item ) . "</samp>"; |
146 |
} |
147 |
|
148 |
function ashighlightLanguageList() { |
149 |
$langs = array(); |
150 |
$langroot = @opendir( ASHIGHLIGHT_LANG_ROOT ); |
151 |
if( $langroot ) { |
152 |
while( $item = readdir( $langroot ) ) { |
153 |
if( preg_match( '/^(.*)\\.lang$/', $item, $matches ) ) { |
154 |
$langs[] = $matches[1]; |
155 |
} |
156 |
} |
157 |
closedir( $langroot ); |
158 |
} |
159 |
sort( $langs ); |
160 |
return $langs; |
161 |
} |
162 |
|
163 |
function ashighlightCss(&$out){ |
164 |
//foreach($ASH_CSS as $css){ |
165 |
// $out.=$css."\n\n"; |
166 |
//} |
167 |
$out.="kwa{font:bold}"; |
168 |
return true; |
169 |
} |
170 |
|
171 |
?> |