Back to index

moin  1.9.0~rc2
RandomQuote.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - RandomQuote Macro
00004 
00005     Selects a random quote from FortuneCookies or a given page.
00006 
00007     Usage:
00008         <<RandomQuote()>>
00009         <<RandomQuote(WikiTips)>>
00010 
00011     Comments:
00012         It will look for list delimiters on the page in question.
00013         It will ignore anything that is not in an "*" list.
00014 
00015     @copyright: 2002-2004 Juergen Hermann <jh@web.de>
00016     @license: GNU GPL, see COPYING for details.
00017 
00018     Originally written by Thomas Waldmann.
00019     Gustavo Niemeyer added wiki markup parsing of the quotes.
00020 """
00021 
00022 import random
00023 
00024 from MoinMoin.Page import Page
00025 
00026 Dependencies = ["time"]
00027 
00028 def macro_RandomQuote(macro, pagename=u'FortuneCookies'):
00029     _ = macro.request.getText
00030 
00031     if macro.request.user.may.read(pagename):
00032         page = Page(macro.request, pagename)
00033         raw = page.get_raw_body()
00034     else:
00035         raw = ""
00036 
00037     # this selects lines looking like a list item
00038     # !!! TODO: make multi-line quotes possible (optionally split by "----" or something)
00039     quotes = raw.splitlines()
00040     quotes = [quote.strip() for quote in quotes]
00041     quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]
00042 
00043     if not quotes:
00044         return (macro.formatter.highlight(1) +
00045                 _('No quotes on %(pagename)s.') % {'pagename': pagename} +
00046                 macro.formatter.highlight(0))
00047 
00048     quote = random.choice(quotes)
00049     page.set_raw_body(quote, 1)
00050     quote = macro.request.redirectedOutput(page.send_page,
00051         content_only=1, content_id="RandomQuote")
00052 
00053     return quote
00054