Changes between Version 1 and Version 2 of Wiki Macros


Ignore:
Timestamp:
Jul 4, 2008, 2:09:43 PM (16 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Wiki Macros

    v1 v2  
    1 =  Wiki Macros =
    2 Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. They allow insertion of custom dynamic HTML data in any module supporting WikiFormatting.
     1= Trac Macros =
    32
    4 Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). See also: WikiProcessors.
     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).
    58
    69== Using Macros ==
    7 Macro calls are enclosed in two ''square brackets''. Like python functions, macros can also have arguments, a comma separated list within parenthesis.
     10Macro 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]]
     23 [[RecentChanges(Trac,3)]]
    1624
    17 {{{
    18  [[HelloWorld(Testing)]]
    19 }}}
    20 Display:
    21  [[HelloWorld(Testing)]]
     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[[MacroList]]
     30
     31== Macros from around the world ==
     32
     33The [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.
     34
     35== Developing Custom Macros ==
     36Macros, like Trac itself, are written in the [http://python.org/ Python programming language].
     37
     38For more information about developing macros, see the [wiki:TracDev development resources] on the main project site.
    2239
    2340
    24 == Available Macros ==
    25 As of 0.6, macros are still a new feature in Trac, and the list of available (and distributed) macros is
    26 admittedly not very impressive. In future Trac releases, we hope to build a library of useful macros, and will of course happily include contributed macros (see below).
     41== Implementation ==
    2742
    28  * '''!HelloWorld''' -- An example macro, useful for learning how to write macros.
    29  * '''Timestamp''' -- Insert the current date and time.
     43Here 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 about the transition.
    3044
     45=== Macro without arguments ===
     46It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name
     47{{{
     48#!python
     49from datetime import datetime
     50# Note: since Trac 0.11, datetime objects are used internally
    3151
    32 ----
     52from genshi.builder import tag
    3353
     54from trac.util.datefmt import format_datetime, utc
     55from trac.wiki.macros import WikiMacroBase
    3456
    35 == Macros from around the world ==
    36 The [http://projects.edgewall.com/trac/ Trac Project] has a section dedicated to user-contributed macros, [http://projects.edgewall.com/trac/wiki/MacroBazaar MacroBazaar]. If you're looking for new macros, or have written new ones to share with the world, don't hesitate adding it to the [http://projects.edgewall.com/trac/wiki/MacroBazaar MacroBazaar] wiki page.
     57class TimestampMacro(WikiMacroBase):
     58    """Inserts the current time (in seconds) into the wiki page."""
    3759
    38   http://projects.edgewall.com/trac/wiki/MacroBazaar
     60    revision = "$Rev$"
     61    url = "$URL$"
    3962
    40 
    41 ----
    42 
    43 
    44 == Developing New Macros ==
    45 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 ''entry point'' function. Trac will display the returned data inserted into the HTML where the macro was called.
    46 
    47 It's easiest to learn from an example:
    48 {{{
    49 # MyMacro.py -- The world's simplest macro
    50 
    51 def execute(hdf, args):
    52     return "Hello World called with args: %s" % args
     63    def expand_macro(self, formatter, name, args):
     64        t = datetime.now(utc)
     65        return tag.b(format_datetime(t, '%c'))
    5366}}}
    5467
    55 === Advanced Topics: Template-enabled Macros ===
    56 For advanced uses, macros can also render structured output in HDF, to be rendered to HTML using clearsilver templates - like most Trac output. In short, this allows more generic and well-designed advanced macros.
     68=== Macro with arguments ===
     69It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name
     70{{{
     71#!python
     72from trac.wiki.macros import WikiMacroBase
    5773
    58 Macros gain direct access to the main HDF tree, and are free to manipulate it.
     74class HelloWorldMacro(WikiMacroBase):
     75    """Simple HelloWorld macro.
    5976
    60 Example:
     77    Note that the name of the class is meaningful:
     78     - it must end with "Macro"
     79     - what comes before "Macro" ends up being the macro name
    6180
    62 {{{
    63 def execute(hdf, args):
    64     # Currently hdf is set only when the macro is called
    65     # From a wiki page
    66     if hdf:
    67         hdf.setValue('wiki.macro.greeting', 'Hello World')
    68        
    69     # args will be null if the macro is called without parentesis.
    70     args = args or 'No arguments'
    71     return 'Hello World, args = ' + args
     81    The documentation of the class (i.e. what you're reading)
     82    will become the documentation of the macro, as shown by
     83    the !MacroList macro (usually used in the WikiMacros page).
     84    """
     85
     86    revision = "$Rev$"
     87    url = "$URL$"
     88
     89    def expand_macro(self, formatter, name, args):
     90        """Return some output that will be displayed in the Wiki content.
     91
     92        `name` is the actual name of the macro (no surprise, here it'll be
     93        `'HelloWorld'`),
     94        `args` is the text enclosed in parenthesis at the call of the macro.
     95          Note that if there are ''no'' parenthesis (like in, e.g.
     96          [[HelloWorld]]), then `args` is `None`.
     97        """
     98        return 'Hello World, args = ' + unicode(args)
     99   
     100    # Note that there's no need to HTML escape the returned data,
     101    # as the template engine (Genshi) will do it for us.
    72102}}}
    73103
    74104
     105=== {{{expand_macro}}} details ===
     106{{{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}}}.
    75107
    76 ----
    77 See also:  WikiProcessors, WikiFormatting, TracGuide
     108If your macro creates wiki markup instead of HTML, you can convert it to HTML like this:
     109
     110{{{
     111#!python
     112  text = "whatever wiki markup you want, even containing other macros"
     113  # Convert Wiki markup to HTML, new style
     114  out = StringIO()
     115  Formatter(self.env, formatter.context).format(text, out)
     116  return Markup(out.getvalue())
     117}}}