| 1 |
#!/usr/local/bin/python |
|---|
| 2 |
# |
|---|
| 3 |
#Copyright (c) 2008, Philippe Monnaie <philippe.monnaie@gmail.com> |
|---|
| 4 |
# |
|---|
| 5 |
#Permission to use, copy, modify, and/or distribute this software for any |
|---|
| 6 |
#purpose with or without fee is hereby granted, provided that the above |
|---|
| 7 |
#copyright notice and this permission notice appear in all copies. |
|---|
| 8 |
# |
|---|
| 9 |
#THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|---|
| 10 |
#WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|---|
| 11 |
#MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|---|
| 12 |
#ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|---|
| 13 |
#WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|---|
| 14 |
#ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|---|
| 15 |
#OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 16 |
|
|---|
| 17 |
# edited by deliriumlabs for working in trac 0.11 |
|---|
| 18 |
#http://blog.deliriumlabs.net |
|---|
| 19 |
from trac.wiki.macros import WikiMacroBase |
|---|
| 20 |
from trac.util import TracError |
|---|
| 21 |
|
|---|
| 22 |
class PageVariable(WikiMacroBase): |
|---|
| 23 |
""" |
|---|
| 24 |
PageVariable macro. |
|---|
| 25 |
This macro sets or gets a variable that is accessible throughout the entire page. |
|---|
| 26 |
|
|---|
| 27 |
Use: [[PageVariable(name,value)]] |
|---|
| 28 |
Name signifies the name of the parameter. |
|---|
| 29 |
Value signifies the value the parameter should be set to. |
|---|
| 30 |
If a value is supplied, this macro returns an empty string. When trying to set a variable that is already set (or present in the url for some other reason), an error is raised. |
|---|
| 31 |
If the parameter value is ommitted, the currently known value is returned. If no value is known, an error is raised. |
|---|
| 32 |
""" |
|---|
| 33 |
|
|---|
| 34 |
revision = "$Rev$" |
|---|
| 35 |
url = "$URL$" |
|---|
| 36 |
#define an array for variables |
|---|
| 37 |
vars={} |
|---|
| 38 |
def expand_macro(self, formatter, name, args): |
|---|
| 39 |
self.req = formatter.req |
|---|
| 40 |
#split the args by commaso it becomes an array |
|---|
| 41 |
args=args.split(',') |
|---|
| 42 |
if len(args) == 1: |
|---|
| 43 |
return self.vars[args[0]] |
|---|
| 44 |
else: |
|---|
| 45 |
if len(args) == 2: |
|---|
| 46 |
self.vars[args[0]]=args[1] |
|---|
| 47 |
else: |
|---|
| 48 |
return 'ERROR: Invalid number of arguments supplied to PageVariable macro: ' + str(args) |
|---|