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 TRUE; |
59 |
} |
60 |
global $wgMessageCache; |
61 |
require_once( dirname( __FILE__ ) . '/ashighlight.i18n.php' ); |
62 |
foreach( efashighlightMessages() as $lang => $messages ) |
63 |
$wgMessageCache->addMessages( $messages, $lang ); |
64 |
return TRUE; |
65 |
} |
66 |
|
67 |
// called anytime a 'source' tag is seen in the wikitext... |
68 |
function ashighlightHook( $text, $params = array(), $parser ) { |
69 |
if ( !class_exists( 'ASHighlight' ) ) { |
70 |
require( 'ashighlight.class.php' ); |
71 |
} |
72 |
ashighlightLoadMessages(); |
73 |
return isset( $params['lang'] ) |
74 |
? ashighlightFormat( trim( $text ), $params, $parser ) |
75 |
: ashighlightHelp(); |
76 |
} |
77 |
|
78 |
// format the passed $text in the language $params['lang'] |
79 |
function ashighlightFormat( $text, $params, $parser ) { |
80 |
$lang = $params['lang']; |
81 |
if ( !preg_match( '/^[A-Za-z_0-9-]*$/', $lang ) ) { |
82 |
return ashighlightHelp( wfMsgHtml( 'ashighlight-err-language' ) ); |
83 |
} |
84 |
|
85 |
$ash = new ASHighlight(); |
86 |
|
87 |
$ash->set_encoding('UTF-8'); |
88 |
|
89 |
if(isset($params['tabwidth'])){ |
90 |
$ash->set_tab_width($params['tabwidth']); |
91 |
} |
92 |
|
93 |
if(isset( $params['line'] ) ) { |
94 |
$ash->enable_line_numbers(); |
95 |
} |
96 |
|
97 |
if(isset( $params['start'] ) ) { |
98 |
$ash->start_line_numbers_at( $params['start'] ); |
99 |
} |
100 |
|
101 |
$out = $ash->parse_code($text,$lang); |
102 |
|
103 |
if($ash->error){ |
104 |
return ashighlightHelp($ash->errmsg); |
105 |
}else{ |
106 |
// Per-language class for stylesheet |
107 |
//$ASH_CSS[]= |
108 |
$css = |
109 |
"<style type=\"text/css\">/*<![CDATA[*/\n".$ash->get_stylesheet()."/*]]>*/</style>\n"; |
110 |
$out = $css . "<pre>$out</pre>"; |
111 |
return $out; |
112 |
} |
113 |
} |
114 |
|
115 |
/** |
116 |
Return a syntax help message |
117 |
@param string $error HTML error message |
118 |
*/ |
119 |
function ashighlightHelp( $error = false ) { |
120 |
return ashighlightError( |
121 |
( $error ? "<p>$error</p>" : '' ) . |
122 |
'<p>' . wfMsg( 'ashighlight-specify' ) . ' ' . |
123 |
'<samp><source lang="html">...</source></samp></p>' . |
124 |
'<p>' . wfMsg( 'ashighlight-supported' ) . '</p>' . |
125 |
ashighlightFormatList( ashighlightLanguageList() ) ); |
126 |
} |
127 |
|
128 |
/** |
129 |
* Put a red-bordered div around an HTML message |
130 |
* @param string $contents HTML error message |
131 |
* @return HTML |
132 |
*/ |
133 |
function ashighlightError( $contents ) { |
134 |
return "<div style=\"border:solid red 1px; padding:.5em;\">$contents</div>"; |
135 |
} |
136 |
|
137 |
function ashighlightFormatList( $list ) { |
138 |
return empty( $list ) |
139 |
? wfMsg( 'ashighlight-err-loading' ) |
140 |
: '<p style="padding:0em 1em;">' . |
141 |
implode( ', ', array_map( 'ashighlightListItem', $list ) ) . |
142 |
'</p>'; |
143 |
} |
144 |
|
145 |
function ashighlightListItem( $item ) { |
146 |
return "<samp>" . htmlspecialchars( $item ) . "</samp>"; |
147 |
} |
148 |
|
149 |
function ashighlightLanguageList() { |
150 |
$langs = array(); |
151 |
$langroot = @opendir( ASHIGHLIGHT_LANG_ROOT ); |
152 |
if( $langroot ) { |
153 |
while( $item = readdir( $langroot ) ) { |
154 |
if( preg_match( '/^(.*)\\.lang$/', $item, $matches ) ) { |
155 |
$langs[] = $matches[1]; |
156 |
} |
157 |
} |
158 |
closedir( $langroot ); |
159 |
} |
160 |
sort( $langs ); |
161 |
return $langs; |
162 |
} |
163 |
|
164 |
function ashighlightCss(&$out){ |
165 |
//foreach($ASH_CSS as $css){ |
166 |
// $out.=$css."\n\n"; |
167 |
//} |
168 |
$out.="kwa{font:bold}"; |
169 |
return true; |
170 |
} |
171 |
|
172 |
?> |