close
Warning:
Can't synchronize with repository "(default)" (/var/svn/mms does not appear to be a Subversion repository.). Look in the Trac log for more information.
- Timestamp:
-
Oct 25, 2018, 9:17:16 AM (7 years ago)
- Author:
-
trac
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
|
v1
|
v2
|
|
| 1 | | = Trac Macros = |
| | 1 | = Trac Macros |
| 2 | 2 | |
| 3 | | [[PageOutline]] |
| | 3 | [[PageOutline(2-5,Contents,pullout)]] |
| 4 | 4 | |
| 5 | | 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. |
| | 5 | '''Trac macros''' extend Trac with custom functionality. Macros are a special type of plugin and are written in Python. A macro generates HTML in any context supporting WikiFormatting. |
| 6 | 6 | |
| 7 | | Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). |
| | 7 | The macro syntax is `[[macro-name(optional-arguments)]]`. |
| 8 | 8 | |
| 9 | | == Using Macros == |
| 10 | | Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses. |
| 11 | | |
| 12 | | 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 | | |
| 14 | | === Example === |
| 15 | | |
| 16 | | A list of 3 most recently changed wiki pages starting with 'Trac': |
| | 9 | '''WikiProcessors''' are another kind of macro, commonly used for source code highlighting using a processor like `!#python` or `!#apache`: |
| 17 | 10 | |
| 18 | 11 | {{{ |
| 19 | | [[RecentChanges(Trac,3)]] |
| | 12 | {{{#!wiki-processor-name |
| | 13 | ... |
| | 14 | }}} |
| 20 | 15 | }}} |
| 21 | 16 | |
| 22 | | Display: |
| 23 | | [[RecentChanges(Trac,3)]] |
| | 17 | == Using Macros |
| 24 | 18 | |
| 25 | | == Available Macros == |
| | 19 | Macro calls are enclosed in double-square brackets `[[..]]`. Like Python functions macros can have arguments, which take the form of a comma separated list within parentheses `[[..(,)]]`. A common macro used is a list of the 3 most recent changes to a wiki page, or here, for example, all wiki pages starting with 'Trac': |
| 26 | 20 | |
| 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].'' |
| | 21 | ||= Wiki Markup =||= Display =|| |
| | 22 | {{{#!td |
| | 23 | {{{ |
| | 24 | [[RecentChanges(Trac,3)]] |
| | 25 | }}} |
| | 26 | }}} |
| | 27 | {{{#!td style="padding-left: 2em;" |
| | 28 | [[RecentChanges(Trac,3)]] |
| | 29 | }}} |
| | 30 | |
| | 31 | === Getting Detailed Help |
| | 32 | |
| | 33 | The list of available macros and the full help can be obtained using the !MacroList macro, see [#AvailableMacros below]. |
| | 34 | |
| | 35 | A brief list can be obtained via `[[MacroList(*)]]` or `[[?]]`. |
| | 36 | |
| | 37 | Detailed help on a specific macro can be obtained by passing it as an argument to !MacroList, e.g. `[[MacroList(MacroList)]]`, or more conveniently, by appending a question mark (`?`) to the macro's name, like in `[[MacroList?]]`. |
| | 38 | |
| | 39 | == Available Macros |
| 28 | 40 | |
| 29 | 41 | [[MacroList]] |
| 30 | 42 | |
| 31 | | == Macros from around the world == |
| | 43 | == Contributed macros |
| 32 | 44 | |
| 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. |
| | 45 | The [http://trac-hacks.org/ Trac Hacks] site provides a large collection of macros and other Trac [TracPlugins plugins] contributed by the Trac community. If you are looking for new macros, or have written one that you would like to share, please visit that site. |
| 34 | 46 | |
| 35 | | == Developing Custom Macros == |
| 36 | | Macros, like Trac itself, are written in the [http://python.org/ Python programming language]. |
| | 47 | == Developing Custom Macros |
| 37 | 48 | |
| 38 | | For more information about developing macros, see the [wiki:TracDev development resources] on the main project site. |
| | 49 | Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are a type of [TracPlugins plugin]. |
| 39 | 50 | |
| | 51 | Here are 2 simple examples showing how to create a Macro. For more information about developing macros, see the [trac:TracDev development resources] and [trac:browser:branches/1.2-stable/sample-plugins sample-plugins]. |
| 40 | 52 | |
| 41 | | == Implementation == |
| | 53 | === Macro without arguments |
| 42 | 54 | |
| 43 | | 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. |
| | 55 | To test the following code, copy it to `timestamp_sample.py` in the TracEnvironment's `plugins/` directory. |
| 44 | 56 | |
| 45 | | === Macro without arguments === |
| 46 | | It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name |
| 47 | | {{{ |
| 48 | | #!python |
| 49 | | from datetime import datetime |
| 50 | | # Note: since Trac 0.11, datetime objects are used internally |
| 51 | | |
| 52 | | from genshi.builder import tag |
| 53 | | |
| 54 | | from trac.util.datefmt import format_datetime, utc |
| | 57 | {{{#!python |
| | 58 | from trac.util.datefmt import datetime_now, format_datetime, utc |
| | 59 | from trac.util.html import tag |
| 55 | 60 | from trac.wiki.macros import WikiMacroBase |
| 56 | 61 | |
| 57 | 62 | class TimestampMacro(WikiMacroBase): |
| 58 | | """Inserts the current time (in seconds) into the wiki page.""" |
| | 63 | _description = "Inserts the current time (in seconds) into the wiki page." |
| 59 | 64 | |
| 60 | | revision = "$Rev$" |
| 61 | | url = "$URL$" |
| 62 | | |
| 63 | | def expand_macro(self, formatter, name, args): |
| 64 | | t = datetime.now(utc) |
| 65 | | return tag.b(format_datetime(t, '%c')) |
| | 65 | def expand_macro(self, formatter, name, content, args=None): |
| | 66 | t = datetime_now(utc) |
| | 67 | return tag.strong(format_datetime(t, '%c')) |
| 66 | 68 | }}} |
| 67 | 69 | |
| 68 | | === Macro with arguments === |
| 69 | | It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name |
| 70 | | {{{ |
| 71 | | #!python |
| | 70 | === Macro with arguments |
| | 71 | |
| | 72 | To test the following code, copy it to `helloworld_sample.py` in the TracEnvironment's `plugins/` directory. |
| | 73 | |
| | 74 | {{{#!python |
| | 75 | from trac.util.translation import cleandoc_ |
| 72 | 76 | from trac.wiki.macros import WikiMacroBase |
| 73 | 77 | |
| 74 | 78 | class HelloWorldMacro(WikiMacroBase): |
| | 79 | _description = cleandoc_( |
| 75 | 80 | """Simple HelloWorld macro. |
| 76 | 81 | |
| … |
… |
|
| 82 | 87 | will become the documentation of the macro, as shown by |
| 83 | 88 | the !MacroList macro (usually used in the WikiMacros page). |
| 84 | | """ |
| | 89 | """) |
| 85 | 90 | |
| 86 | | revision = "$Rev$" |
| 87 | | url = "$URL$" |
| 88 | | |
| 89 | | def expand_macro(self, formatter, name, args): |
| | 91 | def expand_macro(self, formatter, name, content, args=None): |
| 90 | 92 | """Return some output that will be displayed in the Wiki content. |
| 91 | 93 | |
| 92 | 94 | `name` is the actual name of the macro (no surprise, here it'll be |
| 93 | 95 | `'HelloWorld'`), |
| 94 | | `args` is the text enclosed in parenthesis at the call of the macro. |
| 95 | | Note that if there are ''no'' parenthesis (like in, e.g. |
| 96 | | [[HelloWorld]]), then `args` is `None`. |
| | 96 | `content` is the text enclosed in parenthesis at the call of the |
| | 97 | macro. Note that if there are ''no'' parenthesis (like in, e.g. |
| | 98 | [[HelloWorld]]), then `content` is `None`. |
| | 99 | `args` will contain a dictionary of arguments when called using the |
| | 100 | Wiki processor syntax and will be `None` if called using the |
| | 101 | macro syntax. |
| 97 | 102 | """ |
| 98 | | return 'Hello World, args = ' + unicode(args) |
| 99 | | |
| 100 | | # Note that there's no need to HTML escape the returned data, |
| 101 | | # as the template engine (Genshi) will do it for us. |
| | 103 | return 'Hello World, content = ' + unicode(content) |
| 102 | 104 | }}} |
| 103 | 105 | |
| | 106 | Note that `expand_macro` optionally takes a 4^th^ parameter ''`args`''. When the macro is called as a [WikiProcessors WikiProcessor], it is also possible to pass `key=value` [WikiProcessors#UsingProcessors processor parameters]. If given, those are stored in a dictionary and passed in this extra `args` parameter. When called as a macro, `args` is `None`. |
| 104 | 107 | |
| 105 | | === {{{expand_macro}}} details === |
| 106 | | {{{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}}}. |
| | 108 | For example, when writing: |
| | 109 | {{{ |
| | 110 | {{{#!HelloWorld style="polite" -silent verbose |
| | 111 | <Hello World!> |
| | 112 | }}} |
| 107 | 113 | |
| 108 | | If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: |
| | 114 | {{{#!HelloWorld |
| | 115 | <Hello World!> |
| | 116 | }}} |
| 109 | 117 | |
| | 118 | [[HelloWorld(<Hello World!>)]] |
| | 119 | }}} |
| | 120 | |
| | 121 | One should get: |
| 110 | 122 | {{{ |
| 111 | | #!python |
| 112 | | text = "whatever wiki markup you want, even containing other macros" |
| 113 | | # Convert Wiki markup to HTML, new style |
| 114 | | out = StringIO() |
| 115 | | Formatter(self.env, formatter.context).format(text, out) |
| 116 | | return Markup(out.getvalue()) |
| | 123 | Hello World, text = <Hello World!>, args = {'style': u'polite', 'silent': False, 'verbose': True} |
| | 124 | Hello World, text = <Hello World!>, args = {} |
| | 125 | Hello World, text = <Hello World!>, args = None |
| 117 | 126 | }}} |
| | 127 | |
| | 128 | Note that the return value of `expand_macro` is '''not''' HTML escaped. Depending on the expected result, you should escape it yourself (using `return Markup.escape(result)`), or if this is indeed HTML, wrap it in a Markup object: `return Markup(result)` (`from trac.util.html import Markup`). |
| | 129 | |
| | 130 | You can also recursively use a wiki formatter to process the `content` as wiki markup: |
| | 131 | |
| | 132 | {{{#!python |
| | 133 | from trac.wiki.formatter import format_to_html |
| | 134 | from trac.wiki.macros import WikiMacroBase |
| | 135 | |
| | 136 | class HelloWorldMacro(WikiMacroBase): |
| | 137 | def expand_macro(self, formatter, name, content, args): |
| | 138 | content = "any '''wiki''' markup you want, even containing other macros" |
| | 139 | # Convert Wiki markup to HTML |
| | 140 | return format_to_html(self.env, formatter.context, content) |
| | 141 | }}} |