Attachment 'IncludePages-moin12.py'

Download

   1 """
   2     MoinMoin - IncludePages macro
   3     
   4     Copyright (c) 2003 by Jun Hu <j.hu@tue.nl>
   5 
   6     Copyright (c) 2002 by Michael Reinsch <mr@uue.org>
   7     All rights reserved, see COPYING for details.
   8 
   9     Code based on the MoinMoin PageList macro
  10     Copyright (c) 2000, 2001, 2002 by J¨¹rgen Hermann <jh@web.de>
  11 
  12     This macro includes the formatted content of the given pages, following
  13     recursive includes if encountered. Cycles are detected!
  14 
  15     It uses the MoinMoin Include macro which does the real work.
  16 
  17     Usage:
  18         [[IncludePages(pagepattern,level, sort=ascending|descending, items=n)]]
  19 
  20         pagepattern Pattern of the page(s) to include
  21         level       Level (1..5) of the generated heading (optional)
  22         sort        Sorting order (optional). 
  23         items       Maximum number of pages to include. 
  24         
  25         The headings for the included pages will be generated from the page
  26         names
  27 
  28     Examples:
  29         [[IncludePages(FooBar/20.*)]]
  30            -- includes all pages which start with FooBar/20 this is usefull
  31               in combination with the MonthCalendar macro
  32 
  33         [[IncludePages(FooBar/20.*, 2)]]
  34            -- set level to 2 (default is 1)
  35            
  36         [[IncludePages(FooBar/20.*, 2, sort=descending]]
  37            -- reverse the ordering (default is ascending)
  38        
  39         [[IncludePages(FooBar/20.*, 2, sort=descending, items=1]]
  40            -- Only the last item will be included.
  41 
  42     $Id$
  43 """
  44 
  45 import re
  46 #from MoinMoin import user
  47 from MoinMoin import config
  48 from MoinMoin import wikiutil
  49 #from MoinMoin.i18n import _
  50 import MoinMoin.macro.Include
  51 
  52 _arg_level = r',\s*(?P<level>\d+)'
  53 _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
  54 _arg_items = r'(,\s*items=(?P<items>\d+))?'
  55 _args_re_pattern = r'^(?P<pattern>[^,]+)((%s)?%s%s)?$' % (_arg_level,_arg_sort,_arg_items)
  56 
  57 def execute(macro, text, args_re=re.compile(_args_re_pattern)):
  58     ret = ''
  59 
  60     # parse and check arguments
  61     args = args_re.match(text)
  62     if not args:
  63         return ('<p><strong class="error">%s</strong></p>' %
  64             _('Invalid include arguments "%s"!')) % (text,)
  65 
  66     # get the pages
  67     inc_pattern = args.group('pattern')
  68     if args.group('level'):
  69         level = int(args.group('level'))
  70     else:
  71         level = 1
  72 
  73     try:
  74         needle_re = re.compile(inc_pattern, re.IGNORECASE)
  75     except re.error, e:
  76         return ('<p><strong class="error">%s</strong></p>' %
  77             _("ERROR in regex '%s'") % (inc_pattern,), e)
  78 
  79     all_pages = wikiutil.getPageList(config.text_dir)
  80     hits = filter(needle_re.search, all_pages)
  81     hits.sort()
  82     sort_dir = args.group('sort')
  83     if sort_dir == 'descending':
  84         hits.reverse()
  85     max_items = args.group('items')
  86     if max_items:
  87         hits = hits[:int(max_items)]
  88 
  89     for inc_name in hits:
  90         params = '%s,"%s",%s' % (inc_name,inc_name, level)
  91         ret = ret +"<p>"+ MoinMoin.macro.Include.execute(macro, params) +"\n"
  92 
  93     # return include text
  94     return ret

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-02-24 00:09:03, 3.0 KB) [[attachment:IncludePages-moin12.py]]
 All files | Selected Files: delete move to page copy to page

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