GeSHi

From wikiZero

Jump to: navigation, search

GeSHi - Generic Syntax Highlighter

Since MediaWiki is written in PHP, why not test GeSHi with some PHP code, straight out of the source of course!

  1. <?php
  2.     /**
  3.      * Easy way to highlight stuff. Behaves just like highlight_string
  4.      *
  5.      * @param string The code to highlight
  6.      * @param string The language to highlight the code in
  7.      * @param string The path to the language files. You can leave this blank if you need
  8.      *               as from version 1.0.7 the path should be automatically detected
  9.      * @param boolean Whether to return the result or to echo
  10.      * @return string The code highlighted (if $return is true)
  11.      * @since 1.0.2
  12.      */
  13.     function geshi_highlight($string, $language, $path = null, $return = false) {
  14.         $geshi = new GeSHi($string, $language, $path);
  15.         $geshi->set_header_type(GESHI_HEADER_NONE);
  16.  
  17.         if ($return) {
  18.             return '<code>' . $geshi->parse_code() . '</code>';
  19.         }
  20.  
  21.         echo '<code>' . $geshi->parse_code() . '</code>';
  22.  
  23.         if ($geshi->error()) {
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28. }
  29. ?>

See also

Personal tools