MediaWiki扩展:语法高亮处理
本文取自元维基,欢迎共同翻译、整理与大家共享,促进中文wiki发展^_^This extension can be seen in action on the Ciscavate.org wiki: The GeSHiHighlight main page is at Ciscavate:MediaWiki_Extensions
The advantages of GeSHiHighlight over GeSHiColor are 1) it handles more langugages, 2) is still being maintained, and 3) seems to work better in general, although I can't vouch for that personally.
(there is a more active-seeming page for GeSHiHilight at: User:Ajqnic:GeSHiHighlight
History (Brief!)
The pre tags in mediawiki are nice, but they don't quite cut it for source code. With that in mind, I went questing for a syntax highlighting extension, and found "SyntaxHighlight" (http://www.wickle.com/wikis/index.php/Syntax_Highlight_extension) by Coffman, but I disagree with the use of two tags--one to specify you are in a code block, and another to specify the language:
<code><perl/> foreach(@myarr){print $_;}</code>
To get arround that, I hacked Coffman's 1.2 release to accept language tags:
<perl>foreach(@myarr){print $_;}</perl>
<java>public class foo{ /* .... */ }</java>
That version is available here: ...... but I'm not planning on updating it again.
There is another drawback--SyntaxHighlight uses enscript, which (IMO) produces horrible results---better than just pre when dealing with source code, but I want more than comments, strings and keywords to be fontified.
Enter GeSHi.
MediaWiki and GeSHi
GeSHi (found here: http://qbnz.com/highlighter/index.php ) is a syntax highlighing library written in php. I'm new to php (and by new, I mean I wrote my first line of php code about 4 hours prior writing this--see above) and I was amazed at how easy it was to use GeSHi (this says nothing about my love/hate for php in general...).
Why GeSHi?
* Better highlighting. Flat-out kicks enscript's ass. I'm not fond of the default colors, but that can be dealt with later.
* php. enscript, being an external program was writing files with md5hash names, which caused update issues when I was testing, command line params changed causing issues, not quite as elegant. (Using an external program was also my first inclination when thinking about this problem--I actually almost discarded GeSHi because of this narrow-mindedness.)
* It seems to be actively developed. The release I'm using now is only about a week old.
So, where's the source? Here:
<?php
# GeSHiHighlight.php
#
# By: E. Rogan Creswick (aka: Largos)
# creswick@gmail.com
# wiki.ciscavate.org
#
# License: GeSHi Highlight is released under the Gnu Public License (GPL), and comes with no warranties.
# The text of the GPL can be found here: http://www.gnu.org/licenses/gpl.html
# Loosely based on SyntaxHighlight.php by Coffman, (www.wickle.com)
class SyntaxSettings {};
$wgSyntaxSettings = new SyntaxSettings;
$wgExtensionFunctions[] = "wfSyntaxExtension";
function wfSyntaxExtension() {
global $wgParser;
$langArray = geshi_list_languages("extensions\\geshi\\geshi");
# $langArray = array("actionscript","ada","apache","asm","asp","bash",
# "caddcl","cadlisp","c","cpp","css","delphi",
# "html4strict","java","javascript","lisp", "lua",
# "nsis","oobas","pascal","perl","php-brief","php",
# "python","qbasic","sql","vb","visualfoxpro","xml");
foreach ( $langArray as $lang ){
if ($lang == "") continue;
$wgParser->setHook( $lang,
create_function( '$text', '$geshi = new GeSHi(rtrim(ltrim($text,"\n\r")), ' . $lang . ', "includes/geshi");
return $geshi->parse_code();'));
}
}
/**
* function: geshi_list_languages
* -------------------------
* List supported languages by reading the files in the geshi/geshi subdirectory
* (added by JeffK -- Jeff, any more contact info?)-- I haven't tested the code is is, will do that shortly. -Rogan
*
*/
function geshi_list_languages ( $path = 'geshi/' )
{
if ($handle = opendir($path)) {
/* Loop over the directory. */
while (false !== ($file = readdir($handle))) {
/* Drop the current and parent dir entries */
if( "." !== $file && ".." !== $file )
{
/* Drop files that dont end with .php */
if( ".php" == substr($file, strrpos($file, "."),4))
{
$lang_list .= substr($file, 0, strrpos($file, ".")).",";
}
}
}
closedir($handle);
}
return explode(",", $lang_list);
}
?>
Yep, that's IT. Bang. pretty syntax highlighting with 28 language tags, for 28 languages (disclaimer: I haven't tested all these, and I have suspicions about html4strict working out of the box, but still.)
Installation
* Install GeSHi per the GeSHi install instructions (link above)
* Don't forget to include GeSHi also:
include_once("geshi.php")* Either cut & paste the above, or downloade GeSHiHighlight.php here: add file
* Put GeSHiHighlight.php in your mediaWiki extensions folder
* Add:
include("extensions/GeSHiHighlight.php");
to LocalSettings.php
页:
[1]