User:Paxed/MixedSyntaxHighlight

From NetHackWiki
Jump to navigation Jump to search

This is a small extension that adds the magic word __MIXEDSYNTAXHIGHLIGHT__ and is used on the NetHackWiki source code pages to automatically hilite source lines with GeSHi (using SyntaxHighlight_GeSHi while still allowing interspersed wiki markup.

Each line that is supposed to be hilighted should be in the format " <span id="line1234">1234. line of code</span>" (Note the space at the beginning of the line) The files were normal C source code, mangled with Jayt's script.

Code for MixedSyntaxHighlight.php

<?php

if (!defined('MEDIAWIKI')) {
    die("Sorry.");
}

$wgExtensionCredits['parserhook'][] = array(
                                            'name' => 'MixedSyntaxHighlight',
                                            'version' => 0.1,
                                            'author' => '[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
                                            'description' => 'Add MagicWord "_<nowiki>_MIXEDSYNTAXHIGHLIGHT</nowiki>__".',
                                            'url' => 'http://nethackwiki.com/wiki/User:Paxed/MixedSyntaxHighlight',
                                            );

$mixedSyntaxHighlight = new MixedSyntaxHighlight();

$wgHooks['MagicWordMagicWords'][] = array($mixedSyntaxHighlight, 'addMagicWord');
$wgHooks['MagicWordwgVariableIDs'][] = array($mixedSyntaxHighlight, 'addMagicWordId');
$wgHooks['LanguageGetMagic'][] = array($mixedSyntaxHighlight, 'languageGetMagic');
$wgHooks['ParserBeforeStrip'][] = array($mixedSyntaxHighlight, 'parserBeforeStrip');

class MixedSyntaxHighlight {                
    //    private $mixed_hilite_enabled = false;                                                                                             

    function MixedSyntaxHighlight() { }

    function addMagicWord(&$magicWords) {
        $magicWords[] = 'MAG_MIXEDSYNTAXHIGHLIGHT';
        return true;
    }

    function addMagicWordId(&$magicWords) {
        $magicWords[] = MAG_MIXEDSYNTAXHIGHLIGHT;
        return true;
    }

    function languageGetMagic(&$magicWords, $langCode) {
        $magicWords[MAG_MIXEDSYNTAXHIGHLIGHT] = array( 0, "__MIXEDSYNTAXHIGHLIGHT__\n" );
        return true;
    }

    function parserBeforeStrip( &$parser, &$text ) {
        if (MagicWord::get(MAG_MIXEDSYNTAXHIGHLIGHT)->matchAndRemove( $text )) {
            $text = $this->doTaggingExchange($text, 'lang="c" line lineid="line"');
        }
        return true;
    }     
          
    function doTaggingExchange($text, $query='') {
        $tmp = preg_split("/\n/", $text);
        $text = '';
        $codeline = -1;

        foreach ($tmp as $line) {
            if (preg_match('/^ <span id=\"line([0-9]{1,4})">[0-9]{1,4}\. *(.*)<\/span>$/', $line, $matches)) {
                if ($codeline == -1) {
                    $codeline = $matches[1];
                    $text .= "<"."syntaxhighlight ".$query." start=\"".$codeline."\">\n";
                }
                $text .= $matches[2]."\n";
            } else {
                if ($codeline != -1) {
                    $text .= "\n<"."/syntaxhighlight>\n";
                }
                $codeline = -1;
                $text .= $line."\n";
            }
        }
        return $text;
    }
}

?>