TracWikiMacros » History » Version 2
Anonymous, 03/15/2008 04:41 PM
| 1 | 1 | ||
|---|---|---|---|
| 2 | 2 | h1. Trac Macros |
|
| 3 | |||
| 4 | |||
| 5 | 1 | [[PageOutline]] |
|
| 6 | |||
| 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 | |||
| 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 | |||
| 18 | |||
| 19 | h3. Example |
||
| 20 | |||
| 21 | |||
| 22 | 1 | A list of 3 most recently changed wiki pages starting with 'Trac': |
|
| 23 | |||
| 24 | 2 | <pre> |
|
| 25 | 1 | [[RecentChanges(Trac,3)]] |
|
| 26 | 2 | </pre> |
|
| 27 | 1 | ||
| 28 | Display: |
||
| 29 | [[RecentChanges(Trac,3)]] |
||
| 30 | |||
| 31 | |||
| 32 | 2 | h2. Available Macros |
|
| 33 | 1 | ||
| 34 | 2 | ||
| 35 | _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 | |||
| 37 | 1 | [[MacroList]] |
|
| 38 | |||
| 39 | |||
| 40 | 2 | h2. Macros from around the world |
|
| 41 | 1 | ||
| 42 | |||
| 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 | |||
| 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 | |||
| 52 | |||
| 53 | |||
| 54 | h2. Implementation |
||
| 55 | |||
| 56 | |||
| 57 | 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 | |||
| 59 | |||
| 60 | h3. Macro without arguments |
||
| 61 | |||
| 62 | It should be saved as @TimeStamp.py@ as Trac will use the module name as the Macro name |
||
| 63 | <pre> |
||
| 64 | <code class="python"> |
||
| 65 | 1 | from datetime import datetime |
|
| 66 | # Note: since Trac 0.11, datetime objects are used internally |
||
| 67 | |||
| 68 | from genshi.builder import tag |
||
| 69 | |||
| 70 | 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 | |||
| 76 | revision = "$Rev$" |
||
| 77 | url = "$URL$" |
||
| 78 | |||
| 79 | def expand_macro(self, formatter, name, args): |
||
| 80 | t = datetime.now(utc) |
||
| 81 | return tag.b(format_datetime(t, '%c')) |
||
| 82 | 2 | </code></pre> |
|
| 83 | 1 | ||
| 84 | |||
| 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 | <pre> |
||
| 89 | <code class="python"> |
||
| 90 | from trac.wiki.macros import [[WikiMacroBase]] |
||
| 91 | |||
| 92 | class [[HelloWorldMacro]](WikiMacroBase): |
||
| 93 | """Simple [[HelloWorld]] macro. |
||
| 94 | |||
| 95 | 1 | Note that the name of the class is meaningful: |
|
| 96 | - it must end with "Macro" |
||
| 97 | - what comes before "Macro" ends up being the macro name |
||
| 98 | |||
| 99 | The documentation of the class (i.e. what you're reading) |
||
| 100 | will become the documentation of the macro, as shown by |
||
| 101 | 2 | the MacroList macro (usually used in the [[TracWikiMacros]] page). |
|
| 102 | 1 | """ |
|
| 103 | |||
| 104 | revision = "$Rev$" |
||
| 105 | url = "$URL$" |
||
| 106 | |||
| 107 | def expand_macro(self, formatter, name, args): |
||
| 108 | """Return some output that will be displayed in the Wiki content. |
||
| 109 | |||
| 110 | 2 | @name@ is the actual name of the macro (no surprise, here it'll be |
|
| 111 | @'HelloWorld'@), |
||
| 112 | @args@ is the text enclosed in parenthesis at the call of the macro. |
||
| 113 | Note that if there are _no_ parenthesis (like in, e.g. |
||
| 114 | [[HelloWorld]]), then @args@ is @None@. |
||
| 115 | 1 | """ |
|
| 116 | return 'Hello World, args = ' + unicode(args) |
||
| 117 | |||
| 118 | # Note that there's no need to HTML escape the returned data, |
||
| 119 | # as the template engine (Genshi) will do it for us. |
||
| 120 | 2 | </code></pre> |
|
| 121 | 1 | ||
| 122 | |||
| 123 | |||
| 124 | 2 | <pre> |
|
| 125 | |||
| 126 | <pre> |
||
| 127 | |||
| 128 | 1 | If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: |
|
| 129 | |||
| 130 | 2 | <pre> |
|
| 131 | <code class="python"> |
||
| 132 | 1 | text = "whatever wiki markup you want, even containing other macros" |
|
| 133 | # Convert Wiki markup to HTML, new style |
||
| 134 | 2 | out = [[StringIO]]() |
|
| 135 | 1 | Formatter(formatter.context).format(text, out) |
|
| 136 | return Markup(out.getvalue()) |
||
| 137 | 2 | </code></pre> |