phpBB

Development Wiki

Difference between revisions of "User:Pyramide/PHP Syntaxhighlighter for MediaWiki"

From phpBB Development Wiki

m (cnabora)
m (Reverted edits by ElorcAdelm (Talk); changed back to last version by Pyramide)
 
Line 1: Line 1:
letodronra
 
 
This is a simple PHP syntaxhighlighter for MediaWiki (inspired by the [http://meta.wikimedia.org/wiki/Syntax_Highlighting_Extension Syntax Highlighting Extension]). It only supports PHP but doesn't require any external tools. The formatting will look exactly like the code you see on this page.
 
This is a simple PHP syntaxhighlighter for MediaWiki (inspired by the [http://meta.wikimedia.org/wiki/Syntax_Highlighting_Extension Syntax Highlighting Extension]). It only supports PHP but doesn't require any external tools. The formatting will look exactly like the code you see on this page.
  

Latest revision as of 14:12, 26 December 2007

This is a simple PHP syntaxhighlighter for MediaWiki (inspired by the Syntax Highlighting Extension). It only supports PHP but doesn't require any external tools. The formatting will look exactly like the code you see on this page.

To install, just copy&paste the following code and save it as extensions/phpExtension.php:

<?php
$wgExtensionFunctions
[] = "phpExtension";

function 
phpExtension(){
    global 
$wgParser;
    
$wgParser->setHook("PHP""phpSyntaxHighlight");
}

function 
phpSyntaxHighlight($input$argv) {
    
//prepare input
    
$input trim($input);

    
//if we dont have php tags, add them
    
$strip_php false;
    if(
stristr($input'<'.'?php') === false) {
        
$input "<"."?php\n$input\n?".">";
        
$strip_php true;
    }

    
$output highlight_string($inputtrue);
    if(
$strip_php) {
        
$output str_replace(array('&lt;?php<br />''?&gt;'), ''$output);
    }

    
//filter out unnescessary stuff
    
$output str_replace(array('<code>''</code>'"\r""\n"), ''$output);
    
$output "<pre>$output</pre>";
    return 
$output;
}
?>

Then open LocalSettings.php and add this line:

require_once("extensions/phpExtension.php");

The extension should be ready to use now. Just wrap your PHP code inside <php>...</php> (you dont even need the leading space like for normale code sections).