Extension:Syntax Highlighting



About 
The highlight syntax extension is a really simple way for you to add syntax highlighting to your wiki posts with support with several languages. The extension requires the Beautifier Syntax Highlighting Engine. What the extension does is pass the tag inputs and/or arguements to the Beautifier Highlighting engine and returns the HTML output.

Installation

 * 1) Download the Beautifier PHP Package from the Beautifier.org Site.
 * 2) *This is a dead link. But these mirrors work: mirrorservice planetmirror.
 * 3) *Use the php version which is partway down the list
 * 4) Extract the tarball somewhere on your remote server.  (I suggest to a subdirectory of your wiki directory named "beautifierpackage")
 * 5) *In the code he uses require_once("extensions/syntaxHighlightExtension.php"); so name your file as such "extensions/syntaxHighlightExtension.php".
 * 6) Copy all the text in the code section below into a new PHP file. (maybe you have to replace all &amp;lt; with < and &amp;gt; with >).
 * 7) Edit the $BEAUT_PATH, LOCAL_SERVER_ROOT, and $OUTPUT_HIGHLIGHT_AS to localize the extension for your system.
 * 8) Save the new PHP file to your server as syntaxHighlightExtension.php in your MediaWiki extension folder.
 * 9) Open the LocalSettings.php file in the root of your MediaWiki and add the following to the end of the file:

That's it! You're all installed. There are a lot of tweaks you can make to customize beautifier to make things simpler and add some new functions to their framework. Enjoy.

The Code
* @author Lucas Hrabovsky  * @copyright &copy; 2006 Amie Inc. * @package MediaWiki * @subpackage Extensions * @filesource */ /** * * Configuration Vars. * * @string BEAUT_PATH The root path to your Beatuifier installation. * @string LOCAL_SERVER_ROOT The root path to use when opening files on the local server for highlighting. * @string OUTPUT_HIGHLIGHT_AS Whether to output the HTML as CSS friendly or not. enum('CSS','HTML') */ $BEAUT_PATH = "/path/to/your/beautifier/installation/"; $LOCAL_SERVER_ROOT = "/pathtoyour/public_html/or/www/directory/"; $OUTPUT_HIGHLIGHT_AS = "HTML"; /** * Add the syntax highlighting extension to the wgExtensionFunctions array. */ $wgExtensionFunctions[] = "syntaxHighlightingExtension"; /** * Register the syntaxHighlightingExtension with the Wiki Parser and associate * the highlightSyntax tag with the syntaxHighlightingRenderHtml function. */ function syntaxHighlightingExtension{ global $wgParser; $wgParser->setHook("HIGHLIGHTSYNTAX","syntaxHighlightingRenderHtml"); } /** * The callback function for converting the input text to HTML output. */ function syntaxHighlightingRenderHtml($input, $argv){ /**	 * Pass in the globals defined above for use in the function. */	global $BEAUT_PATH, $LOCAL_SERVER_ROOT, $OUTPUT_HIGHLIGHT_AS; /**	 * Require the two main files for the beautifier. */	require_once ($BEAUT_PATH."Beautifier/Core.php");

/**        * If a specific language is requested load the relevant file and create the highlighter */       $myhfile = null; if($argv["language"] && file_exists($BEAUT_PATH."HFile/HFile_".$argv["language"].".php") ){ require_once ($BEAUT_PATH."HFile/HFile_".$argv["language"].".php");

if (FALSE === eval("\$myhfile = new HFile_".$argv["language"].";") ) { $output .= "Error:1 Could not instantiate HFile_".$argv["language"]." "; }

}

/**        * Otherwise use php as a default */       else {               require_once ($BEAUT_PATH."HFile/HFile_php3.php"); $myhfile = new HFile_php3; }

/**        * Set up the highlight object based on whether * you want the output as CSS freindly or straight HTML. */       if($OUTPUT_HIGHLIGHT_AS=="CSS"){ require_once ($BEAUT_PATH."Output/Output_css.php"); $outputter = new Output_css; }       else { require_once ($BEAUT_PATH."Output/Output_HTML.php"); $outputter = new Output_Html; }

$highlighter = new Core($myhfile, $outputter);

/**	 * The meat and potatoes... */

/**	 * If the serverFile argument is passed in the tag, load the local file into the highlighter parser. */ 	if($argv["serverFile"]){ $output .= "&lt;div style=\"font-weight: bold;\">&lt;pre&gt;\n"; $output .= $highlighter->highlight_text($highlighter->load_file($serverRoot.$argv["serverFile"])); $output .= "&lt;/pre&gt;&lt;/div&gt;"; return $output; /**	 * If its a remote file, open it up and parse the returned contents. */	} else if($argv["remoteFile"]){ $openFileName = $argv["remoteFile"]; $fp = fopen($openFileName, "r"); if(!$fp){ $output = "Error: Syntax Highlighter could not open the file ".$openFileName." "; } else { $fileContents = fread($fp, 8096); fclose($fp); $output .= "&lt;div style=\"font-weight: bold;\"&gt;&lt;h1&gt;Code from &lt;a href=\"".$argv["remoteFile"]."\"&gt;".$argv["remoteFile"]."&lt;/a>&lt;/h1&gt;&lt;pre&gt;\n"; $output .= $highlighter->highlight_text($fileContents); $output .= "&lt;/pre&gt;&lt;/div&gt;"; }		return $output; /**	 * Else we just want to highlight the code within the syntaxHighlight tags. */	} else { $output .= "&lt;div style=\"font-weight: bold;\"&gt;&lt;pre>\n"; $output .= $highlighter->highlight_text($input); $output .= "&lt;/pre&gt;&lt;/div&gt;"; return $output; } }

Improving the Highlighter
The output of the highlighter as downloaded from the beautifier web site contains hard coded colours even though the highlighter classes specify colours to use. I re-wrote the HTML outputter class in our installation to use the colours specified in the highlighter file.

To do the same you will need to pass the highlighter into the output class by changing the line to

And then replace the Output_HTML class in Output\Output_HTML.php with the following.

This only changes the HTML outputter, to change the CSS one in a similar fashion would be more complex since it relies on CSS classes to provide the different formatting. I guess you could write these classes out at the start of the block, but I'm not sure how the browser would react to having the styles redefined if you included multiple blocks using different languages.

Inline Highlighting
PHP Code

Code in a specific language

Highlight a file on your local server
or