| | 1 | = Trac Macros = |
| | 2 | |
| | 3 | [[PageOutline]] |
| | 4 | |
| | 5 | Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. A macro inserts dynamic HTML data in any context supporting WikiFormatting. |
| | 6 | |
| | 7 | Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). |
| | 8 | |
| | 9 | == Using Macros == |
| | 10 | Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses. |
| | 11 | |
| | 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. |
| | 13 | |
| | 14 | === Examples === |
| | 15 | |
| | 16 | {{{ |
| | 17 | [[Timestamp]] |
| | 18 | }}} |
| | 19 | Display: |
| | 20 | [[Timestamp]] |
| | 21 | |
| | 22 | {{{ |
| | 23 | [[HelloWorld(Testing)]] |
| | 24 | }}} |
| | 25 | Display: |
| | 26 | [[HelloWorld(Testing)]] |
| | 27 | |
| | 28 | == Available Macros == |
| | 29 | |
| | 30 | ''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].'' |
| | 31 | |
| | 32 | [[MacroList]] |
| | 33 | |
| | 34 | == Macros from around the world == |
| | 35 | |
| | 36 | 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're looking for new macros, or have written one that you'd like to share with the world, please don't hesitate to visit that site. |
| | 37 | |
| | 38 | == Developing Custom Macros == |
| | 39 | Macros, like Trac itself, are written in the [http://python.org/ Python programming language]. |
| | 40 | |
| | 41 | For more information about developing macros, see the [wiki:TracDev development resources] on the main project site. |
| | 42 | |
| | 43 | |
| | 44 | == Implementation == |
| | 45 | |
| | 46 | Here are 2 simple examples on how to create a Macro with [wiki:0.11 Trac 0.11] have a look at source:trunk/sample-plugins/Timestamp.py for an example that shows the difference between old style and new style macros and also source:trunk/wiki-macros/README which Provides a little more insight. |
| | 47 | |
| | 48 | === Macro without arguments === |
| | 49 | It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name |
| | 50 | {{{ |
| | 51 | from trac.core import * |
| | 52 | from trac.wiki.macros import WikiMacroBase |
| | 53 | from StringIO import StringIO |
| | 54 | import time |
| | 55 | |
| | 56 | __all__ = ['TimestampMacro'] |
| | 57 | |
| | 58 | class TimestampMacro(WikiMacroBase): |
| | 59 | """ |
| | 60 | Macro for inserting timestamp |
| | 61 | |
| | 62 | {{{ |
| | 63 | [[Timestamp]] |
| | 64 | }}} |
| | 65 | """ |
| | 66 | def expand_macro(self, formatter, name, args): |
| | 67 | buf = StringIO() |
| | 68 | t = time.localtime() |
| | 69 | buf = "<b>%s</b>" % time.strftime('%c', t) |
| | 70 | return buf |
| | 71 | }}} |
| | 72 | |
| | 73 | === Macro with arguments === |
| | 74 | It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name |
| | 75 | {{{ |
| | 76 | """Example macro.""" |
| | 77 | from trac.core import * |
| | 78 | from trac.wiki.macros import WikiMacroBase |
| | 79 | from trac.util import escape |
| | 80 | |
| | 81 | __all__ = ['HelloWorldMacro'] |
| | 82 | |
| | 83 | class HelloWorldMacro(WikiMacroBase): |
| | 84 | """ |
| | 85 | Demo macro for a greeting with an argument. |
| | 86 | |
| | 87 | {{{ |
| | 88 | [[HelloWorld(args)]] |
| | 89 | }}} |
| | 90 | |
| | 91 | """ |
| | 92 | def expand_macro(self, formatter, name, args): |
| | 93 | # args will be `None` if the macro is called without parenthesis. |
| | 94 | txt = args or 'No arguments' |
| | 95 | |
| | 96 | # then, as `txt` comes from the user, it's important to guard against |
| | 97 | # the possibility to inject malicious HTML/Javascript, by using `escape()`: |
| | 98 | return 'Hello World, args = ' + escape(txt) |
| | 99 | }}} |
| | 100 | |
| | 101 | |
| | 102 | === {{{expand_macro}}} details === |
| | 103 | {{{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. |
| | 104 | |
| | 105 | If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: |
| | 106 | |
| | 107 | {{{ |
| | 108 | text = "whatever wiki markup you want, even containing other macros" |
| | 109 | # Convert Wiki markup to HTML, new style |
| | 110 | out = StringIO() |
| | 111 | Formatter(formatter.context).format(text, out) |
| | 112 | return Markup(out.getvalue()) |
| | 113 | }}} |