| 25 | | ''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].'' |
| | 33 | The list of available macros and the full help can be obtained using the !MacroList macro, see [#AvailableMacros below]. |
| | 34 | |
| | 35 | A brief list can be obtained via `[[MacroList(*)]]` or `[[?]]`. |
| | 36 | |
| | 37 | 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?]]`. |
| | 38 | |
| | 39 | == Available Macros |
| 41 | | def execute(hdf, args, env): |
| 42 | | return "Hello World called with args: %s" % args |
| | 51 | Here are 2 simple examples showing how to create a Macro. For more information about developing macros, see the [trac:TracDev development resources] and [trac:browser:branches/1.4-stable/sample-plugins sample-plugins]. |
| | 52 | |
| | 53 | === Macro without arguments |
| | 54 | |
| | 55 | To test the following code, copy it to `timestamp_sample.py` in the TracEnvironment's `plugins/` directory. |
| | 56 | |
| | 57 | {{{#!python |
| | 58 | from trac.util.datefmt import datetime_now, format_datetime, utc |
| | 59 | from trac.util.html import tag |
| | 60 | from trac.wiki.macros import WikiMacroBase |
| | 61 | |
| | 62 | class TimestampMacro(WikiMacroBase): |
| | 63 | _description = "Inserts the current time (in seconds) into the wiki page." |
| | 64 | |
| | 65 | def expand_macro(self, formatter, name, content, args=None): |
| | 66 | t = datetime_now(utc) |
| | 67 | return tag.strong(format_datetime(t, '%c')) |
| 45 | | You can also use the environment (`env`) object, for example to access configuration data and the database, for example: |
| 46 | | {{{ |
| 47 | | #!python |
| 48 | | def execute(hdf, txt, env): |
| 49 | | return env.config.get('trac', 'repository_dir') |
| | 70 | === Macro with arguments |
| | 71 | |
| | 72 | To test the following code, copy it to `helloworld_sample.py` in the TracEnvironment's `plugins/` directory. |
| | 73 | |
| | 74 | {{{#!python |
| | 75 | from trac.util.translation import cleandoc_ |
| | 76 | from trac.wiki.macros import WikiMacroBase |
| | 77 | |
| | 78 | class HelloWorldMacro(WikiMacroBase): |
| | 79 | _description = cleandoc_( |
| | 80 | """Simple HelloWorld macro. |
| | 81 | |
| | 82 | Note that the name of the class is meaningful: |
| | 83 | - it must end with "Macro" |
| | 84 | - what comes before "Macro" ends up being the macro name |
| | 85 | |
| | 86 | The documentation of the class (i.e. what you're reading) |
| | 87 | will become the documentation of the macro, as shown by |
| | 88 | the !MacroList macro (usually used in the WikiMacros page). |
| | 89 | """) |
| | 90 | |
| | 91 | def expand_macro(self, formatter, name, content, args=None): |
| | 92 | """Return some output that will be displayed in the Wiki content. |
| | 93 | |
| | 94 | `name` is the actual name of the macro (no surprise, here it'll be |
| | 95 | `'HelloWorld'`), |
| | 96 | `content` is the text enclosed in parenthesis at the call of the |
| | 97 | macro. Note that if there are ''no'' parenthesis (like in, e.g. |
| | 98 | [[HelloWorld]]), then `content` is `None`. |
| | 99 | `args` will contain a dictionary of arguments when called using the |
| | 100 | Wiki processor syntax and will be `None` if called using the |
| | 101 | macro syntax. |
| | 102 | """ |
| | 103 | return 'Hello World, content = ' + unicode(content) |
| 56 | | ---- |
| 57 | | See also: WikiProcessors, WikiFormatting, TracGuide |
| | 114 | {{{#!HelloWorld |
| | 115 | <Hello World!> |
| | 116 | }}} |
| | 117 | |
| | 118 | [[HelloWorld(<Hello World!>)]] |
| | 119 | }}} |
| | 120 | |
| | 121 | One should get: |
| | 122 | {{{ |
| | 123 | Hello World, text = <Hello World!>, args = {'style': u'polite', 'silent': False, 'verbose': True} |
| | 124 | Hello World, text = <Hello World!>, args = {} |
| | 125 | Hello World, text = <Hello World!>, args = None |
| | 126 | }}} |
| | 127 | |
| | 128 | Note that the return value of `expand_macro` is '''not''' HTML escaped. Depending on the expected result, you should escape it yourself (using `return Markup.escape(result)`), or if this is indeed HTML, wrap it in a Markup object: `return Markup(result)` (`from trac.util.html import Markup`). |
| | 129 | |
| | 130 | You can also recursively use a wiki formatter to process the `content` as wiki markup: |
| | 131 | |
| | 132 | {{{#!python |
| | 133 | from trac.wiki.formatter import format_to_html |
| | 134 | from trac.wiki.macros import WikiMacroBase |
| | 135 | |
| | 136 | class HelloWorldMacro(WikiMacroBase): |
| | 137 | def expand_macro(self, formatter, name, content, args): |
| | 138 | content = "any '''wiki''' markup you want, even containing other macros" |
| | 139 | # Convert Wiki markup to HTML |
| | 140 | return format_to_html(self.env, formatter.context, content) |
| | 141 | }}} |