TracWikiMacros
Version 1 (Anonymous, 03/15/2008 04:41 PM)
1 | 1 | = Trac Macros = |
|
---|---|---|---|
2 | 1 | ||
3 | 1 | [[PageOutline]] |
|
4 | 1 | ||
5 | 1 | 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 | 1 | ||
7 | 1 | 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 | 1 | ||
9 | 1 | == Using Macros == |
|
10 | 1 | Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses. |
|
11 | 1 | ||
12 | 1 | 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 | 1 | ||
14 | 1 | === Example === |
|
15 | 1 | ||
16 | 1 | A list of 3 most recently changed wiki pages starting with 'Trac': |
|
17 | 1 | ||
18 | 1 | {{{ |
|
19 | 1 | [[RecentChanges(Trac,3)]] |
|
20 | 1 | }}} |
|
21 | 1 | ||
22 | 1 | Display: |
|
23 | 1 | [[RecentChanges(Trac,3)]] |
|
24 | 1 | ||
25 | 1 | == Available Macros == |
|
26 | 1 | ||
27 | 1 | ''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 | 1 | ||
29 | 1 | [[MacroList]] |
|
30 | 1 | ||
31 | 1 | == Macros from around the world == |
|
32 | 1 | ||
33 | 1 | 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 | 1 | ||
35 | 1 | == Developing Custom Macros == |
|
36 | 1 | Macros, like Trac itself, are written in the [http://python.org/ Python programming language]. |
|
37 | 1 | ||
38 | 1 | For more information about developing macros, see the [wiki:TracDev development resources] on the main project site. |
|
39 | 1 | ||
40 | 1 | ||
41 | 1 | == Implementation == |
|
42 | 1 | ||
43 | 1 | 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 about the transition. |
|
44 | 1 | ||
45 | 1 | === Macro without arguments === |
|
46 | 1 | It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name |
|
47 | 1 | {{{ |
|
48 | 1 | #!python |
|
49 | 1 | from datetime import datetime |
|
50 | 1 | # Note: since Trac 0.11, datetime objects are used internally |
|
51 | 1 | ||
52 | 1 | from genshi.builder import tag |
|
53 | 1 | ||
54 | 1 | from trac.util.datefmt import format_datetime, utc |
|
55 | 1 | from trac.wiki.macros import WikiMacroBase |
|
56 | 1 | ||
57 | 1 | class TimestampMacro(WikiMacroBase): |
|
58 | 1 | """Inserts the current time (in seconds) into the wiki page.""" |
|
59 | 1 | ||
60 | 1 | revision = "$Rev$" |
|
61 | 1 | url = "$URL$" |
|
62 | 1 | ||
63 | 1 | def expand_macro(self, formatter, name, args): |
|
64 | 1 | t = datetime.now(utc) |
|
65 | 1 | return tag.b(format_datetime(t, '%c')) |
|
66 | 1 | }}} |
|
67 | 1 | ||
68 | 1 | === Macro with arguments === |
|
69 | 1 | It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name |
|
70 | 1 | {{{ |
|
71 | 1 | #!python |
|
72 | 1 | from trac.wiki.macros import WikiMacroBase |
|
73 | 1 | ||
74 | 1 | class HelloWorldMacro(WikiMacroBase): |
|
75 | 1 | """Simple HelloWorld macro. |
|
76 | 1 | ||
77 | 1 | Note that the name of the class is meaningful: |
|
78 | 1 | - it must end with "Macro" |
|
79 | 1 | - what comes before "Macro" ends up being the macro name |
|
80 | 1 | ||
81 | 1 | The documentation of the class (i.e. what you're reading) |
|
82 | 1 | will become the documentation of the macro, as shown by |
|
83 | 1 | the !MacroList macro (usually used in the TracWikiMacros page). |
|
84 | 1 | """ |
|
85 | 1 | ||
86 | 1 | revision = "$Rev$" |
|
87 | 1 | url = "$URL$" |
|
88 | 1 | ||
89 | 1 | def expand_macro(self, formatter, name, args): |
|
90 | 1 | """Return some output that will be displayed in the Wiki content. |
|
91 | 1 | ||
92 | 1 | `name` is the actual name of the macro (no surprise, here it'll be |
|
93 | 1 | `'HelloWorld'`), |
|
94 | 1 | `args` is the text enclosed in parenthesis at the call of the macro. |
|
95 | 1 | Note that if there are ''no'' parenthesis (like in, e.g. |
|
96 | 1 | [[HelloWorld]]), then `args` is `None`. |
|
97 | 1 | """ |
|
98 | 1 | return 'Hello World, args = ' + unicode(args) |
|
99 | 1 | ||
100 | 1 | # Note that there's no need to HTML escape the returned data, |
|
101 | 1 | # as the template engine (Genshi) will do it for us. |
|
102 | 1 | }}} |
|
103 | 1 | ||
104 | 1 | ||
105 | 1 | === {{{expand_macro}}} details === |
|
106 | 1 | {{{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. |
|
107 | 1 | ||
108 | 1 | If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: |
|
109 | 1 | ||
110 | 1 | {{{ |
|
111 | 1 | #!python |
|
112 | 1 | text = "whatever wiki markup you want, even containing other macros" |
|
113 | 1 | # Convert Wiki markup to HTML, new style |
|
114 | 1 | out = StringIO() |
|
115 | 1 | Formatter(formatter.context).format(text, out) |
|
116 | 1 | return Markup(out.getvalue()) |
|
117 | 1 | }}} |