Edgewall Software

Changes between Initial Version and Version 1 of 0.11/WikiMacros

Apr 10, 2010, 7:32:41 PM (10 years ago)
Christian Boos

archiving wiki:/WikiMacros@35


  • 0.11/WikiMacros

    v1 v1  
     1= Trac Macros =
     5Trac 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.
     7Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting).
     9== Using Macros ==
     10Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses.
     12Trac 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.
     14=== Example ===
     16A list of 3 most recently changed wiki pages starting with 'Trac':
     19 [[RecentChanges(Trac,3)]]
     23 [[RecentChanges(Trac,3)]]
     25== Available Macros ==
     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].''
     29{{{#!div class=important
     31''' Note: ''' The following auto-generated documentation corresponds to macros for Trac 0.12dev. There were no major changes however since the previous version.
     33For the original 0.11 version, please have a look at [/demo-0.11/wiki/WikiMacros].
     38== Macros from around the world ==
     40The [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.
     42== Developing Custom Macros ==
     43Macros, like Trac itself, are written in the [http://python.org/ Python programming language].
     45For more information about developing macros, see the [trac:TracDev development resources] on the main project site.
     48== Implementation ==
     50Here are 2 simple examples showing how to create a Macro with Trac 0.11.
     52Also, have a look at [trac:source:tags/trac-0.11/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 a little more insight about the transition.
     54=== Macro without arguments ===
     55It should be saved as `TimeStamp.py` (in the TracEnvironment's `plugins/` directory) as Trac will use the module name as the Macro name.
     58from datetime import datetime
     59# Note: since Trac 0.11, datetime objects are used internally
     61from genshi.builder import tag
     63from trac.util.datefmt import format_datetime, utc
     64from trac.wiki.macros import WikiMacroBase
     66class TimeStampMacro(WikiMacroBase):
     67    """Inserts the current time (in seconds) into the wiki page."""
     69    revision = "$Rev$"
     70    url = "$URL$"
     72    def expand_macro(self, formatter, name, args):
     73        t = datetime.now(utc)
     74        return tag.b(format_datetime(t, '%c'))
     77=== Macro with arguments ===
     78It should be saved as `HelloWorld.py` (in the TracEnvironment's `plugins/` directory) as Trac will use the module name as the Macro name.
     81from trac.wiki.macros import WikiMacroBase
     83class HelloWorldMacro(WikiMacroBase):
     84    """Simple HelloWorld macro.
     86    Note that the name of the class is meaningful:
     87     - it must end with "Macro"
     88     - what comes before "Macro" ends up being the macro name
     90    The documentation of the class (i.e. what you're reading)
     91    will become the documentation of the macro, as shown by
     92    the !MacroList macro (usually used in the WikiMacros page).
     93    """
     95    revision = "$Rev$"
     96    url = "$URL$"
     98    def expand_macro(self, formatter, name, args):
     99        """Return some output that will be displayed in the Wiki content.
     101        `name` is the actual name of the macro (no surprise, here it'll be
     102        `'HelloWorld'`),
     103        `args` is the text enclosed in parenthesis at the call of the macro.
     104          Note that if there are ''no'' parenthesis (like in, e.g.
     105          [[HelloWorld]]), then `args` is `None`.
     106        """
     107        return 'Hello World, args = ' + unicode(args)
     109    # Note that there's no need to HTML escape the returned data,
     110    # as the template engine (Genshi) will do it for us.
     114=== {{{expand_macro}}} details ===
     115{{{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}}}.
     117If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:
     121  text = "whatever wiki markup you want, even containing other macros"
     122  # Convert Wiki markup to HTML, new style
     123  out = StringIO()
     124  Formatter(self.env, formatter.context).format(text, out)
     125  return Markup(out.getvalue())