CSO Source tag and extension

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Disclaimer: This software is offered free and "as is". Use at your own risk. We assume no liability for its use or misuse.

The CSO_Source extension perfroms syntax highlighting based on the specified source language, e.g. C#, Perl. The original version of this extension was created at Swinburne University down under.

CSO_Source tag usage example:

<CSO_Source >...</CSO_Source > 

View more complex usage examples.

CSO_Source uses GeSHi—Generic Syntax Highlighter. You must download and install GeSHi. I recommend you install it in your /extensions folder. Then. be sure that the extension knows where to find GeSHi by making the following two lines point to it:

# The following two lines require correct paths:
include_once('extensions/geshi/geshi.php');
define('CSH_GESHI_PATH','extensions/geshi/geshi/');

In this case, the directory structure looks like this:

/extensions
   /geshi
      geshi.php
      /geshi

The nested /geshi folder contains the language-specific PHP files.

To register the extension, add this line to <mediawikihome>/LocalSettings.php:

include ("extensions/CSO_Source.php");

And, place the CSO_Source extension in the <mediawikihome>/extensions directory.

<?php
# CSO_Source - source code syntax highlighting extension
# CSharp-Online.NET (C) 2006 Chang & Wagers Associates
# Uses the GeSHi engine: See http://qbnz.com/highlighter/
# Original by Clinton Woodward cwoodward@swin.edu.au for SwinBrain
#
# Usage:
#  &lt;CSO_Source>&#091;&#091;lang,line_no,start_line_no,range(m-n,x-y)]]...&lt;/CSO_Source>
#
# Usage examples:
#  &lt;CSO_Source>&#091;&#091;lang]]...&lt;/CSO_Source> 		
// no line numbers is default
#  &lt;CSO_Source>&#091;&#091;lang,5]]...&lt;/CSO_Source> 
// start line numbers at 5
#  &lt;CSO_Source>&#091;&#091;lang,1,(3-5,7)]]...&lt;/CSO_Source>
// start line numbers at 1,
#										
//highlight lines 3,4,5 and 7
#  - lang is required, others are optional
#
# To activate the extension, add this line to LocalSettings.php:
# include("extensions/CSO_Source.php");
 
# The following two lines require correct paths:
include_once('extensions/geshi/geshi.php');
define('CSH_GESHI_PATH','extensions/geshi/geshi/');
 
$wgExtensionFunctions[] = 'wfCSO_Source';
 
$cshLanguages = array (
  'actionscript','ada','apache','asm','c','cpp','eiffel','ini','html','xhtml',
  'java','java5','css','js','javascript','vbnet','csharp','pascal','xml','php',
  'delphi','bash','perl','lisp','matlab','mpasm','objc','vb','smarty','vhdl',
  'ruby','sql','python','pseudocode','qbasic','scheme','oracle8','dos');
 
function wfCSO_Source()
   {
   global $wgParser, $cshLanguages;
   # register the extension with the WikiText parser
   $wgParser->setHook('CSO_Source', 'CSO_Source');
   }
 
# The callback function for converting the input text to HTML output
function CSO_Source ($source)
   {
   global $cshLanguages;
 
   // extract the required args [...]
   $lines = explode("\n",trim($source));
   $args = trim($lines[0]);
 
   // defaults
   $lang = 'csharp';
   $line_no    = 'N';
   $line_start = 1;
   $line_range = '';
 
   // Is there a [..] section?
   if (strpos($args,'&#091;&#091;')===0 && strpos($args,']]')!==false)
      {
      // extract the args values, trim off the &#091;&#091;]] characters.
      $eofargspos = strpos($args, ']]');
      $args = explode(',', substr($args, 2, $eofargspos - 2));
      // Language? Is it one of the ones we have enabled?
      if (in_array($args[0],$cshLanguages))
         {
      	 $lang = $args[0];
    	 }
      // Is this the special "list the languages" command?
      elseif ($args[0] == 'langlist')
    	 {
      	 sort($cshLanguages); // sort so its easy to read
      	 return 'Current enabled languages: <code>'.implode(', ',$cshLanguages).'.</code>';
    	 }
      else
    	 {
    	 # $lang = '';
       	 $lang = 'csharp';
    	 }
 
   // Show line numbers/start line no?
   if (isset($args[1]))
      {
      if(is_numeric($args[1]))
      	 {
         $line_no = 'Y'; // implied by the presence of a start line number
         $line_start = intval($args[1]);
         if(isset($args[2]))
            {
            $line_range = getCSHMarkRange($line_start,trim($lines[0])); 
            // give the whole arg string
            }
         }
      }
 
   // Get rid of the now used first bit of [...] info, implode and trim
   $lines[0] = trim(substr($lines[0], $eofargspos + 2, strlen($lines[0])));
   }
 
   $source = trim(implode("\n",$lines));
 
   // Remap any languages?
   if ($lang == 'js') 		{ $lang = 'javascript'; } 	// shortcut
   if ($lang == 'html') 	{ $lang = 'html4strict'; } 	// shortcut
   if ($lang == 'pascal') 	{ $lang = 'delphi'; } 		// looks better
   if ($lang == 'xhtml') 	{ $lang = 'html4strict'; } 	// remap
 
   // Create the GeSHi parser object, tell it what it needs...
   $geshi = new GeSHi($source, $lang, CSH_GESHI_PATH);
 
   // turn line numbers on?
   if ($line_no == 'Y')
      {
      // add to remove the extra line height we didn't like to see in mediawiki
      $norm = 'border: 0px solid green; margin: -1px; padding: 0;';
      $geshi->set_line_style($norm);
      $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS,0);
      if(is_array($line_range))
         {
         $mark = 'background-color:#FFFF99; color: black; font-weight: bold;';
         $geshi->set_highlight_lines_extra_style($mark);
         $geshi->highlight_lines_extra($line_range);
      	 }
      }
   // start line numbering at ...
   $geshi->start_line_numbers_at($line_start);
 
   // parse the output, hand it back.
   $output = $geshi->parse_code();
   return $output;
} // end CSO_Source
 
 
function getCSHMarkRange ($line_start, $str)
   {
   // get the (...) substring as an array of int values
   $start = strpos($str,'(')+1;
   $end = strpos($str,')');
   $str = substr($str,$start,$end-$start);
   $tmp = explode(',',$str); // break into parts
   foreach($tmp as $key=>$val)
      {
     // expand the 4-7 type ranges and replace in $tmp
      if(strpos($val,'-'))
         {
      	 list($start,$end) = explode('-',$val);
      	 for($i=$start; $i<=$end; $i++) $tmp[] = $i - $line_start + 1;
         unset($tmp[$key]);
    	 }
      else
    	 {
    	 $tmp[] = $val - $line_start + 1;
	 }
      }
  return $tmp;
  }
?>

Personal tools