User:Austin J. Che/Extensions/SyntaxHighlight

From OpenWetWare

Jump to: navigation, search

<syntax type="php" file="http://austinche.name/mediawiki/SyntaxHighlight.php.txt"> produces the colorful source code of this extension:

<?php
/**
 * Syntax highlighting using GeSHi (http://qbnz.com/highlighter/)
 * Based on code from Andrew Nicol (http://meta.wikimedia.org/wiki/User:Ajqnic:GeSHiHighlight)
 *
 * Wiki syntax used is:
 * <syntax type="my-language">...</syntax> (highlight given text)
 * <syntax type="my-language" file="wiki file" /> (highlight uploaded file)
 * <syntax type="my-language" file="http://..." /> (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 <li> 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 <div> on the line).
        // So the source output looks like this:
        //
        // <pre style="[set_overall_style styles]"><ol>
        // <li style="[set_line_style styles]"><div style="[set_code_style styles]>...</div></li>
        // ...
        // </ol></pre>
        $geshi->set_code_style('color: #000020;', 'color: #000020;');
        */
        $geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true);
 
        // Styles for hyperlinks in the code. GESHI_LINK for default styles, GESHI_HOVER for hover style etc...
        // note that classes must be enabled for this to work.
        $geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
        $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');
 
        return "<style>".$geshi->get_stylesheet()."</style>".$geshi->parse_code();
    }
 
}
?>
 
Personal tools