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