Changes between Version 1 and Version 2 of WikiMacros


Ignore:
Timestamp:
Jul 8, 2009, 6:50:50 PM (15 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v1 v2  
    1 =  Wiki Macros =
     1= Trac Macros =
     2
     3[[PageOutline]]
     4
    25Trac 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.
    36
     
    710Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses.
    811
    9 === Examples ===
     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':
    1017
    1118{{{
    12  [[Timestamp]]
     19 [[RecentChanges(Trac,3)]]
    1320}}}
     21
    1422Display:
    15  [[Timestamp]]
    16 
    17 {{{
    18  [[HelloWorld(Testing)]]
    19 }}}
    20 Display:
    21  [[HelloWorld(Testing)]]
     23 [[RecentChanges(Trac,3)]]
    2224
    2325== Available Macros ==
     
    3234
    3335== Developing Custom Macros ==
    34 Macros, like Trac itself, are written in the [http://www.python.org/ Python programming language]. They are very simple modules, identified by the filename and should contain a single `execute()` function. Trac will display the returned data inserted into the HTML representation of the Wiki page where the macro is called.
     36Macros, like Trac itself, are written in the [http://python.org/ Python programming language].
    3537
    36 It's easiest to learn from an example:
     38For more information about developing macros, see the [trac:TracDev development resources] on the main project site.
     39
     40
     41== Implementation ==
     42
     43Here are 2 simple examples showing how to create a Macro with Trac 0.11.
     44
     45Also, 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.
     46
     47=== Macro without arguments ===
     48It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name
    3749{{{
    3850#!python
    39 # MyMacro.py -- The world's simplest macro
     51from datetime import datetime
     52# Note: since Trac 0.11, datetime objects are used internally
    4053
    41 def execute(hdf, args, env):
    42     return "Hello World called with args: %s" % args
     54from genshi.builder import tag
     55
     56from trac.util.datefmt import format_datetime, utc
     57from trac.wiki.macros import WikiMacroBase
     58
     59class TimeStampMacro(WikiMacroBase):
     60    """Inserts the current time (in seconds) into the wiki page."""
     61
     62    revision = "$Rev$"
     63    url = "$URL$"
     64
     65    def expand_macro(self, formatter, name, args):
     66        t = datetime.now(utc)
     67        return tag.b(format_datetime(t, '%c'))
    4368}}}
    4469
    45 You can also use the environment (`env`) object, for example to access configuration data and the database, for example:
     70=== Macro with arguments ===
     71It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name
    4672{{{
    4773#!python
    48 def execute(hdf, txt, env):
    49     return env.config.get('trac', 'repository_dir')
     74from trac.wiki.macros import WikiMacroBase
     75
     76class HelloWorldMacro(WikiMacroBase):
     77    """Simple HelloWorld macro.
     78
     79    Note that the name of the class is meaningful:
     80     - it must end with "Macro"
     81     - what comes before "Macro" ends up being the macro name
     82
     83    The documentation of the class (i.e. what you're reading)
     84    will become the documentation of the macro, as shown by
     85    the !MacroList macro (usually used in the WikiMacros page).
     86    """
     87
     88    revision = "$Rev$"
     89    url = "$URL$"
     90
     91    def expand_macro(self, formatter, name, args):
     92        """Return some output that will be displayed in the Wiki content.
     93
     94        `name` is the actual name of the macro (no surprise, here it'll be
     95        `'HelloWorld'`),
     96        `args` is the text enclosed in parenthesis at the call of the macro.
     97          Note that if there are ''no'' parenthesis (like in, e.g.
     98          [[HelloWorld]]), then `args` is `None`.
     99        """
     100        return 'Hello World, args = ' + unicode(args)
     101   
     102    # Note that there's no need to HTML escape the returned data,
     103    # as the template engine (Genshi) will do it for us.
    50104}}}
    51105
    52 Note that since version 0.9, wiki macros can also be written as TracPlugins. This gives them some capabilities that “classic” macros do not have, such as being able to directly access the HTTP request.
    53106
    54 For more information about developing macros, see the [http://projects.edgewall.com/trac/wiki/TracDev development resources] on the main project site.
     107=== {{{expand_macro}}} details ===
     108{{{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}}}.
    55109
    56 ----
    57 See also:  WikiProcessors, WikiFormatting, TracGuide
     110If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:
     111
     112{{{
     113#!python
     114  text = "whatever wiki markup you want, even containing other macros"
     115  # Convert Wiki markup to HTML, new style
     116  out = StringIO()
     117  Formatter(self.env, formatter.context).format(text, out)
     118  return Markup(out.getvalue())
     119}}}

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png