Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.macro.Include Namespace Reference

Functions

def extract_titles
def execute

Variables

list Dependencies = ["time"]
 generates_headings = True
string _sysmsg = '<p><strong class="%s">%s</strong></p>'
string _arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
 keep in sync with TableOfContents macro!
string _arg_level = r',\s*(?P<level>\d*)'
string _arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'
string _arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'
string _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
string _arg_items = r'(,\s*items=(?P<items>\d+))?'
string _arg_skipitems = r'(,\s*skipitems=(?P<skipitems>\d+))?'
string _arg_titlesonly = r'(,\s*(?P<titlesonly>titlesonly))?'
string _arg_editlink = r'(,\s*(?P<editlink>editlink))?'
string _args_re_pattern = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s%s)?$'
string _title_re = r"^(?P<heading>\s*(?P<hmarker>=+)\s.*\s(?P=hmarker))$"

Function Documentation

def MoinMoin.macro.Include.execute (   macro,
  text,
  args_re = re.compile(_args_re_pattern),
  title_re = re.compile(_title_re, re.M 
)

Definition at line 55 of file Include.py.

00055 
00056 def execute(macro, text, args_re=re.compile(_args_re_pattern), title_re=re.compile(_title_re, re.M)):
00057     request = macro.request
00058     _ = request.getText
00059 
00060     # return immediately if getting links for the current page
00061     if request.mode_getpagelinks:
00062         return ''
00063 
00064     # parse and check arguments
00065     args = text and args_re.match(text)
00066     if not args:
00067         return (_sysmsg % ('error', _('Invalid include arguments "%s"!')) % (text, ))
00068 
00069     # prepare including page
00070     result = []
00071     print_mode = request.action in ("print", "format")
00072     this_page = macro.formatter.page
00073     if not hasattr(this_page, '_macroInclude_pagelist'):
00074         this_page._macroInclude_pagelist = {}
00075 
00076     # get list of pages to include
00077     inc_name = wikiutil.AbsPageName(this_page.page_name, args.group('name'))
00078     pagelist = [inc_name]
00079     if inc_name.startswith("^"):
00080         try:
00081             inc_match = re.compile(inc_name)
00082         except re.error:
00083             pass # treat as plain page name
00084         else:
00085             # Get user filtered readable page list
00086             pagelist = request.rootpage.getPageList(filter=inc_match.match)
00087 
00088     # sort and limit page list
00089     pagelist.sort()
00090     sort_dir = args.group('sort')
00091     if sort_dir == 'descending':
00092         pagelist.reverse()
00093     max_items = args.group('items')
00094     if max_items:
00095         pagelist = pagelist[:int(max_items)]
00096 
00097     skipitems = 0
00098     if args.group("skipitems"):
00099         skipitems = int(args.group("skipitems"))
00100     titlesonly = args.group('titlesonly')
00101     editlink = args.group('editlink')
00102 
00103     # iterate over pages
00104     for inc_name in pagelist:
00105         if not request.user.may.read(inc_name):
00106             continue
00107         if inc_name in this_page._macroInclude_pagelist:
00108             result.append(u'<p><strong class="error">Recursive include of "%s" forbidden</strong></p>' % (inc_name, ))
00109             continue
00110         if skipitems:
00111             skipitems -= 1
00112             continue
00113         fmt = macro.formatter.__class__(request, is_included=True)
00114         fmt._base_depth = macro.formatter._base_depth
00115         inc_page = Page(request, inc_name, formatter=fmt)
00116         if not inc_page.exists():
00117             continue
00118         inc_page._macroInclude_pagelist = this_page._macroInclude_pagelist
00119 
00120         # check for "from" and "to" arguments (allowing partial includes)
00121         body = inc_page.get_raw_body() + '\n'
00122         from_pos = 0
00123         to_pos = -1
00124         from_re = args.group('from')
00125         if from_re:
00126             try:
00127                 from_match = re.compile(from_re, re.M).search(body)
00128             except re.error:
00129                 ##result.append("*** fe=%s ***" % e)
00130                 from_match = re.compile(re.escape(from_re), re.M).search(body)
00131             if from_match:
00132                 from_pos = from_match.end()
00133             else:
00134                 result.append(_sysmsg % ('warning', 'Include: ' + _('Nothing found for "%s"!')) % from_re)
00135         to_re = args.group('to')
00136         if to_re:
00137             try:
00138                 to_match = re.compile(to_re, re.M).search(body, from_pos)
00139             except re.error:
00140                 to_match = re.compile(re.escape(to_re), re.M).search(body, from_pos)
00141             if to_match:
00142                 to_pos = to_match.start()
00143             else:
00144                 result.append(_sysmsg % ('warning', 'Include: ' + _('Nothing found for "%s"!')) % to_re)
00145 
00146         if titlesonly:
00147             levelstack = []
00148             for title, level in extract_titles(body[from_pos:to_pos], title_re):
00149                 if levelstack:
00150                     if level > levelstack[-1]:
00151                         result.append(macro.formatter.bullet_list(1))
00152                         levelstack.append(level)
00153                     else:
00154                         while levelstack and level < levelstack[-1]:
00155                             result.append(macro.formatter.bullet_list(0))
00156                             levelstack.pop()
00157                         if not levelstack or level != levelstack[-1]:
00158                             result.append(macro.formatter.bullet_list(1))
00159                             levelstack.append(level)
00160                 else:
00161                     result.append(macro.formatter.bullet_list(1))
00162                     levelstack.append(level)
00163                 result.append(macro.formatter.listitem(1))
00164                 result.append(inc_page.link_to(request, title))
00165                 result.append(macro.formatter.listitem(0))
00166             while levelstack:
00167                 result.append(macro.formatter.bullet_list(0))
00168                 levelstack.pop()
00169             continue
00170 
00171         if from_pos or to_pos != -1:
00172             inc_page.set_raw_body(body[from_pos:to_pos], modified=True)
00173         ##result.append("*** f=%s t=%s ***" % (from_re, to_re))
00174         ##result.append("*** f=%d t=%d ***" % (from_pos, to_pos))
00175 
00176         if not hasattr(request, "_Include_backto"):
00177             request._Include_backto = this_page.page_name
00178 
00179         # do headings
00180         level = None
00181         if args.group('heading') and args.group('hquote'):
00182             heading = args.group('htext') or inc_page.split_title()
00183             level = 1
00184             if args.group('level'):
00185                 level = int(args.group('level'))
00186             if print_mode:
00187                 result.append(macro.formatter.heading(1, level) +
00188                               macro.formatter.text(heading) +
00189                               macro.formatter.heading(0, level))
00190             else:
00191                 url = inc_page.url(request)
00192                 result.extend([
00193                     macro.formatter.heading(1, level, id=heading),
00194                     macro.formatter.url(1, url, css="include-heading-link"),
00195                     macro.formatter.text(heading),
00196                     macro.formatter.url(0),
00197                     macro.formatter.heading(0, level),
00198                 ])
00199 
00200         # set or increment include marker
00201         this_page._macroInclude_pagelist[inc_name] = \
00202             this_page._macroInclude_pagelist.get(inc_name, 0) + 1
00203 
00204         # output the included page
00205         strfile = StringIO.StringIO()
00206         request.redirect(strfile)
00207         try:
00208             inc_page.send_page(content_only=True,
00209                                omit_footnotes=True,
00210                                count_hit=False)
00211             result.append(strfile.getvalue())
00212         finally:
00213             request.redirect()
00214 
00215         # decrement or remove include marker
00216         if this_page._macroInclude_pagelist[inc_name] > 1:
00217             this_page._macroInclude_pagelist[inc_name] = \
00218                 this_page._macroInclude_pagelist[inc_name] - 1
00219         else:
00220             del this_page._macroInclude_pagelist[inc_name]
00221 
00222         # if no heading and not in print mode, then output a helper link
00223         if editlink and not (level or print_mode):
00224             result.extend([
00225                 macro.formatter.div(1, css_class="include-link"),
00226                 inc_page.link_to(request, '[%s]' % (inc_name, ), css_class="include-page-link"),
00227                 inc_page.link_to(request, '[%s]' % (_('edit'), ), css_class="include-edit-link", querystr={'action': 'edit', 'backto': request._Include_backto}),
00228                 macro.formatter.div(0),
00229             ])
00230         # XXX page.link_to is wrong now, it escapes the edit_icon html as it escapes normal text
00231 
00232     # return include text
00233     return ''.join(result)
00234 
00235 # vim:ts=4:sw=4:et

Here is the call graph for this function:

def MoinMoin.macro.Include.extract_titles (   body,
  title_re 
)

Definition at line 44 of file Include.py.

00044 
00045 def extract_titles(body, title_re):
00046     titles = []
00047     for title, _ in title_re.findall(body):
00048         h = title.strip()
00049         level = 1
00050         while h[level:level+1] == '=':
00051             level += 1
00052         title_text = h[level:-level].strip()
00053         titles.append((title_text, level))
00054     return titles

Here is the caller graph for this function:


Variable Documentation

string MoinMoin.macro.Include._arg_editlink = r'(,\s*(?P<editlink>editlink))?'

Definition at line 37 of file Include.py.

string MoinMoin.macro.Include._arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'

Definition at line 31 of file Include.py.

string MoinMoin.macro.Include._arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'

keep in sync with TableOfContents macro!

Definition at line 29 of file Include.py.

string MoinMoin.macro.Include._arg_items = r'(,\s*items=(?P<items>\d+))?'

Definition at line 34 of file Include.py.

string MoinMoin.macro.Include._arg_level = r',\s*(?P<level>\d*)'

Definition at line 30 of file Include.py.

string MoinMoin.macro.Include._arg_skipitems = r'(,\s*skipitems=(?P<skipitems>\d+))?'

Definition at line 35 of file Include.py.

string MoinMoin.macro.Include._arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'

Definition at line 33 of file Include.py.

string MoinMoin.macro.Include._arg_titlesonly = r'(,\s*(?P<titlesonly>titlesonly))?'

Definition at line 36 of file Include.py.

string MoinMoin.macro.Include._arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'

Definition at line 32 of file Include.py.

string MoinMoin.macro.Include._args_re_pattern = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s%s)?$'

Definition at line 38 of file Include.py.

string MoinMoin.macro.Include._sysmsg = '<p><strong class="%s">%s</strong></p>'

Definition at line 26 of file Include.py.

string MoinMoin.macro.Include._title_re = r"^(?P<heading>\s*(?P<hmarker>=+)\s.*\s(?P=hmarker))$"

Definition at line 42 of file Include.py.

Definition at line 17 of file Include.py.

Definition at line 19 of file Include.py.