Attachment 'ChildPages.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     ChildPages Macro 2014.4.29
   4 
   5     List first-level subpages for the current page.
   6 
   7     @copyright: 2009, 2010 Renato Silva
   8     @license: GNU GPLv2
   9 """
  10 
  11 Dependencies = ["namespace"]
  12 from MoinMoin import search, wikiutil
  13 from MoinMoin.search.results import FoundAttachment
  14 import re
  15 
  16 def single_name(page_name):
  17     return page_name.split('/')[-1]
  18 
  19 def cut_name(text, max_length):
  20     if max_length < 1:
  21         return text
  22     return re.compile('^(.{%d}).+$' % max_length).sub(ur'\1...', text)
  23 
  24 def spaced_name(wiki_name):
  25     wiki_name = re.compile('([^A-Z\s])([A-Z])').sub(ur'\1 \2', wiki_name)
  26     wiki_name = re.compile('(\d)(\D)').sub(ur'\1 \2', wiki_name)
  27     wiki_name = wiki_name.replace('_', ' ')
  28     return wiki_name.replace('/', ' /')
  29 
  30 def format_name(page_name, max_length, add_spaces):
  31     page_name = single_name(page_name)
  32     if add_spaces:
  33         page_name = spaced_name(page_name)
  34     return cut_name(page_name, max_length)
  35 
  36 def format_macro_title(title, page_name):
  37     if title.count('%s') == 1:
  38         title = title %  page_name
  39     return title
  40 
  41 def action_allowed(request, on_actions):
  42     action_listed = lambda request: request.action in on_actions.split('|')
  43     if on_actions.startswith('not:'):
  44         on_actions = on_actions[4:]
  45         allowed = lambda: not action_listed(request)
  46     elif on_actions != 'all':
  47         allowed = lambda: action_listed(request)
  48     else:
  49         allowed = lambda: True
  50     return allowed()
  51 
  52 def child_regex(page_name):
  53     for old, new in [(' ', '\s'), ('?', '\?'), (':', '.')]:
  54         prepared_page_name = page_name.replace(old, new)
  55     return u'regex:^%s/[^/]+$' % prepared_page_name
  56 
  57 def macro_ChildPages(macro, on=None, title=None, none_note=None, more_link=None, max_pages=0, max_name=0, use_list=True, spaces=True):
  58     if not more_link: more_link = macro._('List all...')
  59     if not on: on = 'show'
  60     request = macro.request
  61     fmt = macro.formatter
  62     page = request.page if fmt.page.page_name == 'SideBar' else fmt.page
  63     if not action_allowed(request, on):
  64         return ''
  65 
  66     output = []
  67     append = output.append
  68     def append_item(item, use_list=use_list):
  69         tag = fmt.listitem if use_list else fmt.paragraph
  70         append(tag(1))
  71         append(item)
  72         append(tag(0))
  73 
  74     if title:
  75         page_name = format_name(page.page_name, max_name, spaces)
  76         append(format_macro_title(title, page_name))
  77 
  78     query = child_regex(page.page_name)
  79     found_children = search.searchPages(request, query, titlesearch=1, case=0, sort='page_name').hits
  80     found_children = [child for child in found_children if not isinstance(child, FoundAttachment)]
  81 
  82     if found_children:
  83         too_many_children = (max_pages > 0 and len(found_children) > max_pages)
  84         if too_many_children: found_children = found_children[:max_pages]
  85 
  86         append(fmt.div(1, css_class='searchresults'))
  87         if use_list: append(fmt.bullet_list(1))
  88 
  89         for found_child in found_children:
  90             child = found_child.page
  91             append_item(child.link_to(request, text=format_name(child.page_name, max_name, spaces)))
  92 
  93         if too_many_children: append_item(page.link_to(request, text=more_link, querystr={'action': 'LocalSiteMap'}))
  94         if use_list: append(fmt.bullet_list(0))
  95         append(fmt.div(0))
  96     elif none_note != None:
  97         append_item(none_note, use_list=use_list)
  98     else:
  99         return ''
 100     return ''.join(output)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2020-12-26 12:14:00, 3.4 KB) [[attachment:ChildPages.py]]
  • [get | view] (2009-06-06 22:37:34, 36.9 KB) [[attachment:example1.png]]
  • [get | view] (2009-06-06 22:49:38, 35.0 KB) [[attachment:example2.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.