MediaWiki/Syntax highlighting: Difference between revisions

From Freephile Wiki
mNo edit summary
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight"
 
(One intermediate revision by one other user not shown)
Line 11: Line 11:
== Usage ==
== Usage ==


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.
Just use the '''<nowiki><syntaxhighlight></nowiki>''' tag with the ''lang'' parameter to define the programming language.  Other optional parameters are avaliable too.  See the example below.


== Parameters ==
== Parameters ==
Line 30: Line 30:


== Example ==
== Example ==
<source lang="php" line>
<syntaxhighlight lang="php" line>
<?php
<?php
if( !defined( 'MEDIAWIKI' ) )
if( !defined( 'MEDIAWIKI' ) )
Line 50: Line 50:


?>
?>
</source>
</syntaxhighlight>





Latest revision as of 13:26, 24 February 2025

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

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

Usage

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

Parameters

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

<?php
if( !defined( 'MEDIAWIKI' ) )
        die();

$wgExtensionFunctions[] = 'syntaxHighlightSetup';
$wgExtensionCredits['parserhook']['SyntaxHighlight_GeSHi'] = array(
        'name'          => 'SyntaxHighlight',
        'author'        => 'Brion Vibber',
        'description'   => 'Provides syntax highlighting using [http://qbnz.com/highlighter/ GeSHi Higlighter]',
        'url'           => 'http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi',
);
$wgHooks['LoadAllMessages'][] = 'syntaxHighlightLoadMessages';

function syntaxHighlightSetup() {
        global $wgParser;
        $wgParser->setHook( 'source', 'syntaxHighlightHook' );
}

?>


Supported Languages

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

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