15 | | [[Timestamp]] |
| 23 | [[RecentChanges(Trac,3)]] |
| 24 | |
| 25 | == Available Macros == |
| 26 | |
| 27 | ''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 | [[MacroList]] |
| 30 | |
| 31 | == Macros from around the world == |
| 32 | |
| 33 | 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. |
| 34 | |
| 35 | == Developing Custom Macros == |
| 36 | Macros, like Trac itself, are written in the [http://python.org/ Python programming language]. |
| 37 | |
| 38 | For more information about developing macros, see the [trac:TracDev development resources] on the main project site. |
| 39 | |
| 40 | |
| 41 | == Implementation == |
| 42 | |
| 43 | Here are 2 simple examples showing how to create a Macro with Trac 0.11. |
| 44 | |
| 45 | Also, have a look at [trac:source:tags/trac-0.11/sample-plugins/Timestamp.py Timestamp.py] for an example that shows the difference between old style and new style macros and at the [trac:source:tags/trac-0.11/wiki-macros/README macros/README] which provides a little more insight about the transition. |
| 46 | |
| 47 | === Macro without arguments === |
| 48 | It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name |
| 49 | {{{ |
| 50 | #!python |
| 51 | from datetime import datetime |
| 52 | # Note: since Trac 0.11, datetime objects are used internally |
| 53 | |
| 54 | from genshi.builder import tag |
| 55 | |
| 56 | from trac.util.datefmt import format_datetime, utc |
| 57 | from trac.wiki.macros import WikiMacroBase |
| 58 | |
| 59 | class TimeStampMacro(WikiMacroBase): |
| 60 | """Inserts the current time (in seconds) into the wiki page.""" |
| 61 | |
| 62 | revision = "$Rev$" |
| 63 | url = "$URL$" |
| 64 | |
| 65 | def expand_macro(self, formatter, name, args): |
| 66 | t = datetime.now(utc) |
| 67 | return tag.b(format_datetime(t, '%c')) |
| 68 | }}} |
| 69 | |
| 70 | === Macro with arguments === |
| 71 | It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name |
| 72 | {{{ |
| 73 | #!python |
| 74 | from trac.wiki.macros import WikiMacroBase |
| 75 | |
| 76 | class HelloWorldMacro(WikiMacroBase): |
| 77 | """Simple HelloWorld macro. |
| 78 | |
| 79 | Note that the name of the class is meaningful: |
| 80 | - it must end with "Macro" |
| 81 | - what comes before "Macro" ends up being the macro name |
| 82 | |
| 83 | The documentation of the class (i.e. what you're reading) |
| 84 | will become the documentation of the macro, as shown by |
| 85 | the !MacroList macro (usually used in the WikiMacros page). |
| 86 | """ |
| 87 | |
| 88 | revision = "$Rev$" |
| 89 | url = "$URL$" |
| 90 | |
| 91 | def expand_macro(self, formatter, name, args): |
| 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 | `args` is the text enclosed in parenthesis at the call of the macro. |
| 97 | Note that if there are ''no'' parenthesis (like in, e.g. |
| 98 | [[HelloWorld]]), then `args` is `None`. |
| 99 | """ |
| 100 | return 'Hello World, args = ' + unicode(args) |
| 101 | |
| 102 | # Note that there's no need to HTML escape the returned data, |
| 103 | # as the template engine (Genshi) will do it for us. |
| 104 | }}} |
| 105 | |
| 106 | |
| 107 | === {{{expand_macro}}} details === |
| 108 | {{{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}}}. |
| 109 | |
| 110 | If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: |
20 | | Display: |
21 | | [[HelloWorld(Testing)]] |
22 | | |
23 | | |
24 | | == Available Macros == |
25 | | Macros are still a new feature, and the list of available (and distributed) macros is |
26 | | admittedly not very impressive. In future Trac releases, we hope to build a library of useful macros, and will of course happily include contributed macros (see below). |
27 | | |
28 | | * '''!HelloWorld''' -- An example macro, useful for learning how to write macros. |
29 | | * '''Timestamp''' -- Insert the current date and time. |
30 | | |
31 | | |
32 | | ---- |
33 | | |
34 | | |
35 | | == Macros from around the world == |
36 | | The [http://projects.edgewall.com/trac/ Trac Project] has a section dedicated to user-contributed macros, [http://projects.edgewall.com/trac/wiki/MacroBazaar MacroBazaar]. If you're looking for new macros, or have written new ones to share with the world, don't hesitate adding it to the [http://projects.edgewall.com/trac/wiki/MacroBazaar MacroBazaar] wiki page. |
37 | | |
38 | | http://projects.edgewall.com/trac/wiki/MacroBazaar |
39 | | |
40 | | |
41 | | ---- |
42 | | |
43 | | |
44 | | == Developing New Macros == |
45 | | Macros, like Trac itself, are written in the [http://www.python.org/ Python programming language]. They are very simple modules, identified by the filename and should contain a single ''entry point'' function. Trac will display the returned data inserted into the HTML where the macro was called. |
46 | | |
47 | | It's easiest to learn from an example: |
48 | | {{{ |
49 | | # MyMacro.py -- The world's simplest macro |
50 | | |
51 | | def execute(hdf, args, env): |
52 | | return "Hello World called with args: %s" % args |
53 | | }}} |
54 | | |
55 | | === Advanced Topics: Template-enabled Macros === |
56 | | For advanced uses, macros can also render structured output in HDF, to be rendered to HTML using clearsilver templates - like most Trac output. In short, this allows more generic and well-designed advanced macros. |
57 | | |
58 | | Macros gain direct access to the main HDF tree, and are free to manipulate it. |
59 | | |
60 | | Example: |
61 | | {{{ |
62 | | def execute(hdf, args, env): |
63 | | # Currently hdf is set only when the macro is called |
64 | | # From a wiki page |
65 | | if hdf: |
66 | | hdf.setValue('wiki.macro.greeting', 'Hello World') |
67 | | |
68 | | # args will be null if the macro is called without parentesis. |
69 | | args = args or 'No arguments' |
70 | | return 'Hello World, args = ' + args |
71 | | }}} |
72 | | |
73 | | You can also use the environment (env) object to access configuration data. |
74 | | |
75 | | Example. |
76 | | {{{ |
77 | | def execute(hdf, txt, env): |
78 | | return env.get_config('trac', 'repository_dir') |
79 | | }}} |
80 | | ---- |
81 | | See also: WikiProcessors, WikiFormatting, TracGuide |