MoinMoin - IncludePages macro

Is this the former version of "Include"? see macro/Include.py and HelpOnMacros/Include

Copyright (c) 2003,2004 by Jun Hu <j.hu@tue.nl>.
A modification of Michael Reinsch's original macro/IncludePages.py, to:

Copyright (c) 2002 by Michael Reinsch <mr@uue.org>.
All rights reserved, see COPYING for details.

Code based on the MoinMoin PageList macro
Copyright (c) 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>

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)]]

Examples

[[IncludePages(FooBar/20.*)]]

[[IncludePages(FooBar/20.*, 2)]]

[[IncludePages(FooBar/20.*, 2, sort=descending]]

[[IncludePages(FooBar/20.*, 2, sort=descending, items=1]]

Code (for Moin 1.2)

MacroMarket/IncludePages/IncludePages-moin12.py

   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
MacroMarket/IncludePages/IncludePages-moin12.py

Code (for Moin 1.1)

MacroMarket/WorksWith1.1/IncludePages.py

   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
MacroMarket/WorksWith1.1/IncludePages.py

Comments?

Q. Is there a way to then have the TableOfContents macro list the included pages? Currently it only lists the page itself.

A. It will list all the page content after including the pages in 1.3

MoinMoin: MacroMarket/IncludePages (last edited 2008-01-29 20:44:28 by JordanCronin)