SyntaxHighlighter v4.0.0

php

<?php
if (!defined('MEDIAWIKI'))
  exit(1);
 
$wgExtensionFunctions[] = "wfSyntaxHighlighterExtension";
 
$wgExtensionCredits['other'][] = array(
  'name' => 'SyntaxHighlighter',
  'author' => array('Alex Gorbatchev'),
  'version' => '1.0',
  'url' => 'http://alexgorbatchev.com/projects/syntaxhighlighter',
  'description' => 'Provides tight integration with SyntaxHighlighter.'
);
 
// Path to the SyntaxHighlighter scripts
$wgSyntaxHighlighterScriptPath = "{$wgScriptPath}/extensions/SyntaxHighlighter/scripts";
 
// Path to the SyntaxHighlighter styles
$wgSyntaxHighlighterStylesPath = "{$wgScriptPath}/extensions/SyntaxHighlighter/styles";
 
// Theme CSS file
$wgSyntaxHighlighterTheme = "shThemeDefault.css";
 
// Brushes to include on the page
$wgSyntaxHighlighterBrushes = array(
  'shBrushBash.js',
  'shBrushCpp.js',
  'shBrushCSharp.js',
  'shBrushCss.js',
  'shBrushDelphi.js',
  'shBrushDiff.js',
  'shBrushGroovy.js',
  'shBrushJava.js',
  'shBrushJScript.js',
  'shBrushPhp.js',
  'shBrushPlain.js',
  'shBrushPython.js',
  'shBrushRuby.js',
  'shBrushScala.js',
  'shBrushSql.js',
  'shBrushVb.js',
  'shBrushXml.js'
);
 
$dir = dirname(__FILE__) . '/';
 
function wfSyntaxHighlighterExtension()
{
  global $wgOut, $wgScriptPath, $wgParser,
      $wgSyntaxHighlighterBrushes, $wgSyntaxHighlighterScriptPath,
      $wgSyntaxHighlighterStylesPath, $wgSyntaxHighlighterTheme;
   
  // Make shCore.js the very first script to be included (before all the brushes)
  array_unshift($wgSyntaxHighlighterBrushes, 'shCore.js');
   
  $home = $wgSyntaxHighlighterScriptPath;
   
  // Add all scripts to the header
  foreach ($wgSyntaxHighlighterBrushes as $script)
      $wgOut->addScript("<script type=\"text/javascript\" src=\"{$wgSyntaxHighlighterScriptPath}/$script\"></script>\n");
 
  // Add CSS links
  foreach (array('shCore.css', $wgSyntaxHighlighterTheme) as $css)
      $wgOut->addHeadItem($css, "<link type=\"text/css\" rel=\"stylesheet\" href=\"{$wgSyntaxHighlighterStylesPath}/$css\"/>\n");
 
  // Add initialization code
  $wgOut->addScript(
      "<script type=\"text/javascript\">".
          "var syntaxHighlighterConfig = { clipboardSwf: '{$wgSyntaxHighlighterScriptPath}/clipboard.swf' };".
          "SyntaxHighlighter.highlight();".
      "</script>\n"
      );
   
  // Finally, set up a MediaWiki hook to the <sh /> tag
  $wgParser->setHook("sh", "wfSyntaxHighlighterExtensionRender");
   
  return true;
}
 
function wfSyntaxHighlighterExtensionRender($input, $argv, $parser)
{
  $args = "";
   
  while (list($key, $val) = each($argv))
      $args .= "$key: $val;";
   
  $input = htmlspecialchars($input);
  return "<pre class=\"$args\">\n$input\n</pre>";
}