<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://t2bwiki.iihe.ac.be/index.php?action=history&amp;feed=atom&amp;title=WikiMacros</id>
	<title>WikiMacros - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://t2bwiki.iihe.ac.be/index.php?action=history&amp;feed=atom&amp;title=WikiMacros"/>
	<link rel="alternate" type="text/html" href="https://t2bwiki.iihe.ac.be/index.php?title=WikiMacros&amp;action=history"/>
	<updated>2026-06-06T11:48:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://t2bwiki.iihe.ac.be/index.php?title=WikiMacros&amp;diff=358&amp;oldid=prev</id>
		<title>Maintenance script: Created page with &quot; == Trac Macros ==  PageOutline  Trac macros are plugins to extend the Trac engine with custom &#039;functions&#039; written in Python. A macro inserts dynamic HTML data in any cont...&quot;</title>
		<link rel="alternate" type="text/html" href="https://t2bwiki.iihe.ac.be/index.php?title=WikiMacros&amp;diff=358&amp;oldid=prev"/>
		<updated>2015-08-26T12:29:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; == Trac Macros ==  &lt;a href=&quot;/index.php?title=PageOutline&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;PageOutline (page does not exist)&quot;&gt;PageOutline&lt;/a&gt;  Trac macros are plugins to extend the Trac engine with custom &amp;#039;functions&amp;#039; written in Python. A macro inserts dynamic HTML data in any cont...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== Trac Macros ==&lt;br /&gt;
&lt;br /&gt;
[[PageOutline]]&lt;br /&gt;
&lt;br /&gt;
Trac macros are plugins to extend the Trac engine with custom &amp;#039;functions&amp;#039; written in Python. A macro inserts dynamic HTML data in any context supporting WikiFormatting.&lt;br /&gt;
&lt;br /&gt;
Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting).&lt;br /&gt;
&lt;br /&gt;
=== Using Macros ===&lt;br /&gt;
&lt;br /&gt;
Macro calls are enclosed in two &amp;#039;&amp;#039;square brackets&amp;#039;&amp;#039;. Like Python functions, macros can also have arguments, a comma separated list within parentheses.&lt;br /&gt;
&lt;br /&gt;
==== Getting Detailed Help ====&lt;br /&gt;
The list of available macros and the full help can be obtained using the MacroList macro, as seen [#AvailableMacros below].&lt;br /&gt;
&lt;br /&gt;
A brief list can be obtained via ![[MacroList(*)]] or ![[?]].&lt;br /&gt;
&lt;br /&gt;
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&amp;#039;s name, like in ![[MacroList?]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
A list of 3 most recently changed wiki pages starting with &amp;#039;Trac&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
{| border=1 class=&amp;quot;simple&amp;quot;&lt;br /&gt;
!= Wiki Markup =&lt;br /&gt;
!= Display =&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;#td&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
  [[RecentChanges(Trac,3)]]&lt;br /&gt;
  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;#td style=&amp;quot;padding-left: 2em;&amp;quot;&lt;br /&gt;
[[RecentChanges(Trac,3)]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-----------------------------------&lt;br /&gt;
&amp;lt;pre&amp;gt;#td&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
  [[RecentChanges?(Trac,3)]]&lt;br /&gt;
  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;#td style=&amp;quot;padding-left: 2em;&amp;quot;&lt;br /&gt;
[[RecentChanges?(Trac,3)]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-----------------------------------&lt;br /&gt;
&amp;lt;pre&amp;gt;#td&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
  [[?]]&lt;br /&gt;
  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;#td style=&amp;quot;padding-left: 2em; font-size: 80%&amp;quot;&lt;br /&gt;
[[?]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Available Macros ===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Note that the following list will only contain the macro documentation if you&amp;#039;ve not enabled &amp;lt;tt&amp;gt;-OO&amp;lt;/tt&amp;gt; optimizations, or not set the &amp;lt;tt&amp;gt;PythonOptimize&amp;lt;/tt&amp;gt; option for [[TracModPython| mod_python]].&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[MacroList]]&lt;br /&gt;
&lt;br /&gt;
=== Macros from around the world ===&lt;br /&gt;
&lt;br /&gt;
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&amp;#039;re looking for new macros, or have written one that you&amp;#039;d like to share with the world, please don&amp;#039;t hesitate to visit that site.&lt;br /&gt;
&lt;br /&gt;
=== Developing Custom Macros ===&lt;br /&gt;
Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are developed as part of TracPlugins.&lt;br /&gt;
&lt;br /&gt;
For more information about developing macros, see the [trac:TracDev development resources] on the main project site.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here are 2 simple examples showing how to create a Macro with Trac 0.11. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==== Macro without arguments ====&lt;br /&gt;
To test the following code, you should saved it in a &amp;lt;tt&amp;gt;timestamp_sample.py&amp;lt;/tt&amp;gt; file located in the TracEnvironment&amp;#039;s &amp;lt;tt&amp;gt;plugins/&amp;lt;/tt&amp;gt; directory.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#python&lt;br /&gt;
from datetime import datetime&lt;br /&gt;
# Note: since Trac 0.11, datetime objects are used internally&lt;br /&gt;
&lt;br /&gt;
from genshi.builder import tag&lt;br /&gt;
&lt;br /&gt;
from trac.util.datefmt import format_datetime, utc&lt;br /&gt;
from trac.wiki.macros import WikiMacroBase&lt;br /&gt;
&lt;br /&gt;
class TimeStampMacro(WikiMacroBase):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Inserts the current time (in seconds) into the wiki page.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    revision = &amp;quot;$Rev$&amp;quot;&lt;br /&gt;
    url = &amp;quot;$URL$&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def expand_macro(self, formatter, name, text):&lt;br /&gt;
        t = datetime.now(utc)&lt;br /&gt;
        return tag.b(format_datetime(t, &amp;#039;%c&amp;#039;))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Macro with arguments ====&lt;br /&gt;
To test the following code, you should saved it in a &amp;lt;tt&amp;gt;helloworld_sample.py&amp;lt;/tt&amp;gt; file located in the TracEnvironment&amp;#039;s &amp;lt;tt&amp;gt;plugins/&amp;lt;/tt&amp;gt; directory.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#python&lt;br /&gt;
from genshi.core import Markup&lt;br /&gt;
&lt;br /&gt;
from trac.wiki.macros import WikiMacroBase&lt;br /&gt;
&lt;br /&gt;
class HelloWorldMacro(WikiMacroBase):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Simple HelloWorld macro.&lt;br /&gt;
&lt;br /&gt;
    Note that the name of the class is meaningful:&lt;br /&gt;
     - it must end with &amp;quot;Macro&amp;quot;&lt;br /&gt;
     - what comes before &amp;quot;Macro&amp;quot; ends up being the macro name&lt;br /&gt;
&lt;br /&gt;
    The documentation of the class (i.e. what you&amp;#039;re reading)&lt;br /&gt;
    will become the documentation of the macro, as shown by&lt;br /&gt;
    the MacroList macro (usually used in the WikiMacros page).&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    revision = &amp;quot;$Rev$&amp;quot;&lt;br /&gt;
    url = &amp;quot;$URL$&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def expand_macro(self, formatter, name, text, args):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Return some output that will be displayed in the Wiki content.&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the actual name of the macro (no surprise, here it&amp;#039;ll be&lt;br /&gt;
        &amp;lt;tt&amp;gt;&amp;#039;HelloWorld&amp;#039;&amp;lt;/tt&amp;gt;),&lt;br /&gt;
        &amp;lt;tt&amp;gt;text&amp;lt;/tt&amp;gt; is the text enclosed in parenthesis at the call of the macro.&lt;br /&gt;
          Note that if there are &amp;#039;&amp;#039;no&amp;#039;&amp;#039; parenthesis (like in, e.g.&lt;br /&gt;
          [[HelloWorld]]), then &amp;lt;tt&amp;gt;text&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;None&amp;lt;/tt&amp;gt;.&lt;br /&gt;
        &amp;lt;tt&amp;gt;args&amp;lt;/tt&amp;gt; are the arguments passed when HelloWorld is called using a&lt;br /&gt;
        &amp;lt;tt&amp;gt;#HelloWorld&amp;lt;/tt&amp;gt; code block.&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        return &amp;#039;Hello World, text = %s, args = %s&amp;#039; % \&lt;br /&gt;
            (Markup.escape(text), Markup.escape(repr(args)))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that &amp;lt;tt&amp;gt;expand_macro&amp;lt;/tt&amp;gt; optionally takes a 4&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; parameter &amp;#039;&amp;#039;&amp;lt;tt&amp;gt;args&amp;lt;/tt&amp;gt;&amp;#039;&amp;#039;. When the macro is called as a [WikiProcessors WikiProcessor], it&amp;#039;s also possible to pass &amp;lt;tt&amp;gt;key=value&amp;lt;/tt&amp;gt; [WikiProcessors#UsingProcessors processor parameters]. If given, those are stored in a dictionary and passed in this extra &amp;lt;tt&amp;gt;args&amp;lt;/tt&amp;gt; parameter. On the contrary, when called as a macro, &amp;lt;tt&amp;gt;args&amp;lt;/tt&amp;gt; is  &amp;lt;tt&amp;gt;None&amp;lt;/tt&amp;gt;. (&amp;#039;&amp;#039;since 0.12&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
For example, when writing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;#HelloWorld style=&amp;quot;polite&amp;quot;&lt;br /&gt;
&amp;lt;Hello World!&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;#HelloWorld&lt;br /&gt;
&amp;lt;Hello World!&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[HelloWorld(&amp;lt;Hello World!&amp;gt;)]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
One should get:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello World, text = &amp;lt;Hello World!&amp;gt; , args = {&amp;#039;style&amp;#039;: u&amp;#039;polite&amp;#039;}&lt;br /&gt;
Hello World, text = &amp;lt;Hello World!&amp;gt; , args = {}&lt;br /&gt;
Hello World, text = &amp;lt;Hello World!&amp;gt; , args = None&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the return value of &amp;lt;tt&amp;gt;expand_macro&amp;lt;/tt&amp;gt; is &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; HTML escaped. Depending on the expected result, you should escape it by yourself (using &amp;lt;tt&amp;gt;return Markup.escape(result)&amp;lt;/tt&amp;gt;) or, if this is indeed HTML, wrap it in a Markup object (&amp;lt;tt&amp;gt;return Markup(result)&amp;lt;/tt&amp;gt;) with &amp;lt;tt&amp;gt;Markup&amp;lt;/tt&amp;gt; coming from Genshi, (&amp;lt;tt&amp;gt;from genshi.core import Markup&amp;lt;/tt&amp;gt;).  &lt;br /&gt;
&lt;br /&gt;
You can also recursively use a wiki Formatter (&amp;lt;tt&amp;gt;from trac.wiki import Formatter&amp;lt;/tt&amp;gt;) to process the &amp;lt;tt&amp;gt;text&amp;lt;/tt&amp;gt; as wiki markup, for example by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#python&lt;br /&gt;
    text = &amp;quot;whatever wiki markup you want, even containing other macros&amp;quot;&lt;br /&gt;
    # Convert Wiki markup to HTML, new style&lt;br /&gt;
    out = StringIO()&lt;br /&gt;
    Formatter(self.env, formatter.context).format(text, out)&lt;br /&gt;
    return Markup(out.getvalue())&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{TracNotice|{{PAGENAME}}}}&lt;/div&gt;</summary>
		<author><name>Maintenance script</name></author>
	</entry>
</feed>