""" MoinMoin - IncludePages macro Copyright (c) 2003 by Jun Hu Copyright (c) 2002 by Michael Reinsch All rights reserved, see COPYING for details. Code based on the MoinMoin PageList macro Copyright (c) 2000, 2001, 2002 by Jščrgen Hermann This macro includes the formatted content of the given pages, following recursive includes if encountered. Cycles are detected! It uses the MoinMoin Include macro which does the real work. Usage: [[IncludePages(pagepattern,level, sort=ascending|descending, items=n)]] pagepattern Pattern of the page(s) to include level Level (1..5) of the generated heading (optional) sort Sorting order (optional). items Maximum number of pages to include. The headings for the included pages will be generated from the page names Examples: [[IncludePages(FooBar/20.*)]] -- includes all pages which start with FooBar/20 this is usefull in combination with the MonthCalendar macro [[IncludePages(FooBar/20.*, 2)]] -- set level to 2 (default is 1) [[IncludePages(FooBar/20.*, 2, sort=descending]] -- reverse the ordering (default is ascending) [[IncludePages(FooBar/20.*, 2, sort=descending, items=1]] -- Only the last item will be included. $Id$ """ import re from MoinMoin import user from MoinMoin import config from MoinMoin import wikiutil from MoinMoin.i18n import _ import MoinMoin.macro.Include _arg_level = r',\s*(?P\d+)' _arg_sort = r'(,\s*sort=(?P(ascending|descending)))?' _arg_items = r'(,\s*items=(?P\d+))?' _args_re_pattern = r'^(?P[^,]+)((%s)?%s%s)?$' % (_arg_level,_arg_sort,_arg_items) def execute(macro, text, args_re=re.compile(_args_re_pattern)): ret = '' # parse and check arguments args = args_re.match(text) if not args: return ('

%s

' % _('Invalid include arguments "%s"!')) % (text,) # get the pages inc_pattern = args.group('pattern') if args.group('level'): level = int(args.group('level')) else: level = 1 try: needle_re = re.compile(inc_pattern, re.IGNORECASE) except re.error, e: return ('

%s

' % _("ERROR in regex '%s'") % (inc_pattern,), e) all_pages = wikiutil.getPageList(config.text_dir) hits = filter(needle_re.search, all_pages) hits.sort() sort_dir = args.group('sort') if sort_dir == 'descending': hits.reverse() max_items = args.group('items') if max_items: hits = hits[:int(max_items)] for inc_name in hits: params = '%s,"%s",%s' % (inc_name,inc_name, level) ret = ret +"

"+ MoinMoin.macro.Include.execute(macro, params) +"\n" # return include text return ret