Edgewall Software

Changes between Initial Version and Version 1 of 1.2/WikiMacros


Ignore:
Timestamp:
Oct 15, 2019, 6:33:07 AM (2 years ago)
Author:
trac
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • 1.2/WikiMacros

    v1 v1  
     1= Trac Macros
     2
     3[[PageOutline(2-5,Contents,pullout)]]
     4[[TranslatedPages]]
     5
     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
     8The macro syntax is `[[macro-name(optional-arguments)]]`.
     9
     10'''WikiProcessors''' are another kind of macro, commonly used for source code highlighting using a processor like `!#python` or `!#apache`:
     11
     12{{{
     13{{{#!wiki-processor-name
     14...
     15}}}
     16}}}
     17
     18== Using Macros
     19
     20Macro 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':
     21
     22||= Wiki Markup =||= Display =||
     23{{{#!td
     24  {{{
     25  [[RecentChanges(Trac,3)]]
     26  }}}
     27}}}
     28{{{#!td style="padding-left: 2em;"
     29[[RecentChanges(Trac,3)]]
     30}}}
     31
     32=== Getting Detailed Help
     33
     34The list of available macros and the full help can be obtained using the !MacroList macro, see [#AvailableMacros below].
     35
     36A brief list can be obtained via `[[MacroList(*)]]` or `[[?]]`.
     37
     38Detailed 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?]]`.
     39
     40== Available Macros
     41
     42{{{#!box note
     43The site includes several macros provided by plugins. The [/demo-1.2/wiki/WikiMacros demo site] shows only the macros provided by Trac.
     44}}}
     45
     46[[MacroList]]
     47
     48== Contributed macros
     49
     50The [https://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.
     51
     52== Developing Custom Macros
     53
     54Macros, like Trac itself, are written in the [https://python.org/ Python programming language] and are a type of [TracPlugins plugin].
     55
     56Here 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].
     57
     58=== Macro without arguments
     59
     60To test the following code, copy it to `timestamp_sample.py` in the TracEnvironment's `plugins/` directory.
     61
     62{{{#!python
     63from trac.util.datefmt import datetime_now, format_datetime, utc
     64from trac.util.html import tag
     65from trac.wiki.macros import WikiMacroBase
     66
     67class TimestampMacro(WikiMacroBase):
     68    _description = "Inserts the current time (in seconds) into the wiki page."
     69
     70    def expand_macro(self, formatter, name, content, args=None):
     71        t = datetime_now(utc)
     72        return tag.strong(format_datetime(t, '%c'))
     73}}}
     74
     75=== Macro with arguments
     76
     77To test the following code, copy it to `helloworld_sample.py` in the TracEnvironment's `plugins/` directory.
     78
     79{{{#!python
     80from trac.util.translation import cleandoc_
     81from trac.wiki.macros import WikiMacroBase
     82
     83class HelloWorldMacro(WikiMacroBase):
     84    _description = cleandoc_(
     85    """Simple HelloWorld macro.
     86
     87    Note that the name of the class is meaningful:
     88     - it must end with "Macro"
     89     - what comes before "Macro" ends up being the macro name
     90
     91    The documentation of the class (i.e. what you're reading)
     92    will become the documentation of the macro, as shown by
     93    the !MacroList macro (usually used in the WikiMacros page).
     94    """)
     95
     96    def expand_macro(self, formatter, name, content, args=None):
     97        """Return some output that will be displayed in the Wiki content.
     98
     99        `name` is the actual name of the macro (no surprise, here it'll be
     100        `'HelloWorld'`),
     101        `content` is the text enclosed in parenthesis at the call of the
     102          macro. Note that if there are ''no'' parenthesis (like in, e.g.
     103          [[HelloWorld]]), then `content` is `None`.
     104        `args` will contain a dictionary of arguments when called using the
     105          Wiki processor syntax and will be `None` if called using the
     106          macro syntax.
     107        """
     108        return 'Hello World, content = ' + unicode(content)
     109}}}
     110
     111Note 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`.
     112
     113For example, when writing:
     114{{{
     115{{{#!HelloWorld style="polite" -silent verbose
     116<Hello World!>
     117}}}
     118
     119{{{#!HelloWorld
     120<Hello World!>
     121}}}
     122
     123[[HelloWorld(<Hello World!>)]]
     124}}}
     125
     126One should get:
     127{{{
     128Hello World, text = <Hello World!>, args = {'style': u'polite', 'silent': False, 'verbose': True}
     129Hello World, text = <Hello World!>, args = {}
     130Hello World, text = <Hello World!>, args = None
     131}}}
     132
     133Note 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`).
     134
     135You can also recursively use a wiki formatter to process the `content` as wiki markup:
     136
     137{{{#!python
     138from trac.wiki.formatter import format_to_html
     139from trac.wiki.macros import WikiMacroBase
     140
     141class HelloWorldMacro(WikiMacroBase):
     142    def expand_macro(self, formatter, name, content, args):
     143        content = "any '''wiki''' markup you want, even containing other macros"
     144        # Convert Wiki markup to HTML
     145        return format_to_html(self.env, formatter.context, content)
     146}}}