Edgewall Software

Changes between Version 35 and Version 36 of WikiMacros


Ignore:
Timestamp:
Apr 10, 2010, 7:34:04 PM (14 years ago)
Author:
Christian Boos
Comment:

copied from 0.12/WikiMacros@5

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v35 v36  
     1** Note: this page documents the 0.12 version of Trac, see [[0.11/WikiMacros]] if you need the previous version **
    12= Trac Macros =
    23
     
    89
    910== Using Macros ==
     11
    1012Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses.
    1113
    12 Trac macros can also be written as TracPlugins. This gives them some capabilities that macros do not have, such as being able to directly access the HTTP request.
     14=== Getting Detailed Help ===
     15The list of available macros and the full help can be obtained using the !MacroList macro, as seen [#AvailableMacros below].
     16
     17A brief list can be obtained via ![[MacroList(*)]] or ![[?]].
     18
     19Detailed 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?]].
     20
     21
    1322
    1423=== Example ===
     
    1625A list of 3 most recently changed wiki pages starting with 'Trac':
    1726
    18 {{{
    19  [[RecentChanges(Trac,3)]]
     27||= Wiki Markup =||= Display =||
     28{{{#!td
     29  {{{
     30  [[RecentChanges(Trac,3)]]
     31  }}}
    2032}}}
    21 
    22 Display:
    23  [[RecentChanges(Trac,3)]]
     33{{{#!td style="padding-left: 2em;"
     34[[RecentChanges(Trac,3)]]
     35}}}
     36|-----------------------------------
     37{{{#!td
     38  {{{
     39  [[RecentChanges?(Trac,3)]]
     40  }}}
     41}}}
     42{{{#!td style="padding-left: 2em;"
     43[[RecentChanges?(Trac,3)]]
     44}}}
     45|-----------------------------------
     46{{{#!td
     47  {{{
     48  [[?]]
     49  }}}
     50}}}
     51{{{#!td style="padding-left: 2em; font-size: 80%"
     52[[?]]
     53}}}
    2454
    2555== Available Macros ==
    2656
    2757''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].''
    28 
    29 {{{#!div class=important
    30 
    31 ''' Note: ''' The following auto-generated documentation corresponds to macros for Trac 0.12dev. There were no major changes however since the previous version.
    32 
    33 For the original 0.11 version, please have a look at [/demo-0.11/wiki/WikiMacros].
    34 }}}
    3558
    3659[[MacroList]]
     
    4164
    4265== Developing Custom Macros ==
    43 Macros, like Trac itself, are written in the [http://python.org/ Python programming language].
     66Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are developed as part of TracPlugins.
    4467
    4568For more information about developing macros, see the [trac:TracDev development resources] on the main project site.
    4669
    47 
    48 == Implementation ==
    4970
    5071Here are 2 simple examples showing how to create a Macro with Trac 0.11.
     
    5374
    5475=== Macro without arguments ===
    55 It should be saved as `TimeStamp.py` (in the TracEnvironment's `plugins/` directory) as Trac will use the module name as the Macro name.
     76To test the following code, you should saved it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory.
    5677{{{
    5778#!python
     
    7091    url = "$URL$"
    7192
    72     def expand_macro(self, formatter, name, args):
     93    def expand_macro(self, formatter, name, text):
    7394        t = datetime.now(utc)
    7495        return tag.b(format_datetime(t, '%c'))
     
    7697
    7798=== Macro with arguments ===
    78 It should be saved as `HelloWorld.py` (in the TracEnvironment's `plugins/` directory) as Trac will use the module name as the Macro name.
     99To test the following code, you should saved it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory.
    79100{{{
    80101#!python
     102from genshi.core import Markup
     103
    81104from trac.wiki.macros import WikiMacroBase
    82105
     
    96119    url = "$URL$"
    97120
    98     def expand_macro(self, formatter, name, args):
     121    def expand_macro(self, formatter, name, text, args):
    99122        """Return some output that will be displayed in the Wiki content.
    100123
    101124        `name` is the actual name of the macro (no surprise, here it'll be
    102125        `'HelloWorld'`),
    103         `args` is the text enclosed in parenthesis at the call of the macro.
     126        `text` is the text enclosed in parenthesis at the call of the macro.
    104127          Note that if there are ''no'' parenthesis (like in, e.g.
    105           [[HelloWorld]]), then `args` is `None`.
     128          [[HelloWorld]]), then `text` is `None`.
     129        `args` are the arguments passed when HelloWorld is called using a
     130        `#!HelloWorld` code block.
    106131        """
    107         return 'Hello World, args = ' + unicode(args)
    108    
    109     # Note that there's no need to HTML escape the returned data,
    110     # as the template engine (Genshi) will do it for us.
     132        return 'Hello World, text = %s, args = %s' % \
     133            (Markup.escape(text), Markup.escape(repr(args)))
     134
    111135}}}
    112136
     137Note 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'').
    113138
    114 === {{{expand_macro}}} details ===
    115 {{{expand_macro}}} should return either a simple Python string which will be interpreted as HTML, or preferably a Markup object (use {{{from trac.util.html import Markup}}}).  {{{Markup(string)}}} just annotates the string so the renderer will render the HTML string as-is with no escaping. You will also need to import Formatter using {{{from trac.wiki import Formatter}}}.
     139For example, when writing:
     140{{{
     141{{{#!HelloWorld style="polite"
     142<Hello World!>
     143}}}
    116144
    117 If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:
     145{{{#!HelloWorld
     146<Hello World!>
     147}}}
     148
     149[[HelloWorld(<Hello World!>)]]
     150}}}
     151One should get:
     152{{{
     153Hello World, text = <Hello World!> , args = {'style': u'polite'}
     154Hello World, text = <Hello World!> , args = {}
     155Hello World, text = <Hello World!> , args = None
     156}}}
     157
     158Note 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`). 
     159
     160You can also recursively use a wiki Formatter (`from trac.wiki import Formatter`) to process the `text` as wiki markup, for example by doing:
    118161
    119162{{{
    120163#!python
    121   text = "whatever wiki markup you want, even containing other macros"
    122   # Convert Wiki markup to HTML, new style
    123   out = StringIO()
    124   Formatter(self.env, formatter.context).format(text, out)
    125   return Markup(out.getvalue())
     164    text = "whatever wiki markup you want, even containing other macros"
     165    # Convert Wiki markup to HTML, new style
     166    out = StringIO()
     167    Formatter(self.env, formatter.context).format(text, out)
     168    return Markup(out.getvalue())
    126169}}}