How to enable PHP color coding in MediaWiki

How to enable PHP color coding in MediaWiki
Jump to: navigation, search

Note: The colorCodePhp function should be enhanced to escape control codes that cause problems with MediaWiki. For example, replace "%%%" with "&#37;&#37;&#37;", replace "<pre>" with "&lt;pre>" and "</pre>" with "&lt;/pre>", and replace '' with &#39;&#39;.
Eric -- March 25, 2006

[edit] How to enable PHP color coding in MediaWiki

By NuCleuZ -- 23/08/2004

Here’s the very quick ( and dirty ) way of color coding PHP in mediawiki ( as seen on http://wiki.cc/php ).

1. $ cd mediawiki/includes/

2. $ vim Parser.php +810

3. Paste the following code:

    function colorCodePhp( $text )
    {
        $text = explode( "%%%", $text );
        $i = 0;
        $ret = '';
        foreach( $text as $k )
        {
            if( $i % 2 == 0 )
            {
                $ret .= $k;
            }
            else
            {
                $ret .= '<pre>' . highlight_string( $k, TRUE ) . '</pre>';
            }
            $i++;
        }
        return $ret;
    }

4. go to function internalParse()

5. Insert

$text = $this->colorCodePhp( $text );

before

$text = $this->removeHTMLtags( $text );

( you will need to do the PHP color before all the html transforming takes place )

6. Save and hit the mediawiki with color coded PHP :)


This will reformat

%%%
<?php
    $code = "here";
/>?>
%%%

in the wiki as: %%%<?php

  $code = "here"; 

?>%%%

Here's what the colorCodePhp function looks like, with interactions between the MediaWiki and PHP parsers noted:

%%%<?php

   function colorCodePhp( $text )
   {
       # PHP formatter recognizes three %'s, hence x between % and %%
       $text = explode( "%x%%", $text ); 
       $i = 0;
       # WikiMedia interprets two 's together as emphasis code
       $ret = ' '; 
       foreach( $text as $k )
       {
           if( $i % 2 == 0 )
           {
               $ret .= $k;
           }
           else
           {
               # Wikimedia interprets the pre and /pre tags if intact
               $ret .= '<' . 'pre>' . highlight_string( $k, TRUE ) .  '<' . '/pre>';
           }
           $i++;
       }
       return $ret;
   }

?>%%%


Can it be done more efficient? yes.
Can it be done with other tags? yes.
Did it take much time? no.
Does it work? yes :)


Source: http://www.phpvolcano.com/blog/index.php?/archives/151-How-to-enable-PHP-color-coding-in-Mediawiki.html