Help:Sandkasten

From BlueM
Revision as of 04:32, 24 August 2006 by Froehlich (talk | contribs)
Jump to navigation Jump to search

<HIGHLIGHTSYNTAX language="php3"> <?php /**

* WikiMedia Extension for Code Syntax Highlighting with the [ http://www.beautifier.org | Beautifier Syntax Highlighting Engine].
*
* Parses any content defined within the <highlightSyntax></highlightSyntax> tag
* with the [ http://www.beautifier.org | Beautifier Syntax Highlighting Engine] and 
* returns the HTML output.
* @author Lucas Hrabovsky <lucas@amie.st>
* @copyright © 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"]){
               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();
       }
       /**
        * Sett 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 .= "

<pre>\n";
               $output .= $highlighter->highlight_text($highlighter->load_file($serverRoot.$argv["serverFile"]));
$output .= "

";

               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 .= "

Code from <a href=\"".$argv["remoteFile"]."\">".$argv["remoteFile"]."</a>

<pre>\n";
                       $output .= $highlighter->highlight_text($fileContents);
$output .= "

";

               }
               return $output;
       /**
        * Else we just want to highlight the code within the syntaxHighlight tags.
        */
       } else {

$output .= "

<pre>\n";
               $output .= $highlighter->highlight_text($input);
$output .= "

";

               return $output;
       }

} ?> </HIGHLIGHTSYNTAX>