00001
00002 """
00003 MoinMoin - Include macro
00004
00005 This macro includes the formatted content of the given page(s). See
00006
00007 http://purl.net/wiki/moinmaster/HelpOnMacros/Include
00008
00009 for detailed docs.
00010
00011 @copyright: 2000-2004 by Jürgen Hermann <jh@web.de>
00012 @copyright: 2000-2001 by Richard Jones <richard@bizarsoftware.com.au>
00013 @license: GNU GPL, see COPYING for details.
00014 """
00015
00016
00017 Dependencies = ["time"]
00018
00019 import re, StringIO
00020 from MoinMoin import wikiutil
00021 from MoinMoin.Page import Page
00022 from MoinMoin.util import web
00023
00024 _sysmsg = '<p><strong class="%s">%s</strong></p>'
00025
00026
00027 _arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
00028 _arg_level = r',\s*(?P<level>\d*)'
00029 _arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'
00030 _arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'
00031 _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
00032 _arg_items = r'(,\s*items=(?P<items>\d+))?'
00033 _arg_skipitems = r'(,\s*skipitems=(?P<skipitems>\d+))?'
00034 _arg_titlesonly = r'(,\s*(?P<titlesonly>titlesonly))?'
00035 _arg_editlink = r'(,\s*(?P<editlink>editlink))?'
00036 _args_re_pattern = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s%s)?$' % (
00037 _arg_heading, _arg_level, _arg_from, _arg_to, _arg_sort, _arg_items,
00038 _arg_skipitems, _arg_titlesonly, _arg_editlink)
00039
00040 _title_re = r"^(?P<heading>\s*(?P<hmarker>=+)\s.*\s(?P=hmarker))$"
00041
00042 def extract_titles(body, title_re):
00043 titles = []
00044 for title, _ in title_re.findall(body):
00045 h = title.strip()
00046 level = 1
00047 while h[level:level+1] == '=': level = level+1
00048 depth = min(5,level)
00049 title_text = h[level:-level].strip()
00050 titles.append((title_text, level))
00051 return titles
00052
00053 def execute(macro, text, args_re=re.compile(_args_re_pattern), title_re=re.compile(_title_re, re.M), called_by_toc=0):
00054 request = macro.request
00055 _ = request.getText
00056
00057
00058 if request.mode_getpagelinks:
00059 return ''
00060
00061
00062 args = text and args_re.match(text)
00063 if not args:
00064 return (_sysmsg % ('error', _('Invalid include arguments "%s"!')) % (text,))
00065
00066
00067 result = []
00068 print_mode = macro.form.has_key('action') and macro.form['action'][0] in ("print", "format")
00069 this_page = macro.formatter.page
00070 if not hasattr(this_page, '_macroInclude_pagelist'):
00071 this_page._macroInclude_pagelist = {}
00072
00073
00074 inc_name = wikiutil.AbsPageName(request, this_page.page_name, args.group('name'))
00075 pagelist = [inc_name]
00076 if inc_name.startswith("^"):
00077 try:
00078 inc_match = re.compile(inc_name)
00079 except re.error:
00080 pass
00081 else:
00082
00083 pagelist = request.rootpage.getPageList(filter=inc_match.match)
00084
00085
00086 pagelist.sort()
00087 sort_dir = args.group('sort')
00088 if sort_dir == 'descending':
00089 pagelist.reverse()
00090 max_items = args.group('items')
00091 if max_items:
00092 pagelist = pagelist[:int(max_items)]
00093
00094 skipitems = 0
00095 if args.group("skipitems"):
00096 skipitems = int(args.group("skipitems"))
00097 titlesonly = args.group('titlesonly')
00098 editlink = args.group('editlink')
00099
00100
00101 for inc_name in pagelist:
00102 if not request.user.may.read(inc_name):
00103 continue
00104 if this_page._macroInclude_pagelist.has_key(inc_name):
00105 result.append(u'<p><strong class="error">Recursive include of "%s" forbidden</strong></p>' % (inc_name,))
00106 continue
00107 if skipitems:
00108 skipitems -= 1
00109 continue
00110 fmt = macro.formatter.__class__(request, is_included=True)
00111 fmt._base_depth = macro.formatter._base_depth
00112 inc_page = Page(request, inc_name, formatter=fmt)
00113 if not inc_page.exists():
00114 continue
00115 inc_page._macroInclude_pagelist = this_page._macroInclude_pagelist
00116
00117
00118 body = inc_page.get_raw_body() + '\n'
00119
00120
00121 body = body.replace('amcl', "_" + this_page.page_name + "_")
00122
00123
00124 this_page._macroInclude_pagelist[inc_name] = \
00125 this_page._macroInclude_pagelist.get(inc_name, 0) + 1
00126
00127
00128 strfile = StringIO.StringIO()
00129 request.redirect(strfile)
00130 try:
00131 cid = request.makeUniqueID("Include_%s" % wikiutil.quoteWikinameURL(inc_page.page_name))
00132 inc_page.send_page(request, content_only=1, content_id=cid,
00133 omit_footnotes=True)
00134 result.append(strfile.getvalue())
00135 finally:
00136 request.redirect()
00137
00138
00139 if this_page._macroInclude_pagelist[inc_name] > 1:
00140 this_page._macroInclude_pagelist[inc_name] = \
00141 this_page._macroInclude_pagelist[inc_name] - 1
00142 else:
00143 del this_page._macroInclude_pagelist[inc_name]
00144
00145
00146
00147 str = ''.join(result)
00148 return str
00149
00150