# User:Paxed/MixedSyntaxHighlight
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' =&gt; 0.1,
                                            'author' =&gt; '[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
                                            'description' =&gt; 'Add MagicWord "_

<nowiki>_MIXEDSYNTAXHIGHLIGHT</nowiki>

__".',
                                            'url' =&gt; '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(&amp;$magicWords) {
        $magicWords[] = 'MAG_MIXEDSYNTAXHIGHLIGHT';
        return true;
    }

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

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

    function parserBeforeStrip( &amp;$parser, &amp;$text ) {
        if (MagicWord::get(MAG_MIXEDSYNTAXHIGHLIGHT)-&gt;matchAndRemove( $text )) {
            $text = $this-&gt;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}\\. \*(.\*)&lt;\\/span&gt;$/', $line, $matches)) {
                if ($codeline == -1) {
                    $codeline = $matches\[1\];
                    $text .= "&lt;"."syntaxhighlight ".$query." start=\\"".$codeline."\\"&gt;\\n";
                }
                $text .= $matches\[2\]."\\n";
            } else {
                if ($codeline != -1) {
                    $text .= "\\n&lt;"."/syntaxhighlight&gt;\\n";
                }
                $codeline = -1;
                $text .= $line."\\n";
            }
        }
        return $text;
    }
}

?&gt;

</span>