Edgewall Software

Version 1 (modified by anonymous, 17 years ago) ( diff )

keep subpages system to 0.11 to not have a url with trac*trac*trac

Beware: I'm by no means an expert and just quickly collected Information that seems right. The sources for these Macros are at: ticket:5274

Implementation Changes

Trac0.11 has significant changes for it's Macro processing and Implementation.

Here are 2 simple examples on how to create a Macro with Trac0.11 have a look at source:trunk/sample-plugins/Timestamp.py for an example that shows the difference between old style and new style macros and also source:trunk/wiki-macros/README which Provides a little more insight.

Macro without arguments

It should be saved as TimeStamp.py as Trac will use the module name as the Macro name

from trac.core import *
from trac.wiki.macros import WikiMacroBase
from StringIO import StringIO
import time

__all__ = ['TimestampMacro']

class TimestampMacro(WikiMacroBase):
        """
        Macro for inserting timestamp

        {{{
        [[Timestamp]]
        }}}
        """
        def expand_macro(self, formatter, name, args):
                buf = StringIO()
                t = time.localtime()
                buf = "<b>%s</b>" % time.strftime('%c', t)
                return buf

Macro with arguments

It should be saved as HelloWorld.py as Trac will use the module name as the Macro name

"""Example macro."""
from trac.core import *
from trac.wiki.macros import WikiMacroBase
from trac.util import escape

__all__ = ['HelloWorldMacro']

class HelloWorldMacro(WikiMacroBase):
        """
        Demo macro for a greeting with an argument.

        {{{
        [[HelloWorld(args)]]
        }}}

        """
        def expand_macro(self, formatter, name, args):
                # args will be `None` if the macro is called without parenthesis.
                txt = args or 'No arguments'

                # then, as `txt` comes from the user, it's important to guard against
                # the possibility to inject malicious HTML/Javascript, by using `escape()`:
                return 'Hello World, args = ' + escape(txt)
Note: See TracWiki for help on using the wiki.