Edgewall Software

Changes between Version 54 and Version 55 of WikiMacros


Ignore:
Timestamp:
Jul 8, 2018, 12:12:54 AM (6 years ago)
Author:
Ryan J Ollos
Comment:

Minor changes.

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v54 v55  
    44[[TranslatedPages]]
    55
    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 data in 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.
    77
    88The macro syntax is `[[macro-name(optional-arguments)]]`.
    99
    10 '''WikiProcessors''' are another kind of macros. 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`:
    1111
    1212{{{
     
    1818== Using Macros
    1919
    20 Macro calls are enclosed in double-square brackets `[[..]]`. Like Python functions, macros can have arguments, which is then a comma separated list within parentheses `[[..(,)]]`.
     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 `[[..(,)]]`.
    2121
    2222=== Getting Detailed Help
    2323
    24 The list of available macros and the full help can be obtained using the !MacroList macro, as seen [#AvailableMacros below].
     24The list of available macros and the full help can be obtained using the !MacroList macro, see [#AvailableMacros below].
    2525
    2626A brief list can be obtained via `[[MacroList(*)]]` or `[[?]]`.
    2727
    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?]]`.
     28Detailed 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?]]`.
    2929
    3030=== Example
     
    7272== Available Macros
    7373
     74{{{#!box note
     75The site includes several macros provided by plugins. The [/demo-1.2/wiki/WikiMacros demo site] shows only the macros provided by Trac.
     76}}}
     77
    7478[[MacroList]]
    7579
    7680== Macros from around the world
    7781
    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.
     82The [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.
    7983
    8084== Developing Custom Macros
    8185
    82 Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are developed as part of TracPlugins.
     86Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are a type of [TracPlugins plugin].
    8387
    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.
     88Here are 2 simple examples showing how to create a Macro. For more information about developing macros, see the [trac:TracDev development resources].
    8789
    8890=== Macro without arguments
    8991
    90 To test the following code, save it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory.
     92To test the following code, copy it to `timestamp_sample.py` in the TracEnvironment's `plugins/` directory.
    9193
    9294{{{#!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
     95from trac.util.datefmt import datetime_now, format_datetime, utc
    9796from trac.util.html import tag
    9897from trac.wiki.macros import WikiMacroBase
    9998
    100 class TimeStampMacro(WikiMacroBase):
    101     """Inserts the current time (in seconds) into the wiki page."""
    10299
    103     revision = "$Rev$"
    104     url = "$URL$"
     100class TimestampMacro(WikiMacroBase):
     101    _description = "Inserts the current time (in seconds) into the wiki page."
    105102
    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)
    108105        return tag.strong(format_datetime(t, '%c'))
    109106}}}
     
    111108=== Macro with arguments
    112109
    113 To test the following code, save it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory.
     110To test the following code, copy it to `helloworld_sample.py` in the TracEnvironment's `plugins/` directory.
    114111
    115112{{{#!python
    116 from trac.util.html import Markup
     113from trac.util.translation import cleandoc_
    117114from trac.wiki.macros import WikiMacroBase
    118115
     116
    119117class HelloWorldMacro(WikiMacroBase):
     118    _description = cleandoc_(
    120119    """Simple HelloWorld macro.
    121120
     
    127126    will become the documentation of the macro, as shown by
    128127    the !MacroList macro (usually used in the WikiMacros page).
    129     """
     128    """)
    130129
    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):
    135131        """Return some output that will be displayed in the Wiki content.
    136132
    137133        `name` is the actual name of the macro (no surprise, here it'll be
    138134        `'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.
    144141        """
    145         return 'Hello World, text = %s, args = %s' % \
    146             (Markup.escape(text), Markup.escape(repr(args)))
    147 
     142        return 'Hello World, content = ' + unicode(content)
    148143}}}
    149144
    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'').
     145Note 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`.
    151146
    152147For example, when writing:
     
    170165}}}
    171166
    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`).
     167Note 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`).
    173168
    174169You can also recursively use a wiki formatter to process the `content` as wiki markup: