Difference between revisions of "MediaWiki/Syntax highlighting"

From Freephile Wiki
Jump to navigation Jump to search
(added line numbers and supported language list)
Line 1: Line 1:
 
Writing good code follows good coding standards.  On top of coding standards, syntax highlighting reveals the code making it easier to read (and learn).
 
Writing good code follows good coding standards.  On top of coding standards, syntax highlighting reveals the code making it easier to read (and learn).
  
There is a [http://qbnz.com/highlighter/faq.php Generic Syntax Highlighter] or GeSHi for short which is available as a Debian package (sudo apt-get install php-geshi).  Adding [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi Brion Vibber's extension] (is better than this alternate [http://meta.wikimedia.org/wiki/GeSHiCodeTag_Extension GeSHi Code Tag] extension because Brion's just registers one new tag: 'source' and uses the extension parameter passing available since MW 1.5) extension to allows it to be used in this wiki.
+
There is a [http://qbnz.com/highlighter/faq.php Generic Syntax Highlighter] or GeSHi for short which is available as a Debian package (sudo apt-get install php-geshi).  However, if using [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi Brion Vibber's extension] (which is better than this alternate [http://meta.wikimedia.org/wiki/GeSHiCodeTag_Extension GeSHi Code Tag] extension because Brion's just registers one new tag: 'source' and uses the extension parameter passing available since MW 1.5) you will need to get the more recent source code download of GeSHi.
  
Incorporating it into Mediawiki is just a matter of creating/configuring the extension and including it in LocalSettings.
+
== Installation ==
 +
# Download GeSHi source
 +
# Download MW extension source
 +
# Make sure MW extension includes geshi.php
 +
# Add include to LocalSettings
  
 
== Usage ==
 
== Usage ==
  
There are two code tags that you can use:
+
Just use the '''<nowiki><source></nowiki>''' tag with the ''lang'' parameter to define the programming language.  Other optional parameters are avaliable too.  See the example below.
  
# '''Simple Tag Format:'''      <nowiki><'language'>source code here</'language'></nowiki>
+
== Parameters ==
# '''Advanced Tag Format:'''    <nowiki><code ['language'|list] [n] >source code here</code></nowiki>
 
  
 +
* '''lang:''' Defines the language
 +
* '''line:''' Corresponds to [http://qbnz.com/highlighter/geshi-doc.html#enabling-line-numbers enable_line_numbers] method on GeSHi
 +
* '''start:''' Corresponds to [http://qbnz.com/highlighter/geshi-doc.html#starting-line-numbers start_line_numbers_at] method on GeSHi
 +
<!--
 +
* '''case:''' Corresponds to [http://qbnz.com/highlighter/geshi-doc.html#auto-caps-nocaps set_case_keywords] method on GeSHi
 +
* '''tab:''' Corresponds to [http://qbnz.com/highlighter/geshi-doc.html#setting-tab-width set_tab_width] method on GeSHi
 +
-->
 +
* '''enclose:''' Corresponds to [http://qbnz.com/highlighter/geshi-doc.html#the-code-container set_header_type] method on GeSHi
 +
* '''strict:''' Corresponds to [http://qbnz.com/highlighter/geshi-doc.html#using-strict-mode enable_strict_mode] method on GeSHi
  
'''Tag Examples'''
+
The effect and usage of these parameters can be consulted in [http://qbnz.com/highlighter/geshi-doc.html GeSHi's documentation].
  
''- simple code tag''
+
Since r22246, you can override the colors using <nowiki>[[MediaWiki:GeSHi.css]]</nowiki>.
<br>
 
<nowiki>
 
<php> echo "Hello World"; </php>
 
</nowiki>
 
  
 +
== Example ==
 +
<source lang="php" line>
 +
<?php
 +
if( !defined( 'MEDIAWIKI' ) )
 +
        die();
  
''- advanced code tag''
+
$wgExtensionFunctions[] = 'syntaxHighlightSetup';
<br>
+
$wgExtensionCredits['parserhook']['SyntaxHighlight_GeSHi'] = array(
<nowiki>
+
        'name'          => 'SyntaxHighlight',
<code php>
+
        'author'        => 'Brion Vibber',
echo "Hello World";
+
        'description'  => 'Provides syntax highlighting using [http://qbnz.com/highlighter/ GeSHi Higlighter]',
</code>
+
        'url'          => 'http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi',
</nowiki>
+
);
 +
$wgHooks['LoadAllMessages'][] = 'syntaxHighlightLoadMessages';
  
+
function syntaxHighlightSetup() {
''- advanced code tag with line numbers''
+
        global $wgParser;
<br>
+
        $wgParser->setHook( 'source', 'syntaxHighlightHook' );
<nowiki>
+
}
<code php n>
 
echo "Hello World";
 
</code>
 
</nowiki>
 
  
 +
?>
 +
</source>
  
''- default code tag''
 
<br>
 
<nowiki>
 
<code>
 
echo "Hello World";
 
</code>
 
</nowiki>
 
 
 
''- display a list of supported languages''
 
<br>
 
<nowiki>
 
<code list></code>
 
</nowiki>
 
  
 
== Supported Languages ==
 
== Supported Languages ==
<code list></code>
+
These are the languages known by GeSHi that can be used in the '''lang''' parameter:
 +
{| border="0" cellpadding="0" cellspacing="20"
 +
|- valign="top"
 +
|
 +
* actionscript
 +
* ada
 +
* apache
 +
* applescript
 +
* asm
 +
* asp
 +
* autoit
 +
* bash
 +
* blitzbasic
 +
* bnf
 +
* c
 +
* caddcl
 +
* cadlisp
 +
* cfdg
 +
* cfm
 +
* cpp-qt
 +
* cpp
 +
* csharp
 +
* css-gen.cfg
 +
* css
 +
|
 +
* c_mac
 +
* d
 +
* delphi
 +
* diff
 +
* div
 +
* dos
 +
* eiffel
 +
* fortran
 +
* freebasic
 +
* gml
 +
* groovy
 +
* html4strict
 +
* idl
 +
* ini
 +
* inno
 +
* io
 +
* java
 +
* java5
 +
* javascript
 +
* latex
 +
|
 +
* lisp
 +
* lua
 +
* matlab
 +
* mirc
 +
* mpasm
 +
* mysql
 +
* nsis
 +
* objc
 +
* ocaml-brief
 +
* ocaml
 +
* oobas
 +
* oracle8
 +
* pascal
 +
* perl
 +
* php-brief
 +
* php
 +
* plsql
 +
* python
 +
* qbasic
 +
* reg
 +
|
 +
* robots
 +
* ruby
 +
* sas
 +
* scheme
 +
* sdlbasic
 +
* smalltalk
 +
* smarty
 +
* sql
 +
* tcl
 +
* text
 +
* thinbasic
 +
* tsql
 +
* vb
 +
* vbnet
 +
* vhdl
 +
* visualfoxpro
 +
* winbatch
 +
* xml
 +
* z80
 +
|}
 +
See '''documentation''' for the latest list of additions
  
 
== Documentation ==
 
== Documentation ==

Revision as of 22:27, 3 June 2007

Writing good code follows good coding standards. On top of coding standards, syntax highlighting reveals the code making it easier to read (and learn).

There is a Generic Syntax Highlighter or GeSHi for short which is available as a Debian package (sudo apt-get install php-geshi). However, if using Brion Vibber's extension (which is better than this alternate GeSHi Code Tag extension because Brion's just registers one new tag: 'source' and uses the extension parameter passing available since MW 1.5) you will need to get the more recent source code download of GeSHi.

Installation[edit | edit source]

  1. Download GeSHi source
  2. Download MW extension source
  3. Make sure MW extension includes geshi.php
  4. Add include to LocalSettings

Usage[edit | edit source]

Just use the <source> tag with the lang parameter to define the programming language. Other optional parameters are avaliable too. See the example below.

Parameters[edit | edit source]

The effect and usage of these parameters can be consulted in GeSHi's documentation.

Since r22246, you can override the colors using [[MediaWiki:GeSHi.css]].

Example[edit | edit source]

 1 <?php
 2 if( !defined( 'MEDIAWIKI' ) )
 3         die();
 4 
 5 $wgExtensionFunctions[] = 'syntaxHighlightSetup';
 6 $wgExtensionCredits['parserhook']['SyntaxHighlight_GeSHi'] = array(
 7         'name'          => 'SyntaxHighlight',
 8         'author'        => 'Brion Vibber',
 9         'description'   => 'Provides syntax highlighting using [http://qbnz.com/highlighter/ GeSHi Higlighter]',
10         'url'           => 'http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi',
11 );
12 $wgHooks['LoadAllMessages'][] = 'syntaxHighlightLoadMessages';
13 
14 function syntaxHighlightSetup() {
15         global $wgParser;
16         $wgParser->setHook( 'source', 'syntaxHighlightHook' );
17 }
18 
19 ?>


Supported Languages[edit | edit source]

These are the languages known by GeSHi that can be used in the lang parameter:

  • actionscript
  • ada
  • apache
  • applescript
  • asm
  • asp
  • autoit
  • bash
  • blitzbasic
  • bnf
  • c
  • caddcl
  • cadlisp
  • cfdg
  • cfm
  • cpp-qt
  • cpp
  • csharp
  • css-gen.cfg
  • css
  • c_mac
  • d
  • delphi
  • diff
  • div
  • dos
  • eiffel
  • fortran
  • freebasic
  • gml
  • groovy
  • html4strict
  • idl
  • ini
  • inno
  • io
  • java
  • java5
  • javascript
  • latex
  • lisp
  • lua
  • matlab
  • mirc
  • mpasm
  • mysql
  • nsis
  • objc
  • ocaml-brief
  • ocaml
  • oobas
  • oracle8
  • pascal
  • perl
  • php-brief
  • php
  • plsql
  • python
  • qbasic
  • reg
  • robots
  • ruby
  • sas
  • scheme
  • sdlbasic
  • smalltalk
  • smarty
  • sql
  • tcl
  • text
  • thinbasic
  • tsql
  • vb
  • vbnet
  • vhdl
  • visualfoxpro
  • winbatch
  • xml
  • z80

See documentation for the latest list of additions

Documentation[edit | edit source]

See http://qbnz.com/highlighter/geshi-doc.html for the extensive documentation. GeSHi is used in a number of other software projects like

  • Dokuwiki - An advanced wiki engine
  • gtk.php.net - Their manual uses GeSHi for syntax highlighting
  • WordPress - A powerful blogging system*
  • PHP-Fusion - A constantly evovling CMS
  • SQL Manager - A Postgres DBAL
  • Mambo - A popular open source CMS
  • MediaWiki - A leader in Wikis*
  • TikiWiki - A megapowerful Wiki/CMS
  • TikiPro - Another powerful Wiki based on Tikiwiki
  • RWeb - A site-building tool

If you want to use it in OASIS projects besides this wiki, then start with the documentation.