CodeSyntaxHighlight Usage

From SwinBrain

Contents

Code Syntax Highlighting

We had to have code syntax highlighting support! This has been possible thanks to the fantastic GeSHi project by Nigel McNie, released under the GNU GPL.

Clinton wrote an extension named simply CodeSyntaxHighlight for use here with SwinBrain (MediaWiki engine), and the extension code and installation instructions are available for anyone interested.

Basically, if the code is in a <code> element with the language named inside square braces [] - and the language is supported - it will be highlighted (XHTML + inline CSS) by the GeSHi engine and then shown by the MediaWiki engine.

Quick Example

Below is an example of using the CodeSyntaxHighlight extension in SwinBrain:

Example Result
<code>[pascal,N]
for i := 0 to high(myArray) do begin
  writeln('Hi '+myArray[i]+'!');
end;
</code>
for i := 0 to high(myArray) do begin
  writeln('Hi '+myArray[i]+'!');
end;

Where [language, line_no_on (Y/N), line_no_start].

Argument Details

Here's some more examples of how the arguments in the square [ ] brackets are used taken from the comments in the extension php file.

# Usage in mediawiki
#  <code>[lang,line_no,start_at,range(1-4,6-7)]...</ code>
# Examples
#  <code>[lang]...</ code> // line_no default=Y, start_at default=1
#  <code>[lang,N]...</ code> // no line numbers
#  <code>[lang,5]...</ code> // start at line 5 (with line numbers
#  <code>[lang,Y,5]...</ code> // deprecated - same as above
#                             // ('Y' is implied by start_at)
#  <code>[lang,1,(3-5,7)]...</ code> // mark lines 3,4,5 and 7
#
#  - lang is required, others are optional
#  - line_no default is Y, start_at is default to 1 

The default for line_no_on is Y, and line_no_start is 1, so we can just write a simple

<code>[pascal]
...
</code>

and that works fine!

Example - Set Start Line Number

Here is the same example as above with line numbers shown. Here you can see the effect of the line number option, in this case the line numbers start at 5. (Note, you no longer have to specify [lang,Y,5] - just a simple [lang,5] will do).

Example Result
<code>[pascal,5]
for i := 0 to high(myArray) do begin
  writeln('Hi '+myArray[i]+'!');
end;
</code>
  1. for i := 0 to high(myArray) do begin
  2. writeln('Hi '+myArray[i]+'!');
  3. end;

Example - Mark Special Lines

Here is the similar example but with a set of lines numbers marked in a different style (which you could then talk about). Note that the argument supports both single numbers separated by commas, and ranges of numbers like 3-5 (which equals 3,4,5).

Example Result
<code>[pascal,1,(2,4-6)]
// here is some code...
for i := 0 to high(myArray) do 
begin
  writeln('Hi '+myArray[i]+'!');
end;

for i := high(myArray) downto 0 do 
begin
  writeln('Hi '+myArray[i]+'!');
end;
</code>
  1. // here is some code...
  2. for i := 0 to high(myArray) do
  3. begin
  4. writeln('Hi '+myArray[i]+'!');
  5. end;
  6.  
  7. for i := high(myArray) downto 0 do
  8. begin
  9. writeln('Hi '+myArray[i]+'!');
  10. end;


Example - Attribute Parameters

Now that mediawiki supports the passing of attibutes to extensions, the extension has been updated to support there is an alternative way of setting parameters with attribute values instead of the [...] syntax.

  • lang="..." Required. Value is any of the supported languages or the special "langlist" value to list all languages.
  • numbers="Y|N". Default = N. Not required. If startat or range are set, numbers is overridden to "Y"
  • startat="...". Default is 1. Not required.
  • range="...". Default is nothing. Not required. list of comma separated lines, or hyphen separated ranges eg range="2,4-6" would highlight lines 2, and 4,5,6.
Example Result
<code lang="pascal" startat="1" range="2,4-6">
// here is some code...
for i := 0 to high(myArray) do 
begin
  writeln('Hi '+myArray[i]+'!');
end;

for i := high(myArray) downto 0 do 
begin
  writeln('Hi '+myArray[i]+'!');
end;
</code>
  1. // here is some code...
  2. for i := 0 to high(myArray) do
  3. begin
  4. writeln('Hi '+myArray[i]+'!');
  5. end;
  6.  
  7. for i := high(myArray) downto 0 do
  8. begin
  9. writeln('Hi '+myArray[i]+'!');
  10. end;

Powered by GeSHi

What languages are supported?

The list shown below is dynamically generated by the extension, so it is "up-do-date".

  • Current enabled languages: actionscript, ada, apache, asm, bash, c, cpp, csharp, css, delphi, dos, eiffel, html, ini, java, java5, javascript, js, lisp, matlab, mpasm, objc, oracle8, pascal, perl, php, pseudocode, python, qbasic, ruby, scheme, smarty, sql, text, vb, vbnet, vhdl, xhtml, xml.