Edgewall Software

Changes between Version 7 and Version 8 of 0.11/TracMacros


Ignore:
Timestamp:
Aug 5, 2007, 2:41:23 PM (17 years ago)
Author:
ThurnerRupert
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • 0.11/TracMacros

    v7 v8  
    3232The [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.
    3333
     34== Implementation ==
     35
     36Here are 2 simple examples on how to create a Macro with [wiki:0.11 Trac 0.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.
     37
     38=== Macro without arguments ===
     39It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name
     40{{{
     41from trac.core import *
     42from trac.wiki.macros import WikiMacroBase
     43from StringIO import StringIO
     44import time
     45
     46__all__ = ['TimestampMacro']
     47
     48class TimestampMacro(WikiMacroBase):
     49        """
     50        Macro for inserting timestamp
     51
     52        {{{
     53        [[Timestamp]]
     54        }}}
     55        """
     56        def expand_macro(self, formatter, name, args):
     57                buf = StringIO()
     58                t = time.localtime()
     59                buf = "<b>%s</b>" % time.strftime('%c', t)
     60                return buf
     61}}}
     62
     63=== Macro with arguments ===
     64It should be saved as `HelloWorld.py` as Trac will use the module name as the Macro name
     65{{{
     66"""Example macro."""
     67from trac.core import *
     68from trac.wiki.macros import WikiMacroBase
     69from trac.util import escape
     70
     71__all__ = ['HelloWorldMacro']
     72
     73class HelloWorldMacro(WikiMacroBase):
     74        """
     75        Demo macro for a greeting with an argument.
     76
     77        {{{
     78        [[HelloWorld(args)]]
     79        }}}
     80
     81        """
     82        def expand_macro(self, formatter, name, args):
     83                # args will be `None` if the macro is called without parenthesis.
     84                txt = args or 'No arguments'
     85
     86                # then, as `txt` comes from the user, it's important to guard against
     87                # the possibility to inject malicious HTML/Javascript, by using `escape()`:
     88                return 'Hello World, args = ' + escape(txt)
     89}}}
     90
     91