diff --git a/wiki/macro/Include.py b/wiki/macro/Include.py new file mode 100644 index 00000000..d7175a00 --- /dev/null +++ b/wiki/macro/Include.py @@ -0,0 +1,78 @@ +# Include is now a standard macro + +""" + MoinMoin - Include macro + + Copyright (c) 2000 by Richard Jones + Copyright (c) 2000 by Jürgen Hermann + 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\w+)(,\s*(?P(heading|[\'"](?P.+)[\'"]))(,\s*(?P\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 + 'Invaild include arguments "%s"'%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 + 'Recursive include of "%s" forbidden'%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, '[goto %s]'%name) + + del this_page.included[name] + return ret