Edgewall Software
Home
Trac
Trac Hacks
Genshi
Babel
Bitten
Home
Download
Documentation
Mailing Lists
License
FAQ
Search:
Login
Preferences
Help/Guide
About Trac
Wiki
Timeline
Roadmap
Browse Source
View Tickets
New Ticket
Search
Context Navigation
-1
Start Page
Index
History
Editing WikiMacros
Adjust edit area height:
8
12
16
20
24
28
32
36
40
Edit side-by-side
** Note: this page documents the 0.12 version of Trac, see [[0.11/WikiMacros]] if you need the previous version and [[0.13/WikiMacros]] if you need the next version** = Trac Macros = [[PageOutline]] [[TranslatedPages]] 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 below]. 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 =|| {{{#!td {{{ [[RecentChanges(Trac,3)]] }}} }}} {{{#!td style="padding-left: 2em;" [[RecentChanges(Trac,3)]] }}} |----------------------------------- {{{#!td {{{ [[RecentChanges?(Trac,3)]] }}} }}} {{{#!td style="padding-left: 2em;" [[RecentChanges?(Trac,3)]] }}} |----------------------------------- {{{#!td {{{ [[?]] }}} }}} {{{#!td style="padding-left: 2em" {{{#!html <div style="font-size: 80%" class="trac-macrolist"> <h3><code>[[Image]]</code></h3>Embed an image in wiki-formatted text. The first argument is the file … <h3><code>[[InterTrac]]</code></h3>Provide a list of known <a class="wiki" href="/wiki/InterTrac">InterTrac</a> prefixes. <h3><code>[[InterWiki]]</code></h3>Provide a description list for the known <a class="wiki" href="/wiki/InterWiki">InterWiki</a> prefixes. <h3><code>[[KnownMimeTypes]]</code></h3>List all known mime-types which can be used as <a class="wiki" href="/wiki/WikiProcessors">WikiProcessors</a>. Can be …</div> }}} etc. }}} == 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 mod_python].'' (the following is snapshot for Trac 0.12.2 - see [[0.13/WikiMacros]] for the documentation of macros in trunk / 0.13dev) {{{#!html <div class="trac-macrolist"><h3 id="Image-macro"><code>[[Image]]</code></h3><p> Embed an image in wiki-formatted text. </p> <p> The first argument is the file specification. The file specification may reference attachments in three ways: </p> <ul><li><tt>module:id:file</tt>, where module can be either <strong>wiki</strong> or <strong>ticket</strong>, to refer to the attachment named <em>file</em> of the specified wiki page or ticket. </li><li><tt>id:file</tt>: same as above, but id is either a ticket shorthand or a Wiki page name. </li><li><tt>file</tt> to refer to a local attachment named 'file'. This only works from within that wiki page or a ticket. </li></ul><p> Also, the file specification may refer to repository files, using the <tt>source:file</tt> syntax (<tt>source:file@rev</tt> works also). </p> <p> Files can also be accessed with a direct URLs; <tt>/file</tt> for a project-relative, <tt>//file</tt> for a server-relative, or <tt>http://server/file</tt> for absolute location of the file. </p> <p> The remaining arguments are optional and allow configuring the attributes and style of the rendered <tt><img></tt> element: </p> <ul><li>digits and unit are interpreted as the size (ex. 120, 25%) for the image </li><li><tt>right</tt>, <tt>left</tt>, <tt>center</tt>, <tt>top</tt>, <tt>bottom</tt> and <tt>middle</tt> are interpreted as the alignment for the image (alternatively, the first three can be specified using <tt>align=...</tt> and the last three using <tt>valign=...</tt>) </li><li><tt>link=some TracLinks...</tt> replaces the link to the image source by the one specified using a <a class="wiki" href="/wiki/TracLinks">TracLinks</a>. If no value is specified, the link is simply removed. </li><li><tt>nolink</tt> means without link to image source (deprecated, use <tt>link=</tt>) </li><li><tt>key=value</tt> style are interpreted as HTML attributes or CSS style indications for the image. Valid keys are: <ul><li>align, valign, border, width, height, alt, title, longdesc, class, margin, margin-(left,right,top,bottom), id and usemap </li><li><tt>border</tt>, <tt>margin</tt>, and <tt>margin-</tt>* can only be a single number </li><li><tt>margin</tt> is superseded by <tt>center</tt> which uses auto margins </li></ul></li></ul><p> Examples: </p> <pre class="wiki"> [[Image(photo.jpg)]] # simplest [[Image(photo.jpg, 120px)]] # with image width size [[Image(photo.jpg, right)]] # aligned by keyword [[Image(photo.jpg, nolink)]] # without link to source [[Image(photo.jpg, align=right)]] # aligned by attribute </pre><p> You can use image from other page, other ticket or other module. </p> <pre class="wiki"> [[Image(OtherPage:foo.bmp)]] # if current module is wiki [[Image(base/sub:bar.bmp)]] # from hierarchical wiki page [[Image(#3:baz.bmp)]] # if in a ticket, point to #3 [[Image(ticket:36:boo.jpg)]] [[Image(source:/images/bee.jpg)]] # straight from the repository! [[Image(htdocs:foo/bar.png)]] # image file in project htdocs dir. </pre><p> <em>Adapted from the Image.py macro created by Shun-ichi Goto <gotoh@…></em> </p> <h3 id="InterTrac-macro"><code>[[InterTrac]]</code></h3><p> Provide a list of known <a class="wiki" href="/wiki/InterTrac">InterTrac</a> prefixes. </p> <h3 id="InterWiki-macro"><code>[[InterWiki]]</code></h3><p> Provide a description list for the known <a class="wiki" href="/wiki/InterWiki">InterWiki</a> prefixes. </p> <h3 id="KnownMimeTypes-macro"><code>[[KnownMimeTypes]]</code></h3><p> List all known mime-types which can be used as <a class="wiki" href="/wiki/WikiProcessors">WikiProcessors</a>. </p> <p> Can be given an optional argument which is interpreted as mime-type filter. </p> <h3 id="MacroList-macro"><code>[[MacroList]]</code></h3><p> Display a list of all installed Wiki macros, including documentation if available. </p> <p> Optionally, the name of a specific macro can be provided as an argument. In that case, only the documentation for that macro will be rendered. </p> <p> Note that this macro will not be able to display the documentation of macros if the <tt>PythonOptimize</tt> option is enabled for mod_python! </p> <h3 id="PageOutline-macro"><code>[[PageOutline]]</code></h3><p> Display a structural outline of the current wiki page, each item in the outline being a link to the corresponding heading. </p> <p> This macro accepts three optional parameters: </p> <ul><li>The first is a number or range that allows configuring the minimum and maximum level of headings that should be included in the outline. For example, specifying "1" here will result in only the top-level headings being included in the outline. Specifying "2-3" will make the outline include all headings of level 2 and 3, as a nested list. The default is to include all heading levels. </li><li>The second parameter can be used to specify a custom title (the default is no title). </li><li>The third parameter selects the style of the outline. This can be either <tt>inline</tt> or <tt>pullout</tt> (the latter being the default). The <tt>inline</tt> style renders the outline as normal part of the content, while <tt>pullout</tt> causes the outline to be rendered in a box that is by default floated to the right side of the other content. </li></ul><h3 id="RecentChanges-macro"><code>[[RecentChanges]]</code></h3><p> List all pages that have recently been modified, grouping them by the day they were last modified. </p> <p> This macro accepts two parameters. The first is a prefix string: if provided, only pages with names that start with the prefix are included in the resulting list. If this parameter is omitted, all pages are listed. </p> <p> The second parameter is a number for limiting the number of pages returned. For example, specifying a limit of 5 will result in only the five most recently changed pages to be included in the list. </p> <h3 id="RepositoryIndex-macro"><code>[[RepositoryIndex]]</code></h3><p> Display the list of available repositories. </p> <p> Can be given the following named arguments: </p> <dl class="wiki"><dt><em>format</em></dt><dd> Select the rendering format: <ul><li><em>compact</em> produces a comma-separated list of repository prefix names (default) </li><li><em>list</em> produces a description list of repository prefix names </li><li><em>table</em> produces a table view, similar to the one visible in the <em>Browse View</em> page </li></ul></dd><dt><em>glob</em></dt><dd> Do a glob-style filtering on the repository names (defaults to '*') </dd><dt><em>order</em></dt><dd> Order repositories by the given column (one of "name", "date" or "author") </dd><dt><em>desc</em></dt><dd> When set to 1, order by descending order </dd></dl> <p> (<em>since 0.12</em>) </p> <h3 id="TicketQuery-macro"><code>[[TicketQuery]]</code></h3><p> Wiki macro listing tickets that match certain criteria. </p> <p> This macro accepts a comma-separated list of keyed parameters, in the form "key=value". </p> <p> If the key is the name of a field, the value must use the syntax of a filter specifier as defined in <a class="wiki" href="/wiki/TracQuery#QueryLanguage">TracQuery#QueryLanguage</a>. Note that this is <em>not</em> the same as the simplified URL syntax used for <tt>query:</tt> links starting with a <tt>?</tt> character. Commas (<tt>,</tt>) can be included in field values by escaping them with a backslash (<tt>\</tt>). </p> <p> Groups of field constraints to be OR-ed together can be separated by a litteral <tt>or</tt> argument. </p> <p> In addition to filters, several other named parameters can be used to control how the results are presented. All of them are optional. </p> <p> The <tt>format</tt> parameter determines how the list of tickets is presented: </p> <ul><li><strong>list</strong> -- the default presentation is to list the ticket ID next to the summary, with each ticket on a separate line. </li><li><strong>compact</strong> -- the tickets are presented as a comma-separated list of ticket IDs. </li><li><strong>count</strong> -- only the count of matching tickets is displayed </li><li><strong>table</strong> -- a view similar to the custom query view (but without the controls) </li></ul><p> The <tt>max</tt> parameter can be used to limit the number of tickets shown (defaults to <strong>0</strong>, i.e. no maximum). </p> <p> The <tt>order</tt> parameter sets the field used for ordering tickets (defaults to <strong>id</strong>). </p> <p> The <tt>desc</tt> parameter indicates whether the order of the tickets should be reversed (defaults to <strong>false</strong>). </p> <p> The <tt>group</tt> parameter sets the field used for grouping tickets (defaults to not being set). </p> <p> The <tt>groupdesc</tt> parameter indicates whether the natural display order of the groups should be reversed (defaults to <strong>false</strong>). </p> <p> The <tt>verbose</tt> parameter can be set to a true value in order to get the description for the listed tickets. For <strong>table</strong> format only. <em>deprecated in favor of the <tt>rows</tt> parameter</em> </p> <p> The <tt>rows</tt> parameter can be used to specify which field(s) should be viewed as a row, e.g. <tt>rows=description|summary</tt> </p> <p> For compatibility with Trac 0.10, if there's a last positional parameter given to the macro, it will be used to specify the <tt>format</tt>. Also, using "&" as a field separator still works (except for <tt>order</tt>) but is deprecated. </p> <h3 id="TitleIndex-macro"><code>[[TitleIndex]]</code></h3><p> Insert an alphabetic list of all wiki pages into the output. </p> <p> Accepts a prefix string as parameter: if provided, only pages with names that start with the prefix are included in the resulting list. If this parameter is omitted, all pages are listed. If the prefix is specified, a second argument of value 'hideprefix' can be given as well, in order to remove that prefix from the output. </p> <p> Alternate <tt>format</tt> and <tt>depth</tt> named parameters can be specified: </p> <ul><li><tt>format=compact</tt>: The pages are displayed as comma-separated links. </li><li><tt>format=group</tt>: The list of pages will be structured in groups according to common prefix. This format also supports a <tt>min=n</tt> argument, where <tt>n</tt> is the minimal number of pages for a group. </li><li><tt>format=hierarchy</tt>: The list of pages will be structured according to the page name path hierarchy. This format also supports a <tt>min=n</tt> argument, where higher <tt>n</tt> flatten the display hierarchy </li><li><tt>depth=n</tt>: limit the depth of the pages to list. If set to 0, only toplevel pages will be shown, if set to 1, only immediate children pages will be shown, etc. If not set, or set to -1, all pages in the hierarchy will be shown. </li></ul><h3 id="TracAdminHelp-macro"><code>[[TracAdminHelp]]</code></h3><p> Display help for trac-admin commands. </p> <p> Examples: </p> <pre class="wiki">[[TracAdminHelp]] # all commands [[TracAdminHelp(wiki)]] # all wiki commands [[TracAdminHelp(wiki export)]] # the "wiki export" command [[TracAdminHelp(upgrade)]] # the upgrade command </pre><h3 id="TracGuideToc-macro"><code>[[TracGuideToc]]</code></h3><p> Display a table of content for the Trac guide. </p> <p> This macro shows a quick and dirty way to make a table-of-contents for the <a class="missing wiki">Help/Guide?</a>. The table of contents will contain the Trac* and <a class="wiki" href="/wiki/WikiFormatting">WikiFormatting</a> pages, and can't be customized. Search for <a class="missing wiki">TocMacro?</a> for a a more customizable table of contents. </p> <h3 id="TracIni-macro"><code>[[TracIni]]</code></h3><p> Produce documentation for the Trac configuration file. </p> <p> Typically, this will be used in the <a class="wiki" href="/wiki/TracIni">TracIni</a> page. Optional arguments are a configuration section filter, and a configuration option name filter: only the configuration options whose section and name start with the filters are output. </p> </div> }}} == Macros from around the world == The [http://trac-hacks.org/ Trac Hacks] site provides a wide collection of macros and other Trac [TracPlugins plugins] 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 [http://python.org/ Python programming language] and are developed as part of TracPlugins. For more information about developing macros, see the [trac:TracDev development resources] 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 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 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. {{{ #!python from datetime import datetime # Note: since Trac 0.11, datetime objects are used internally from genshi.builder import tag from trac.util.datefmt import format_datetime, utc from trac.wiki.macros import WikiMacroBase class TimeStampMacro(WikiMacroBase): """Inserts the current time (in seconds) into the wiki page.""" revision = "$Rev$" url = "$URL$" def expand_macro(self, formatter, name, text): t = datetime.now(utc) return tag.b(format_datetime(t, '%c')) }}} === 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. {{{ #!python from genshi.core import Markup from trac.wiki.macros import WikiMacroBase class HelloWorldMacro(WikiMacroBase): """Simple HelloWorld macro. Note that the name of the class is meaningful: - it must end with "Macro" - what comes before "Macro" ends up being the macro name The documentation of the class (i.e. what you're reading) will become the documentation of the macro, as shown by the !MacroList macro (usually used in the WikiMacros page). """ revision = "$Rev$" url = "$URL$" def expand_macro(self, formatter, name, text, args): """Return some output that will be displayed in the Wiki content. `name` is the actual name of the macro (no surprise, here it'll be `'HelloWorld'`), `text` is the text enclosed in parenthesis at the call of the macro. Note that if there are ''no'' parenthesis (like in, e.g. [[HelloWorld]]), then `text` is `None`. `args` are the arguments passed when HelloWorld is called using a `#!HelloWorld` code block. """ return 'Hello World, text = %s, args = %s' % \ (Markup.escape(text), Markup.escape(repr(args))) }}} Note that `expand_macro` optionally takes a 4^th^ parameter ''`args`''. When the macro is called as a [WikiProcessors WikiProcessor], it's also possible to pass `key=value` [WikiProcessors#UsingProcessors processor parameters]. 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: {{{ {{{#!HelloWorld style="polite" <Hello World!> }}} {{{#!HelloWorld <Hello World!> }}} [[HelloWorld(<Hello World!>)]] }}} One should get: {{{ Hello World, text = <Hello World!> , args = {'style': u'polite'} Hello World, text = <Hello World!> , args = {} Hello World, text = <Hello World!> , args = None }}} 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: {{{ #!python from genshi.core import Markup from trac.wiki.macros import WikiMacroBase from trac.wiki import Formatter import StringIO class HelloWorldMacro(WikiMacroBase): def expand_macro(self, formatter, name, text, args): text = "whatever '''wiki''' markup you want, even containing other macros" # Convert Wiki markup to HTML, new style out = StringIO.StringIO() Formatter(self.env, formatter.context).format(text, out) return Markup(out.getvalue()) }}}
Note:
See
WikiFormatting
and
TracWiki
for help on editing wiki content.
Change information
Your email or username:
E-mail address and name can be saved in the
Preferences
Comment about this change (optional):
Note:
See
TracWiki
for help on using the wiki.