Code example box with syntax highlighting


This macro renders a code example box that supports syntax highlighting. It comes in the following forms: simple, correct, and incorrect. The examples can be used for code review and functional testing, see below for examples.

Key features:

  • The SELECT ALL link highlights all of the code in the box to simplify the copy and paste action.
  • A mouse click on the header (title) will show or hide the code block.
  • Code from the repository can be displayed using TracLinks syntax. The multiple repository configuration (Trac >= 0.12) is supported.

All rights reserved by Codedgers Inc.

Bugs/Feature Requests

Existing bugs and feature requests for CodeExampleMacro are here.

If you have any issues, create a new ticket.


12 / 14


8 / 11


1 / 1


Download the zipped source from here.


You can check out CodeExampleMacro from here using Subversion, or browse the source with Trac.


  1. Install plugin globally, for example with easy_install:
    [sudo] easy_install
    • [sudo] is optional in case you have authorisation to modify your server.
    • You might be required to select a specific tag, branch. Defaults to select from trunk.
  2. Enable this component by updating TracIni file as follows:
    codeexample.code_example_processor.* = enabled
  3. Restart web server on command line:
    [sudo] /etc/init.d/apache2 restart
    • [sudo] is optional in case you have authorisation to modify your server.
  4. Test if this component is working properly. If the plugin is installed correctly, the example shown in the #Usage section on this page should be displayed.


  1. Inside a wiki area you can invoke the #!CodeExample wiki processor to render a special formatted box containing specified source code, as shown and exemplified down:
    ## parameter = value
    ## parameter = value
    • Where ## parameter = value can be one or many from:
Required (R)
Optional (O)
## type O A type of box example: simple, good, bad
* Example: ## type = bad
## title O The title of the example.
* Example: ## title = Source from repository A
If ## type = simple then EXAMPLE:
If ## type = good then CORRECT EXAMPLE:
If ## type = bad then INCORRECT EXAMPLE:
## path O A file in the repository (using TracLinks format for source code)
* Example: ## path = GPGMail/Source/GPGMailPreferences.m
## repo O Repository to use (Trac 0.12 and upper only).
* Example: ## repo = T.B.D.
## regex O A regular expression indicates where to start an example.
* Example: ## regex = ".*updater\s*{"
## lines O Number of lines to show.
* Example: ## lines = 3
#!language O Syntax highlighter. language is any supported by Trac. See TracSyntaxColoring.
* Example: #!python
  1. The simple example:
    def get_templates_dirs():
        """ Notify Trac about templates dir. """
        from pkg_resources import resource_filename
        return [resource_filename(__name__, 'templates')]
    • will be rendered as:

  2. The incorrect example:
    ## type = bad
    fibs = 0 : 1 : [ a + b | a <- fibs | b <- tail fibs ]
    • will be rendered as:

  3. The correct example:
    ## type = good
    fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
    • will be rendered as:

  4. There is also support for getting sources from the repository:
    ## path=GPGMail/Source/GPGMailPreferences.m
    ## regex=".*updater\s*{"
    ## lines=3
    • will be rendered as:

Recent Changes

14903 by rjollos on 2015-09-21 17:43:45
Removes svn:mime-type text/plain.
14039 by rjollos on 2014-07-26 02:37:54
1.2: Include images in egg and fixed links to sample screenshots. Fixes #8331.

Thanks to AllenB and ilewismsl for the patches.

14038 by rjollos on 2014-07-26 02:37:35
Added URL to and bumped version to 1.1. Refs #9716.


Author: nuald
Maintainer: Alexander Slesarev, rjollos

Last modified 12 months ago Last modified on Nov 16, 2015, 1:34:36 PM

Attachments (7)

Download all attachments as: .zip