Skip to content

WikiMacros

Jonathan Guyer edited this page Sep 26, 2014 · 1 revision

Table of Contents

Trac Macros

PageOutline

Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. A macro inserts dynamic HTML data in any context supporting WikiFormatting.

Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting).

Using Macros

Macro calls are enclosed in two square brackets. Like Python functions, macros can also have arguments, a comma separated list within parentheses.

Getting Detailed Help

The list of available macros and the full help can be obtained using the !MacroList macro, as seen [#AvailableMacros].

A brief list can be obtained via `MacroList(*)` or `?`.

Detailed help on a specific macro can be obtained by passing it as an argument to !MacroList, e.g. `MacroList(MacroList)`, or, more conveniently, by appending a question mark (`?`) to the macro's name, like in `MacroList?`.

Example

A list of 3 most recently changed wiki pages starting with 'Trac':

||= Wiki Markup =||= Display =||

|-----------------------------------

|-----------------------------------

Available Macros

Note that the following list will only contain the macro documentation if you've not enabled `-OO` optimizations, or not set the `PythonOptimize` option for [wiki:TracModPython].

MacroList

Macros from around the world

The Trac Hacks site provides a wide collection of macros and other Trac [TracPlugins] contributed by the Trac community. If you're looking for new macros, or have written one that you'd like to share with the world, please don't hesitate to visit that site.

Developing Custom Macros

Macros, like Trac itself, are written in the Python programming language and are developed as part of TracPlugins.

For more information about developing macros, see the [trac:TracDev] on the main project site.

Here are 2 simple examples showing how to create a Macro with Trac 0.11.

Also, have a look at [trac:source:tags/trac-0.11/sample-plugins/Timestamp.py] for an example that shows the difference between old style and new style macros and at the [trac:source:tags/trac-0.11/wiki-macros/README] which provides a little more insight about the transition.

Macro without arguments

To test the following code, you should saved it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory.

Macro with arguments

To test the following code, you should saved it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory.

Note that `expand_macro` optionally takes a 4^th^ parameter `args`. When the macro is called as a [WikiProcessors], it's also possible to pass `key=value` [WikiProcessors#UsingProcessors]. If given, those are stored in a dictionary and passed in this extra `args` parameter. On the contrary, when called as a macro, `args` is `None`. (since 0.12).

For example, when writing:

One should get:

Note that the return value of `expand_macro` is not HTML escaped. Depending on the expected result, you should escape it by yourself (using `return Markup.escape(result)`) or, if this is indeed HTML, wrap it in a Markup object (`return Markup(result)`) with `Markup` coming from Genshi, (`from genshi.core import Markup`).

You can also recursively use a wiki Formatter (`from trac.wiki import Formatter`) to process the `text` as wiki markup, for example by doing:

Clone this wiki locally