Ed Price

Email: <edp AT SPAMFREE panix DOT com>

I started using MoinMoin recently at work and at home. I like it.

RandomQuote regex support

I had an idea to rewrite FortuneCookies as [[Include(^.*Quotes)]].

But that breaks RandomQuote due to gory details of processing order etc.

Since I figured "fixing" that would be difficult, Plan B was to support [[RandomQuote(^.*Quotes)]].

The following modified RandomQuote.py seems to achieve this:

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - RandomQuote macro
   4 
   5     Selects a random quote from FortuneCookies or the given page,
   6     or pages matching the given regex.
   7 
   8     Usage:
   9         [[RandomQuote()]]
  10         [[RandomQuote(WikiTips)]]
  11         [[RandomQuote(^.*Quotes)]]
  12 
  13     Comments:
  14         It will look for list delimiters on the pages in question.
  15         It will ignore anything that is not in an "*" list.
  16 
  17     @copyright: 2002-2004 by Jürgen Hermann <jh@web.de>
  18     @license: GNU GPL, see COPYING for details.
  19 
  20     Originally written by Thomas Waldmann.
  21     Gustavo Niemeyer added wiki markup parsing of the quotes.
  22     Ed Price added regex support.
  23 """
  24 
  25 import random, cStringIO, re
  26 from MoinMoin.Page import Page, wikiutil, config
  27 
  28 Dependencies = ["time"]
  29 
  30 def execute(macro, args):
  31     _ = macro.request.getText
  32 
  33     pagename = args or 'FortuneCookies'
  34 
  35     # BEGIN regex support
  36     pagelist = [pagename]
  37 
  38     if pagename.startswith("^"):
  39         try:
  40             page_match = re.compile(pagename)
  41         except re.error, e:
  42             return (macro.formatter.highlight(1) +
  43                     _('Error matching %(pagename)s: %(error)s.') % {'pagename': pagename, 'error': e} +
  44                     macro.formatter.highlight(0))
  45         else:
  46             pagelist = wikiutil.getPageList(config.text_dir)
  47             pagelist = filter(page_match.match, pagelist)
  48 
  49     if not pagelist:
  50         return (macro.formatter.highlight(1) +
  51                 _('No pages matched %(pagename)s.') % {'pagename': pagename} +
  52                 macro.formatter.highlight(0))
  53 
  54     # for avoiding loops...
  55     this_page = macro.formatter.page;
  56     if not hasattr(this_page, '_macroRandomQuote_pagelist'):
  57         this_page._macroRandomQuote_pagelist = {}
  58 
  59     allquotes = []
  60     for pagename in pagelist:
  61         if not macro.request.user.may.read(pagename):
  62             continue
  63         if this_page._macroRandomQuote_pagelist.has_key(pagename):
  64             continue # avoid loops
  65         page = Page(pagename)
  66         raw = page.get_raw_body()
  67         # this selects lines looking like a list item
  68         # !!! TODO: make multi-line quotes possible (optionally split by "----" or something)
  69         quotes = raw.splitlines()
  70         quotes = [quote.strip() for quote in quotes]
  71         quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]
  72         allquotes = allquotes + quotes
  73 
  74     quotes = allquotes
  75     # END regex support
  76 
  77     if not quotes:
  78         return (macro.formatter.highlight(1) +
  79                 _('No quotes on %(pagename)s.') % {'pagename': pagename} +
  80                 macro.formatter.highlight(0))
  81 
  82     quote = random.choice(quotes)
  83     page.set_raw_body(quote, 1)
  84     out = cStringIO.StringIO()
  85     macro.request.redirect(out)
  86     page.send_page(macro.request, content_only=1, content_id="RandomQuote_%s" % wikiutil.quoteWikiname(page.page_name) )
  87     quote = out.getvalue()
  88     macro.request.redirect()
  89 
  90     return quote


CategoryHomepage

MoinMoin: EdPrice (last edited 2007-10-29 19:19:28 by localhost)