Changes between Version 2 and Version 3 of 0.12/WikiMacros
- Timestamp:
- Feb 2, 2010, 5:19:22 PM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
0.12/WikiMacros
v2 v3 68 68 69 69 70 == Implementation ==71 72 70 Here are 2 simple examples showing how to create a Macro with Trac 0.11. 73 71 … … 75 73 76 74 === 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.75 To test the following code, you should saved it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory. 78 76 {{{ 79 77 #!python … … 92 90 url = "$URL$" 93 91 94 def expand_macro(self, formatter, name, args):92 def expand_macro(self, formatter, name, text): 95 93 t = datetime.now(utc) 96 94 return tag.b(format_datetime(t, '%c')) … … 98 96 99 97 === 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.98 To test the following code, you should saved it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory. 101 99 {{{ 102 100 #!python 101 from genshi.core import Markup 102 103 103 from trac.wiki.macros import WikiMacroBase 104 104 … … 118 118 url = "$URL$" 119 119 120 def expand_macro(self, formatter, name, args):120 def expand_macro(self, formatter, name, text, args): 121 121 """Return some output that will be displayed in the Wiki content. 122 122 123 123 `name` is the actual name of the macro (no surprise, here it'll be 124 124 `'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. 126 126 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. 128 130 """ 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 133 134 }}} 134 135 136 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 (''since 0.12''). 135 137 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`. 138 139 139 If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: 140 For 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 }}} 152 One should get: 153 {{{ 154 Hello World, text = <Hello World!> , args = {'style': u'polite'} 155 Hello World, text = <Hello World!> , args = {} 156 Hello World, text = <Hello World!> , args = None 157 }}} 158 159 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`). 160 161 You can also recursively use a wiki Formatter (`from trac.wiki import Formatter`) to process the `text` as wiki markup, for example by doing: 140 162 141 163 {{{ 142 164 #!python 143 text = "whatever wiki markup you want, even containing other macros"144 # Convert Wiki markup to HTML, new style145 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()) 148 170 }}}