Changes between Version 1 and Version 2 of WikiMacros


Ignore:
Timestamp:
Apr 7, 2008, 11:15:27 AM (9 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v1 v2  
    22Trac 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. 
    33 
    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. 
     4Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). 
    55 
    66== 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.  
     7Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses.  
    88 
    99=== Examples === 
     
    2121 [[HelloWorld(Testing)]] 
    2222 
     23== Available Macros == 
    2324 
    24 == Available Macros == 
    25 Macros are still a new feature, 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).  
     25''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].'' 
    2726 
    28  * '''!HelloWorld''' -- An example macro, useful for learning how to write macros. 
    29  * '''Timestamp''' -- Insert the current date and time. 
    30  
    31  
    32 ---- 
    33  
     27[[MacroList]] 
    3428 
    3529== 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. 
    3730 
    38   http://projects.edgewall.com/trac/wiki/MacroBazaar 
     31The [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. 
    3932 
    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. 
     33== Developing Custom Macros == 
     34Macros, 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. 
    4635 
    4736It's easiest to learn from an example: 
    4837{{{ 
     38#!python 
    4939# MyMacro.py -- The world's simplest macro 
    5040 
     
    5343}}} 
    5444 
    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. 
    57  
    58 Macros gain direct access to the main HDF tree, and are free to manipulate it.  
    59  
    60 Example: 
     45You can also use the environment (`env`) object, for example to access configuration data and the database, for example: 
    6146{{{ 
    62 def execute(hdf, args, env): 
    63     # Currently hdf is set only when the macro is called 
    64     # From a wiki page 
    65     if hdf: 
    66         hdf.setValue('wiki.macro.greeting', 'Hello World') 
    67          
    68     # args will be null if the macro is called without parentesis. 
    69     args = args or 'No arguments' 
    70     return 'Hello World, args = ' + args 
     47#!python 
     48def execute(hdf, txt, env): 
     49    return env.config.get('trac', 'repository_dir') 
    7150}}} 
    7251 
    73 You can also use the environment (env) object to access configuration data. 
     52Note 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. 
    7453 
    75 Example. 
    76 {{{ 
    77 def execute(hdf, txt, env): 
    78     return env.get_config('trac', 'repository_dir') 
    79 }}} 
     54For more information about developing macros, see the [http://projects.edgewall.com/trac/wiki/TracDev development resources] on the main project site. 
     55 
    8056---- 
    8157See also:  WikiProcessors, WikiFormatting, TracGuide