Changes between Version 54 and Version 55 of WikiMacros
- Timestamp:
- Jul 8, 2018, 12:12:54 AM (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
WikiMacros
v54 v55 4 4 [[TranslatedPages]] 5 5 6 '''Trac macros''' extend the Trac engine with custom functionality. Macros are a special type of plugin and are written in Python. A macro inserts dynamic HTML datain any context supporting WikiFormatting.6 '''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. 7 7 8 8 The macro syntax is `[[macro-name(optional-arguments)]]`. 9 9 10 '''WikiProcessors''' are another kind of macro s. They are typically used for source code highlighting, such as `!#python` or `!#apache` and when the source code spans multiple lines, such as:10 '''WikiProcessors''' are another kind of macro, commonly used for source code highlighting using a processor like `!#python` or `!#apache`: 11 11 12 12 {{{ … … 18 18 == Using Macros 19 19 20 Macro calls are enclosed in double-square brackets `[[..]]`. Like Python functions , macros can have arguments, which is thena comma separated list within parentheses `[[..(,)]]`.20 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 `[[..(,)]]`. 21 21 22 22 === Getting Detailed Help 23 23 24 The list of available macros and the full help can be obtained using the !MacroList macro, as seen[#AvailableMacros below].24 The list of available macros and the full help can be obtained using the !MacroList macro, see [#AvailableMacros below]. 25 25 26 26 A brief list can be obtained via `[[MacroList(*)]]` or `[[?]]`. 27 27 28 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?]]`.28 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?]]`. 29 29 30 30 === Example … … 72 72 == Available Macros 73 73 74 {{{#!box note 75 The site includes several macros provided by plugins. The [/demo-1.2/wiki/WikiMacros demo site] shows only the macros provided by Trac. 76 }}} 77 74 78 [[MacroList]] 75 79 76 80 == Macros from around the world 77 81 78 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 are looking for new macros, or have written one that you would like to share, please visit that site.82 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. 79 83 80 84 == Developing Custom Macros 81 85 82 Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are developed as part of TracPlugins.86 Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are a type of [TracPlugins plugin]. 83 87 84 For more information about developing macros, see the [trac:TracDev development resources] on the main project site. 85 86 Here are 2 simple examples showing how to create a Macro. Also, have a look at [trac:source:tags/trac-1.0.2/sample-plugins/Timestamp.py Timestamp.py] for an example that shows the difference between old style and new style macros and at the [trac:source:tags/trac-0.11/wiki-macros/README macros/README] which provides more insight about the transition. 88 Here are 2 simple examples showing how to create a Macro. For more information about developing macros, see the [trac:TracDev development resources]. 87 89 88 90 === Macro without arguments 89 91 90 To test the following code, save it in a `timestamp_sample.py` file locatedin the TracEnvironment's `plugins/` directory.92 To test the following code, copy it to `timestamp_sample.py` in the TracEnvironment's `plugins/` directory. 91 93 92 94 {{{#!python 93 from datetime import datetime 94 # Note: since Trac 0.11, datetime objects are used internally 95 96 from trac.util.datefmt import format_datetime, utc 95 from trac.util.datefmt import datetime_now, format_datetime, utc 97 96 from trac.util.html import tag 98 97 from trac.wiki.macros import WikiMacroBase 99 98 100 class TimeStampMacro(WikiMacroBase):101 """Inserts the current time (in seconds) into the wiki page."""102 99 103 revision = "$Rev$" 104 url = "$URL$"100 class TimestampMacro(WikiMacroBase): 101 _description = "Inserts the current time (in seconds) into the wiki page." 105 102 106 def expand_macro(self, formatter, name, text):107 t = datetime .now(utc)103 def expand_macro(self, formatter, name, content, args=None): 104 t = datetime_now(utc) 108 105 return tag.strong(format_datetime(t, '%c')) 109 106 }}} … … 111 108 === Macro with arguments 112 109 113 To test the following code, save it in a `helloworld_sample.py` file locatedin the TracEnvironment's `plugins/` directory.110 To test the following code, copy it to `helloworld_sample.py` in the TracEnvironment's `plugins/` directory. 114 111 115 112 {{{#!python 116 from trac.util. html import Markup113 from trac.util.translation import cleandoc_ 117 114 from trac.wiki.macros import WikiMacroBase 118 115 116 119 117 class HelloWorldMacro(WikiMacroBase): 118 _description = cleandoc_( 120 119 """Simple HelloWorld macro. 121 120 … … 127 126 will become the documentation of the macro, as shown by 128 127 the !MacroList macro (usually used in the WikiMacros page). 129 """ 128 """) 130 129 131 revision = "$Rev$" 132 url = "$URL$" 133 134 def expand_macro(self, formatter, name, text, args): 130 def expand_macro(self, formatter, name, content, args=None): 135 131 """Return some output that will be displayed in the Wiki content. 136 132 137 133 `name` is the actual name of the macro (no surprise, here it'll be 138 134 `'HelloWorld'`), 139 `text` is the text enclosed in parenthesis at the call of the macro. 140 Note that if there are ''no'' parenthesis (like in, e.g. 141 [[HelloWorld]]), then `text` is `None`. 142 `args` are the arguments passed when HelloWorld is called using a 143 `#!HelloWorld` code block. 135 `content` is the text enclosed in parenthesis at the call of the 136 macro. Note that if there are ''no'' parenthesis (like in, e.g. 137 [[HelloWorld]]), then `content` is `None`. 138 `args` will contain a dictionary of arguments when called using the 139 Wiki processor syntax and will be `None` if called using the 140 macro syntax. 144 141 """ 145 return 'Hello World, text = %s, args = %s' % \ 146 (Markup.escape(text), Markup.escape(repr(args))) 147 142 return 'Hello World, content = ' + unicode(content) 148 143 }}} 149 144 150 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. In the other case, when called as a macro, `args` is `None`. (''since 0.12'').145 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`. 151 146 152 147 For example, when writing: … … 170 165 }}} 171 166 172 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`).167 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`). 173 168 174 169 You can also recursively use a wiki formatter to process the `content` as wiki markup: