1 """
2 MoinMoin - if processor
3 A conditional processor
4
5 @copyright: Pascal Bauermeister <pascal DOT bauermeister AT gmail DOT com>
6 @license: GPL
7
8 Updates:
9
10 * [v0.1.1] 2005/12/14
11 Help added
12
13 * [v0.1.0] 2005/02/21
14 Initial version
15
16 ----
17
18 Usage:
19 {{{#!if EXPRESSION
20
21 Wiki text block 1
22
23 #elif EXPRESSION
24
25 Wiki text block 2
26
27 #else
28
29 Wiki text block 3
30
31 }}}
32
33 Where:
34 * EXPRESSION must be a valid Python expression which may contain:
35 user_name
36 string giving the current user name, or "" if not logged-in.
37
38 action
39 string giving the page's action.
40
41 is_member (group)
42 function returning 1 if the user is member of the given group, 0
43 otherwise.
44
45 When EXPRESSION is omitted, this help is displayed.
46
47 * #elif (or #else if, or #elseif) is optional
48
49 * #else is optional
50
51 ----
52
53 Sample 1: area restricted to logged-in members
54
55 {{{#!if user_name != ""
56 = Here are some info restricted to logged-in users =
57 ...
58
59 #else
60 = Public infos =
61 ...
62 }}}
63
64 ----
65
66 Sample 2: area restricted to members of a certain groups
67
68 {{{#!if is_member("BlueGroup") or is_member("YellowGroup")
69 = Green info =
70 ...
71 }}}
72
73
74 """
75
76
77 def _usage (full = False):
78
79 """Returns the interesting part of the module's doc"""
80
81 if full: return __doc__
82
83 lines = __doc__.replace ('\\n', '\\\\n'). splitlines ()
84 start = 0
85 end = len (lines)
86 for i in range (end):
87 if lines [i].strip ().lower () == "usage:":
88 start = i
89 break
90 for i in range (start, end):
91 if lines [i].startswith ('--'):
92 end = i
93 break
94 return '\n'.join (lines [start:end])
95
96
97
98
99
100 import inspect, re, StringIO
101 from MoinMoin import config, wikiutil, version
102
103
104 class _Error (Exception):
105 pass
106
107
108 def evaluate (request, expr):
109
110 def is_member (group):
111 if request.dicts.has_member (group, request.user.name):
112 return True
113 else: return False
114
115 user_name = request.user.name
116
117 action = ""
118 if request.form.has_key ('action'): action = request.form ['action'] [0]
119
120 try:
121 val = eval (expr,
122 {'__builtins__': []},
123 {'user_name': user_name,
124 'action': action,
125 'is_member': is_member,
126 }
127 )
128 except Exception: val = False
129 return val
130
131
132
133
134 def process (request, formatter, lines):
135
136 try: _process (request, formatter, lines)
137
138 except _Error, msg:
139 request.write (formatter.rawHTML ("""
140 <p><strong class="error">
141 Error: 'if' processor: %s</strong></p>""" % msg ))
142
143
144 def _process (request, formatter, lines):
145
146 blocks = []
147
148 command = block = None
149
150 if len (lines [0].strip()) <= 4:
151 raise _Error ("<pre>%s</pre>" % _usage ())
152
153
154 for l in lines:
155 ll = l.strip ()
156 if ll.startswith ("#") and not ll.startswith ("##"):
157 if command: blocks.append ( (command, block) )
158 command, block = ll [1:], []
159 else: block.append (l)
160 if command: blocks.append ( (command, block) )
161
162
163 for command, block in blocks:
164
165
166 try: pos = command.index (" ")
167 except ValueError: pos = -1
168
169 if pos == -1: cmd, cond = command.strip (), ""
170 else: cmd, cond = command [:pos].strip (), command [pos:].strip ()
171 cmd = cmd.strip ("!")
172
173 doit = False
174 if cmd == "if" or cmd == "elif" or cmd == "elsif" or cmd == "else if":
175 if evaluate (request, cond): doit = True
176 elif cmd == "else":
177 doit = True
178
179 if doit:
180 html = _format ("\n".join (block), request, formatter)
181 request.write (formatter.rawHTML (html))
182 return
183
184
185 def _format (wiki_text, request, formatter):
186
187 """ parse the text (in wiki source format) and make HTML,
188 after diverting sys.stdout to a string"""
189
190 from MoinMoin.parser import wiki
191 str_out = StringIO.StringIO ()
192 request.redirect (str_out)
193
194 wiki.Parser (wiki_text, request).format (formatter)
195 request.redirect ()
196 return str_out.getvalue ()
197
198
199