Highlight Test Two

  1. // factorial(4) => 24
  2. function factorial(i)
  3. {
  4.   // tail-recursive helper
  5.   var fac_helper = function(i, acc)
  6.   {
  7.     if (i < 2)
  8.       return acc;
  9.     else
  10.       return fac_helper(i - 1, acc * i);
  11.   }
  12.   return fac_helper(i, 1);
  13. }
WHAT'S HAPPENING HERE:

The PHP engine on my server is performing syntax highlighting on my code for your viewing pleasure. Ooh, look at the pretty colors! See the line numbers!

The PHP being run is the excellent GeSHi - Generic Syntax Highlighter.

HOW DID I DO THAT:

Here is the "PHP code" used for the above:

<?php
//
// Include the GeSHi library
//
require_once DRUPAL_ROOT . '/includes/geshi.php';
 
//
// Define some source to highlight, a language to use
// and the path to the language files
//
$source = <<<EOF
// factorial(4) => 24
function factorial(i)
{
  // tail-recursive helper
  var fac_helper = function(i, acc)
  {
    if (i < 2)
      return acc;
    else
      return fac_helper(i - 1, acc * i);
  }
  return fac_helper(i, 1);
}
EOF;
$language = 'javascript';
 
//
// Create a GeSHi object
//
$geshi = new GeSHi($source, $language);

//
// Enable line numbers
//
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);

//
// Wrap the output in a <div> rather than a <pre> for a larger font
//
$geshi->set_header_type(GESHI_HEADER_DIV);

//
// And echo the result!
//
echo $geshi->parse_code();
?>