Keith, the code needs more commenting, parts of it are totally obscure to me, especially the phrases thing (and note that globals are forbidden starting with v1.0). And t.rstrip is certainly redundant or a bug. -- JürgenHermann 2002-05-10 10:00:29
1 '''
2 MoinMoin - WikiTips Macro
3
4 Copyright (c) 2001 by Keith Naas <knaas@netjets.com>
5 Copyright (c) 2000 by Richard Jones <richard@bizarsoftware.com.au>
6 Copyright (c) 2000 by Jürgen Hermann <jh@web.de>
7 All rights reserved, see COPYING for details.
8
9 Derived from:
10 Flounder's Mission Statement Generator
11 Written 4/26/2000
12 by Scott Fitzenrider
13 flounder@bright.net
14
15 I believe the original is at:
16 http://umweb1.unitedmedia.com/comics/dilbert/duh/bin/ms_gen/ms2.cgi
17
18 Usage:
19 [[WikiTips(TipPage)]]
20
21 Examples:
22 [[WikiTips(WikiTips)]] -- generates a random wikitip from WikiTips
23
24 Comments:
25 It will look for list delimiters on the page in question. It will ignore
26 anything that is not in an "*" list.
27 Needs to be updated to new request object. I'm sure this is easy.
28 Can change the name of this to RandomQuote, etc.
29 Also should probably figure out how to format the selected item instead of just displaying it.
30
31 $Id: $
32 '''
33
34 import random, string, copy,re
35 from MoinMoin.Page import Page
36
37 _phrases = {
38 'tips': ['No Tips.'],
39 }
40
41 def select(type):
42 item = random.choice(phrases[type])
43 phrases[type].remove(item)
44 return item
45
46 def execute(macro, text):
47 try:
48 pagename = text
49 except:
50 return '<b> error in : WikiTips Macro : cannot get arguments </b>'
51
52 global phrases
53 raw = Page(pagename).get_raw_body()
54 raw = raw[raw.find(' * '):]
55 tips = raw.split(' * ')
56 for t in tips:
57 t.rstrip
58 if len(t) <= 0:
59 tips.remove(t)
60
61 pagelink = """<font size=-1>%s</font>""" % (macro.formatter.pagelink(pagename),)
62 if len(tips) > 0:
63 phrases = { pagename: tips}
64 statement = '%s\n%s<font class="small">%s</font>' % (pagelink, macro.formatter.linebreak(0), select(pagename))
65 else:
66 statement = '%s\n%s<font class="small">No tips.</font>' % (pagelink, macro.formatter.linebreak(0), )
67
68 return statement
69
70 if __name__ == '__main__':
71 print execute(None, None)