11 | | Using a macro, we could pretty easily add support for RestructuredText in Trac. -- DanielLundin |
| 23 | |
| 24 | == Macros from around the world == |
| 25 | 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. |
| 26 | |
| 27 | http://projects.edgewall.com/trac/wiki/MacroBazaar |
| 28 | |
| 29 | |
| 30 | ---- |
| 31 | |
| 32 | |
| 33 | == Developing New Macros == |
| 34 | 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. |
| 35 | |
| 36 | It's easiest to learn from an example: |
| 37 | {{{ |
| 38 | # MyMacro.py -- The world's simplest macro |
| 39 | |
| 40 | def execute(hdf, args): |
| 41 | return "Hello World called with args: %s" % args |
| 42 | }}} |
| 43 | |
| 44 | === Advanced Topics: Template-enabled Macros === |
| 45 | 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. |
| 46 | |
| 47 | Macros gain direct access to the main HDF tree, and are free to manipulate it. |
| 48 | |
| 49 | Example: |
| 50 | {{{ |
| 51 | def execute(hdf, args): |
| 52 | # Currently hdf is set only when the macro is called |
| 53 | # From a wiki page |
| 54 | if hdf: |
| 55 | hdf.setValue('wiki.macro.greeting', 'Hello World') |
| 56 | |
| 57 | # args will be null if the macro is called without parentesis. |
| 58 | args = args or 'No arguments' |
| 59 | return 'Hello World, args = ' + args |
| 60 | }}} |
| 61 | |
| 62 | |
| 63 | |
| 64 | ---- |
| 65 | See also: WikiProcessors, WikiFormatting, TracGuide |