Commit 7e6d2ba3 authored by Aurore Mo-Ma's avatar Aurore Mo-Ma

[wiki] ajout de la macro Include.py (http://moinmo.in/HelpOnMacros/Include)

parent 93f369ea
# Include is now a standard macro
"""
MoinMoin - Include macro
Copyright (c) 2000 by Richard Jones <richard@bizarsoftware.com.au>
Copyright (c) 2000 by Jürgen Hermann <jh@web.de>
All rights reserved, see COPYING for details.
This macro includes the formatted content of the given page, following recursive
includes if encountered. Cycles are detected!
Usage:
[[Include(pagename,heading,level)]]
pagename Name of the page to include
heading Text for the generated heading (optional)
level Level (1..5) of the generated heading (optional)
Examples:
[[Include(FooBar)]] -- include the text of FooBar in the current paragraph.
[[Include(FooBar, heading)]] -- add a H1 of 'Foo Bar' followed by the text
[[Include(FooBar, heading, 2]] -- add a H2 of 'Foo Bar'
[[Include(FooBar, 'All about Foo Bar', 2]] -- add a H2 of 'All about Foo Bar'
$Id$
"""
import sys, string, re, StringIO
from MoinMoin.Page import Page
args_re_pattern = r'(?P<name>\w+)(,\s*(?P<heading>(heading|[\'"](?P<htext>.+)[\'"]))(,\s*(?P<level>\d+))?)?'
def execute(macro, text, args_re=re.compile(args_re_pattern)):
ret = ''
# parse and check arguments
m = args_re.match(text)
if not m:
ret = ret + '<strong class="error">Invaild include arguments "%s"</strong>'%text
# get the Page
name = m.group('name')
this_page = macro.formatter.page
if not hasattr(this_page, 'included'):
this_page.included = {}
if this_page.included.has_key(name):
ret = ret + '<strong class="error">Recursive include of "%s" forbidden</strong>'%name
inc_page = Page(name)
inc_page.included = this_page.included
# do headings
level = None
if m.group('heading'):
level = 1
if m.group('htext'):
heading = m.group('htext')
else:
heading = page.split_title()
if m.group('level'):
level = int(m.group('level'))
ret = ret + macro.formatter.url(name, macro.formatter.heading(level, heading))
# output the Page
this_page.included[name] = 1
stdout = sys.stdout
# python... I love you
sys.stdout = StringIO.StringIO()
inc_page.send_page(macro.form, content_only=1)
ret = ret + sys.stdout.getvalue()
sys.stdout = stdout
# if not printable, then output a helper link
if not level and (not macro.form.has_key('print') or
not macro.form['print'].value):
ret = ret + macro.formatter.url(name, '<small>[goto %s]</small>'%name)
del this_page.included[name]
return ret
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment