... (highlight given text)
* (highlight uploaded file)
* (highlight file from web)
*
* Code released under the GPL http://www.gnu.org/licenses/gpl.html
* Author: Austin Che
*/
include_once('geshi/geshi.php');
define('SYNTAX_HIGHLIGHT_MAX_BYTES_FROM_FILE', 128 * 1024); // shouldn't be highlighting more text than this
$wgExtensionFunctions[] = "wfSyntaxHighlightExtension";
$wgExtensionCredits['other'][''] = array('name' => 'SyntaxHighlight',
'version' => '2007/03/18',
'author' => 'Austin Che',
'url' => 'http://openwetware.org/wiki/User:Austin/Extensions/SyntaxHighlight',
'description' => 'Allows for syntax highlighting using GeSHi');
function wfSyntaxHighlightExtension()
{
global $wgParser, $wgSyntaxHighlight;
$wgSyntaxHighlight = new SyntaxHighlight;
$wgParser->setHook("syntax", array(&$wgSyntaxHighlight, "syntaxTag"));
}
class SyntaxHighlight
{
function syntaxTag($text, $argv, &$parser)
{
// if "type" argument is not given or is unknown, we let GeSHi do whatever it does by default
$type = $argv["type"];
if ($argv["file"])
{
$file = $argv["file"];
if (preg_match("@^http://@", $file))
{
// get a remote url via fopen
$fp = @fopen($file, "r");
if (!$fp)
return $text;
$text = stream_get_contents($fp, SYNTAX_HIGHLIGHT_MAX_BYTES_FROM_FILE);
fclose($fp);
}
else
{
// treat as an uploaded file on the wiki
$image = new Image(Title::makeTitle(NS_IMAGE, $file));
if (! $image->exists())
return $text;
$text = file_get_contents($image->getImagePath(), SYNTAX_HIGHLIGHT_MAX_BYTES_FROM_FILE);
}
// maybe disable parser cache?
//$parser->disableCache();
}
return $this->highlight($text, $type);
}
function highlight($text, $type)
{
$geshi = new GeSHi($text, $type);
$geshi->enable_classes();
$geshi->set_header_type(GESHI_HEADER_PRE);
//$geshi->set_overall_class("code");
$geshi->set_encoding("utf-8");
// Set the style for the PRE around the code. The line numbers are contained within this box (not
// XHTML compliant btw, but if you are liberally minded about these things then you'll appreciate
// the reduced source output).
$geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true);
/*
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
// Set the style for line numbers. In order to get style for line numbers working, the
element
// is being styled. This means that the code on the line will also be styled, and most of the time
// you don't want this. So the set_code_style reverts styles for the line (by using a on the line).
// So the source output looks like this:
//
//
// ".$geshi->get_stylesheet()."".$geshi->parse_code();
}
}
?>