Attachment 'RandomPageContaining.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - RandomPageContaining Macro
   4     modified by Jonathan C .Dietrich
   5     @copyright: 2000 by Jürgen Hermann <jh@web.de>
   6     @license: GNU GPL, see COPYING for details.
   7 
   8         [[RandomPageContaining(pattern,num,ignoreTitlePattern)]]
   9                 pattern: a regex. pages will only be considered their contents match this
  10                 num: a number. this is the number of pages to return
  11                 ignoreTitlePattern: a regex. pages will not be included if their TITLE  matches this
  12                 
  13                 
  14                 [[RandomPageContaining(,3,.*[oO])]]  3 random pages that do not include the letter o
  15                 [[RandomPageContaining(MyCategory,1,.*MoinEditorBackup|.*Template)]] find 1 random page in MyCategory but do not inlcude templates or editor backups
  16 
  17 """
  18 
  19 # Imports
  20 import whrandom, re
  21 from MoinMoin import config, wikiutil
  22 
  23 Dependencies = ["time"]
  24 _arg_links = r',\s*(?P<links>\d*)'
  25 _arg_ignore = r',\s*(?P<ignore>[^,]+)'
  26 _args_re_pattern = r'^(?P<pattern>[^,]+)?((%s)?(%s)?)?$' % (_arg_links,_arg_ignore)
  27 
  28 def execute(macro, text, args_re=re.compile(_args_re_pattern)):
  29 
  30     ret = ''
  31   #  all_pages = wikiutil.getPageList(config.text_dir)
  32 
  33 #   parse and check arguments
  34     args = args_re.match(text)
  35     if not args:
  36         return ('<p><strong class="error">%s</strong></p>' %
  37             _('Invalid include arguments "%s"!')) % (text,)
  38 
  39     if args.group('pattern') is not None:
  40         filter_regex = args.group('pattern')
  41 
  42 #       Return a (filtered) list of pages names.
  43         if filter_regex:
  44            matching_pages = wikiutil.searchPages(filter_regex)
  45            matching_pages = matching_pages[1]
  46 #       Clean the list
  47            cleanlist = []
  48         while len(matching_pages) > 0:
  49                 cleanlist.append(matching_pages[0][1])
  50                 del matching_pages[0]
  51     else:
  52         cleanlist = wikiutil.getPageList(config.text_dir)
  53 
  54 #       Filter out the ignored list
  55     if args.group('ignore') is not None:
  56         ignorefilter = '(?!' +  args.group('ignore') + ')'
  57         cond = re.compile(ignorefilter).match
  58         cleanlist = filter(cond, cleanlist)
  59 
  60     # get number of wanted links
  61     try:
  62         links = max(int(args.group('links')), 1)
  63     except StandardError:
  64         links = 1
  65 
  66     # select the pages from the page list
  67     pages = []
  68     while len(pages) < links and cleanlist:
  69         page = whrandom.choice(cleanlist)
  70         if macro.request.user.may.read(page):
  71             pages.append(page)
  72         cleanlist.remove(page)
  73 
  74     # return a single page link
  75     if links == 1: return macro.formatter.pagelink(pages[0], generated=1)
  76 
  77     # return a list of page links
  78     pages.sort()
  79     result = macro.formatter.bullet_list(1)
  80     for name in pages:
  81         result = result + macro.formatter.listitem(1)
  82         result = result + macro.formatter.pagelink(name, generated=1)
  83         result = result + macro.formatter.listitem(0)
  84     result = result + macro.formatter.bullet_list(0)
  85 
  86     return result

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-04-06 20:21:23, 3.1 KB) [[attachment:RandomPageContaining.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.