1 # Include is now a standard macro
   2 
   3 """
   4     MoinMoin - Include macro
   5 
   6     Copyright (c) 2000 by Richard Jones <richard@bizarsoftware.com.au>
   7     Copyright (c) 2000 by Jürgen Hermann <jh@web.de>
   8     All rights reserved, see COPYING for details.
   9 
  10     This macro includes the formatted content of the given page, following recursive
  11     includes if encountered. Cycles are detected!
  12 
  13     Usage:
  14         [[Include(pagename,heading,level)]]
  15 
  16         pagename    Name of the page to include
  17         heading     Text for the generated heading (optional)
  18         level       Level (1..5) of the generated heading (optional)
  19 
  20     Examples:
  21         [[Include(FooBar)]]  -- include the text of FooBar in the current paragraph.
  22         [[Include(FooBar, heading)]] -- add a H1 of 'Foo Bar' followed by the text
  23         [[Include(FooBar, heading, 2]] -- add a H2 of 'Foo Bar'
  24         [[Include(FooBar, 'All about Foo Bar', 2]] -- add a H2 of 'All about Foo Bar'
  25 
  26     $Id$
  27 """
  28 
  29 import sys, string, re, StringIO
  30 from MoinMoin.Page import Page
  31 
  32 args_re_pattern = r'(?P<name>\w+)(,\s*(?P<heading>(heading|[\'"](?P<htext>.+)[\'"]))(,\s*(?P<level>\d+))?)?'
  33 
  34 def execute(macro, text, args_re=re.compile(args_re_pattern)):
  35     ret = ''
  36     # parse and check arguments
  37     m = args_re.match(text)
  38     if not m:
  39         ret = ret + '<strong class="error">Invaild include arguments "%s"</strong>'%text
  40 
  41     # get the Page
  42     name = m.group('name')
  43     this_page = macro.formatter.page
  44     if not hasattr(this_page, 'included'):
  45         this_page.included = {}
  46     if this_page.included.has_key(name):
  47         ret = ret + '<strong class="error">Recursive include of "%s" forbidden</strong>'%name
  48     inc_page = Page(name)
  49     inc_page.included = this_page.included
  50 
  51     # do headings
  52     level = None
  53     if m.group('heading'):
  54         level = 1
  55         if m.group('htext'):
  56             heading = m.group('htext')
  57         else:
  58             heading = page.split_title()
  59         if m.group('level'):
  60             level = int(m.group('level'))
  61         ret = ret + macro.formatter.url(name, macro.formatter.heading(level, heading))
  62 
  63     # output the Page
  64     this_page.included[name] = 1
  65     stdout = sys.stdout
  66     # python... I love you
  67     sys.stdout = StringIO.StringIO()
  68     inc_page.send_page(macro.form, content_only=1)
  69     ret = ret + sys.stdout.getvalue()
  70     sys.stdout = stdout
  71 
  72     # if not printable, then output a helper link
  73     if not level and (not macro.form.has_key('print') or
  74             not macro.form['print'].value):
  75         ret = ret + macro.formatter.url(name, '<small>[goto %s]</small>'%name)
  76 
  77     del this_page.included[name]
  78     return ret

MoinMoin: macro/Include.py (last edited 2007-10-29 19:20:31 by localhost)