development1Add my vote for this tag rendering1Add my vote for this tag syntax_highlighting2Add my vote for this tag create new tag
, view all tags


Highlight and format code fragments using GNU source-highlight


This plugin allows you to have code fragments automatically formatted and highlighted using the GNU source-highlight package.

Syntax Rules

  • Just paste the code between %CODE{"syntax"}% and %ENDCODE%. The following indicates the syntax used for the different languages:

Language Key Syntax
C++ cpp %CODE{"cpp"}% ... %ENDCODE%
C cpp %CODE{"cpp"}% ... %ENDCODE%
Flex flex %CODE{"flex"}% ... %ENDCODE%
Java java %CODE{"java"}% ... %ENDCODE%
Perl perl %CODE{"perl"}% ... %ENDCODE%
PHP3 php3 %CODE{"php3"}% ... %ENDCODE%
Prolog prolog %CODE{"prolog"}% ... %ENDCODE%
Python python %CODE{"python"}% ... %ENDCODE%

You can also define a default language and use the shortcut:



   void foo() {
     print("Do stuff.\n");

SourceHighlightPlugin Global Settings

Plugin settings are stored as preferences variables. To reference a plugin setting write %<plugin>_<setting>%, i.e. %SOURCEHIGHLIGHTPLUGIN_DEFAULTLANGUAGE%

  • One line description, is shown in the TextFormattingRules topic:
    • Set SHORTDESCRIPTION = Highlight and format code fragments using GNU source-highlight
    • Set DEBUG = 0

  • Full path to GNU source-highlight binary (REQUIRED)
    • Set BINARY = /usr/local/bin/source-highlight

  • Language to use when none specified (optional); choose from one of the Key values in the table above

  • CSS class to use for the div tag surrounding the HTML output; if undefined we use codefragment

Plugin Installation Instructions

Note: You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the server where TWiki is running.

  • Install the GNU source-highlight package and note its location
  • Open /usr/local/shard/source-highlight/html.outlang. Eliminate all <tt> </tt>s. This avoids a blank line at the beginning of the code.
  • Download the ZIP file from the Plugin web (see below)
  • Unzip SourceHighlightPlugin.zip in your twiki installation directory. Content:
    File: Description:
    data/TWiki/SourceHighlightPlugin.txt Plugin topic
    lib/TWiki/Plugins/SourceHighlightPlugin.pm Plugin Perl module
  • Visit configure in your TWiki installation, and enable the plugin in the {Plugins} section.
  • To get a smart-looking box around the code fragment, something like the following is needed in the skin's stylesheet. Note that you may need to change the class name if you modify the FORMATCLASS configuration variable above.

.codefragment {
        background : #FFFFCC;
        border : 1px solid #CCCCCC;
        margin-left : 2em;
        margin-right : 2em;
        padding : 4px;
  • Test if the installation was successful:

%CODE% package com.foo.stuff;

import java.util.ArrayList; import java.util.List;

public class Thingy { private List myList;

public Thingy() { myList = new ArrayList(); }

/** * @param count This is the thingy you want. */ public String getThingy( int count ) { return (String)myList.get( count ); } } %ENDCODE%

Plugin Info

Plugin Author: TWiki:Main.ChrisWinters
Copyright: © 2003 TWiki:Main.ChrisWinters,
© 2003-2010 TWiki:TWiki.TWikiContributor
License: GPL (GNU General Public License)
Plugin Version: 19332 (2010-08-06)
Change History:  
2010-08-06: TWikibug:Item6530 - doc enhancements; changing TWIKIWEB to SYSTEMWEB -- TWiki:Main.PeterThoeny
2009-02-10: Fix for last bracket missing; fixed default language, changing from DEFAULTLANG to DEFAULTLANGUAGE -- TWiki:Main.FengZhaolin
2003-01-23: Adapted from TWiki:Plugins.BeautifierPlugin -- TWiki:Main.ChrisWinters
CPAN Dependencies: none
Other Dependencies: GNU source-highlight
Perl Version: 5.005
Plugin Home: http://TWiki.org/cgi-bin/view/Plugins/SourceHighlightPlugin
Feedback: http://TWiki.org/cgi-bin/view/Plugins/SourceHighlightPluginDev

Related Topics: TWikiPreferences, TWikiPlugins

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatmd5 SourceHighlightPlugin.md5 r1 manage 0.2 K 2010-08-07 - 06:57 PeterThoeny  
Compressed Zip archivetgz SourceHighlightPlugin.tgz r1 manage 5.9 K 2010-08-07 - 06:57 PeterThoeny  
Compressed Zip archivezip SourceHighlightPlugin.zip r2 r1 manage 7.5 K 2010-08-07 - 06:57 PeterThoeny  
Unknown file formatEXT SourceHighlightPlugin_installer r2 r1 manage 3.4 K 2010-08-07 - 06:57 PeterThoeny  
Edit | Attach | Watch | Print version | History: r16 < r15 < r14 < r13 < r12 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r16 - 2013-10-16 - PeterThoeny
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.