comparison MoinMoin/macro/RandomQuote.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children 0bbc39b09f95
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - RandomQuote Macro
4
5 Selects a random quote from FortuneCookies or a given page.
6
7 Usage:
8 [[RandomQuote()]]
9 [[RandomQuote(WikiTips)]]
10
11 Comments:
12 It will look for list delimiters on the page in question.
13 It will ignore anything that is not in an "*" list.
14
15 @copyright: 2002-2004 by Jürgen Hermann <jh@web.de>
16 @license: GNU GPL, see COPYING for details.
17
18 Originally written by Thomas Waldmann.
19 Gustavo Niemeyer added wiki markup parsing of the quotes.
20 """
21
22 import random, StringIO
23 from MoinMoin.Page import Page, wikiutil
24
25 Dependencies = ["time"]
26
27 def execute(macro, args):
28 _ = macro.request.getText
29
30 pagename = args or 'FortuneCookies'
31 if macro.request.user.may.read(pagename):
32 page = Page(macro.request, pagename)
33 raw = page.get_raw_body()
34 else:
35 raw = ""
36
37 # this selects lines looking like a list item
38 # !!! TODO: make multi-line quotes possible (optionally split by "----" or something)
39 quotes = raw.splitlines()
40 quotes = [quote.strip() for quote in quotes]
41 quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]
42
43 if not quotes:
44 return (macro.formatter.highlight(1) +
45 _('No quotes on %(pagename)s.') % {'pagename': pagename} +
46 macro.formatter.highlight(0))
47
48 quote = random.choice(quotes)
49 page.set_raw_body(quote, 1)
50 out = StringIO.StringIO()
51 macro.request.redirect(out)
52 page.send_page(macro.request, content_only=1, content_id="RandomQuote_%s" % wikiutil.quoteWikinameFS(page.page_name) )
53 quote = out.getvalue()
54 macro.request.redirect()
55
56 return quote
57