Edgewall Software

Changes between Version 2 and Version 3 of 0.12/WikiMacros


Ignore:
Timestamp:
Feb 2, 2010, 5:19:22 PM (14 years ago)
Author:
Christian Boos
Comment:

document #8204

Legend:

Unmodified
Added
Removed
Modified
  • 0.12/WikiMacros

    v2 v3  
    6868
    6969
    70 == Implementation ==
    71 
    7270Here are 2 simple examples showing how to create a Macro with Trac 0.11.
    7371
     
    7573
    7674=== Macro without arguments ===
    77 It should be saved as `TimeStamp.py` (in the TracEnvironment's `plugins/` directory) as Trac will use the module name as the Macro name.
     75To test the following code, you should saved it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory.
    7876{{{
    7977#!python
     
    9290    url = "$URL$"
    9391
    94     def expand_macro(self, formatter, name, args):
     92    def expand_macro(self, formatter, name, text):
    9593        t = datetime.now(utc)
    9694        return tag.b(format_datetime(t, '%c'))
     
    9896
    9997=== Macro with arguments ===
    100 It should be saved as `HelloWorld.py` (in the TracEnvironment's `plugins/` directory) as Trac will use the module name as the Macro name.
     98To test the following code, you should saved it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory.
    10199{{{
    102100#!python
     101from genshi.core import Markup
     102
    103103from trac.wiki.macros import WikiMacroBase
    104104
     
    118118    url = "$URL$"
    119119
    120     def expand_macro(self, formatter, name, args):
     120    def expand_macro(self, formatter, name, text, args):
    121121        """Return some output that will be displayed in the Wiki content.
    122122
    123123        `name` is the actual name of the macro (no surprise, here it'll be
    124124        `'HelloWorld'`),
    125         `args` is the text enclosed in parenthesis at the call of the macro.
     125        `text` is the text enclosed in parenthesis at the call of the macro.
    126126          Note that if there are ''no'' parenthesis (like in, e.g.
    127           [[HelloWorld]]), then `args` is `None`.
     127          [[HelloWorld]]), then `text` is `None`.
     128        `args` are the arguments passed when HelloWorld is called using a
     129        `#!HelloWorld` code block.
    128130        """
    129         return 'Hello World, args = ' + unicode(args)
    130    
    131     # Note that there's no need to HTML escape the returned data,
    132     # as the template engine (Genshi) will do it for us.
     131        return 'Hello World, text = %s, args = %s' % \
     132            (Markup.escape(text), Markup.escape(repr(args)))
     133
    133134}}}
    134135
     136Note 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 (''since 0.12'').
    135137
    136 === {{{expand_macro}}} details ===
    137 {{{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}}}.
     138'''FIXME''' when called as a macro, `args` should be `None`.
    138139
    139 If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:
     140For example, when writing:
     141{{{
     142{{{#!HelloWorld style="polite"
     143<Hello World!>
     144}}}
     145
     146{{{#!HelloWorld
     147<Hello World!>
     148}}}
     149
     150[[HelloWorld(<Hello World!>)]]
     151}}}
     152One should get:
     153{{{
     154Hello World, text = <Hello World!> , args = {'style': u'polite'}
     155Hello World, text = <Hello World!> , args = {}
     156Hello World, text = <Hello World!> , args = None
     157}}}
     158
     159Note 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`). 
     160
     161You can also recursively use a wiki Formatter (`from trac.wiki import Formatter`) to process the `text` as wiki markup, for example by doing:
    140162
    141163{{{
    142164#!python
    143   text = "whatever wiki markup you want, even containing other macros"
    144   # Convert Wiki markup to HTML, new style
    145   out = StringIO()
    146   Formatter(self.env, formatter.context).format(text, out)
    147   return Markup(out.getvalue())
     165    text = "whatever wiki markup you want, even containing other macros"
     166    # Convert Wiki markup to HTML, new style
     167    out = StringIO()
     168    Formatter(self.env, formatter.context).format(text, out)
     169    return Markup(out.getvalue())
    148170}}}