Edgewall Software

Changes between Initial Version and Version 1 of 0.11/WikiMacros


Ignore:
Timestamp:
Apr 10, 2010, 7:32:41 PM (14 years ago)
Author:
Christian Boos
Comment:

archiving wiki:/WikiMacros@35

Legend:

Unmodified
Added
Removed
Modified
  • 0.11/WikiMacros

    v1 v1  
     1= Trac Macros =
     2
     3[[PageOutline]]
     4
     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.
     6
     7Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting).
     8
     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.
     11
     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.
     13
     14=== Example ===
     15
     16A list of 3 most recently changed wiki pages starting with 'Trac':
     17
     18{{{
     19 [[RecentChanges(Trac,3)]]
     20}}}
     21
     22Display:
     23 [[RecentChanges(Trac,3)]]
     24
     25== Available Macros ==
     26
     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].''
     28
     29{{{#!div class=important
     30
     31''' Note: ''' The following auto-generated documentation corresponds to macros for Trac 0.12dev. There were no major changes however since the previous version.
     32
     33For the original 0.11 version, please have a look at [/demo-0.11/wiki/WikiMacros].
     34}}}
     35
     36[[MacroList]]
     37
     38== Macros from around the world ==
     39
     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.
     41
     42== Developing Custom Macros ==
     43Macros, like Trac itself, are written in the [http://python.org/ Python programming language].
     44
     45For more information about developing macros, see the [trac:TracDev development resources] on the main project site.
     46
     47
     48== Implementation ==
     49
     50Here are 2 simple examples showing how to create a Macro with Trac 0.11.
     51
     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.
     53
     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.
     56{{{
     57#!python
     58from datetime import datetime
     59# Note: since Trac 0.11, datetime objects are used internally
     60
     61from genshi.builder import tag
     62
     63from trac.util.datefmt import format_datetime, utc
     64from trac.wiki.macros import WikiMacroBase
     65
     66class TimeStampMacro(WikiMacroBase):
     67    """Inserts the current time (in seconds) into the wiki page."""
     68
     69    revision = "$Rev$"
     70    url = "$URL$"
     71
     72    def expand_macro(self, formatter, name, args):
     73        t = datetime.now(utc)
     74        return tag.b(format_datetime(t, '%c'))
     75}}}
     76
     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.
     79{{{
     80#!python
     81from trac.wiki.macros import WikiMacroBase
     82
     83class HelloWorldMacro(WikiMacroBase):
     84    """Simple HelloWorld macro.
     85
     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
     89
     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    """
     94
     95    revision = "$Rev$"
     96    url = "$URL$"
     97
     98    def expand_macro(self, formatter, name, args):
     99        """Return some output that will be displayed in the Wiki content.
     100
     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)
     108   
     109    # Note that there's no need to HTML escape the returned data,
     110    # as the template engine (Genshi) will do it for us.
     111}}}
     112
     113
     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}}}.
     116
     117If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:
     118
     119{{{
     120#!python
     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())
     126}}}