| 1 |
"""Example macro.""" |
|---|
| 2 |
|
|---|
| 3 |
revision = "$Rev: 2 $" |
|---|
| 4 |
url = "$URL: /mirror/sample-plugins/HelloWorld.py $" |
|---|
| 5 |
|
|---|
| 6 |
# |
|---|
| 7 |
# The following shows the code for macro, old-style. |
|---|
| 8 |
# |
|---|
| 9 |
# The `execute` function serves no purpose other than to illustrate |
|---|
| 10 |
# the example, it will not be used anymore. |
|---|
| 11 |
# |
|---|
| 12 |
# ---- (ignore in your own macro) ---- |
|---|
| 13 |
# -- |
|---|
| 14 |
from trac.util import escape |
|---|
| 15 |
|
|---|
| 16 |
def execute(hdf, txt, env): |
|---|
| 17 |
# Currently hdf is set only when the macro is called |
|---|
| 18 |
# From a wiki page |
|---|
| 19 |
if hdf: |
|---|
| 20 |
hdf['wiki.macro.greeting'] = 'Hello World' |
|---|
| 21 |
|
|---|
| 22 |
# args will be `None` if the macro is called without parenthesis. |
|---|
| 23 |
args = txt or 'No arguments' |
|---|
| 24 |
|
|---|
| 25 |
# then, as `txt` comes from the user, it's important to guard against |
|---|
| 26 |
# the possibility to inject malicious HTML/Javascript, by using `escape()`: |
|---|
| 27 |
return 'Hello World, args = ' + escape(args) |
|---|
| 28 |
# -- |
|---|
| 29 |
# ---- (ignore in your own macro) ---- |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
# |
|---|
| 33 |
# The following is the converted new-style macro |
|---|
| 34 |
# |
|---|
| 35 |
# ---- (reuse for your own macro) ---- |
|---|
| 36 |
# -- |
|---|
| 37 |
from trac.wiki.macros import WikiMacroBase |
|---|
| 38 |
|
|---|
| 39 |
class HelloWorldMacro(WikiMacroBase): |
|---|
| 40 |
"""Simple HelloWorld macro. |
|---|
| 41 |
|
|---|
| 42 |
Note that the name of the class is meaningful: |
|---|
| 43 |
- it must end with "Macro" |
|---|
| 44 |
- what comes before "Macro" ends up being the macro name |
|---|
| 45 |
|
|---|
| 46 |
The documentation of the class (i.e. what you're reading) |
|---|
| 47 |
will become the documentation of the macro, as shown by |
|---|
| 48 |
the !MacroList macro (usually used in the TracWikiMacros page). |
|---|
| 49 |
""" |
|---|
| 50 |
|
|---|
| 51 |
def expand_macro(self, formatter, name, args): |
|---|
| 52 |
"""Return some output that will be displayed in the Wiki content. |
|---|
| 53 |
|
|---|
| 54 |
`name` is the actual name of the macro (no surprise, here it'll be |
|---|
| 55 |
`'HelloWorld'`), |
|---|
| 56 |
`args` is the text enclosed in parenthesis at the call of the macro. |
|---|
| 57 |
Note that if there are ''no'' parenthesis (like in, e.g. |
|---|
| 58 |
[[HelloWorld]]), then `args` is `None`. |
|---|
| 59 |
""" |
|---|
| 60 |
return 'Hello World, args = ' + unicode(args) |
|---|
| 61 |
|
|---|
| 62 |
# Note that there's no need to HTML escape the returned data, |
|---|
| 63 |
# as the template engine (Genshi) will do it for us. |
|---|
| 64 |
# -- |
|---|
| 65 |
# ---- (reuse for your own macro) ---- |
|---|