Attachment 'Navitree.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Navitree Macro
   4 
   5     Originally authored by Jennifer Vanderputten (Jen@RabidWeasel.com)
   6     Based off of Navigation.py
   7 """
   8 
   9 import re
  10 from MoinMoin import config
  11 from MoinMoin.Page import Page
  12 
  13 Dependencies = ["namespace"]
  14 
  15 def _getParent(pagename):
  16     """ Return parent of pagename.
  17     """
  18     pos = pagename.rfind('/')
  19     if pos >= 0:
  20         return pagename[:pos]
  21     return None
  22 
  23 
  24 def _getPages(request, filter_regex=None):
  25     """ Return a (filtered) list of pages names.
  26     """
  27     filter = None
  28     if filter_regex:
  29         filter = re.compile(filter_regex).match
  30     pages = request.rootpage.getPageList(filter=filter)
  31     pages.sort()
  32     return pages
  33 
  34 
  35 def _getLinks(request, pagename, filter_regex=None):
  36     """ Return pagename for up, first, prev, next, last; each can be None.
  37     """
  38     pos, size, first, prev, next, last = 0, 0, None, None, None, None
  39 
  40     all_pages = _getPages(request, filter_regex)
  41     size = len(all_pages)
  42 
  43     if all_pages:
  44         try:
  45             pos = all_pages.index(pagename)
  46         except ValueError:
  47             # this should never happen in theory, but let's be sure
  48             pass
  49         else:
  50             if pos > 0:
  51                 first = all_pages[0]
  52                 prev = all_pages[pos-1]
  53             if pos+1 < len(all_pages):
  54                 next = all_pages[pos+1]
  55                 last = all_pages[-1]
  56 
  57     return pos, size, (first, prev, next, last)
  58 
  59 
  60 class Navitree:
  61     """ Dispatcher class implementing the navitree schemes.
  62     """
  63 
  64     # querystring for slideshow links
  65     PROJECTION = 'action=print&media=projection'
  66 
  67     def __init__(self, macro, args):
  68         """ Prepare common values used during processing.
  69         """
  70         self.macro = macro
  71         self.args = args.split(',')
  72         self._ = self.macro.request.getText
  73 
  74         self.pagename = self.macro.formatter.page.page_name
  75         self.print_mode = self.macro.request.form.has_key('action') \
  76             and self.macro.request.form['action'][0] == 'print'
  77         self.media = self.macro.request.form.get('media', [None])[0]
  78         self.querystr = self.print_mode and self.PROJECTION or ''
  79 
  80 
  81     def dispatch(self):
  82         """ Return None if in plain print mode (no navigational
  83             elements in printouts), else the proper HTML code.
  84         """
  85         if self.print_mode and self.media != 'projection':
  86             return None
  87 
  88         scheme = self.args[0] or '<default>'
  89         return getattr(self, 'do_'+scheme, self.badscheme)()
  90 
  91 
  92     def badscheme(self):
  93         """ Bad scheme argument.
  94         """
  95         _ = self._
  96         scheme = self.args[0]
  97         return (self.macro.formatter.sysmsg(1) +
  98                 self.macro.formatter.text(
  99             _("Unsupported navitree scheme '%(scheme)s'!") %
 100             {'scheme': scheme}) +
 101                 self.macro.formatter.sysmsg(0))
 102 
 103 
 104     def do_childtree(self):
 105         return self.do_tree(root=self.pagename, currentdepth=0, parents={})
 106 
 107     def do_tree(self, root=None, currentdepth=0, parents={}):
 108         """ Navigate to subpages from a parent page.
 109         """
 110         _ = self._
 111         try:
 112             depth = int(self.args[1])
 113         except (IndexError, TypeError, ValueError):
 114             depth = 0
 115 
 116         # get parent page name if no root was specified
 117         parent = root or _getParent(self.pagename)
 118         if not parent:
 119             return (self.macro.formatter.sysmsg(1) +
 120                     self.macro.formatter.text(_('No parent page found!'))+
 121                     self.macro.formatter.sysmsg(0))
 122         else:
 123             parents[parent] = 1
 124 
 125         # limit depth if a depth was specified
 126         if depth and currentdepth >= depth:
 127             return ''
 128  
 129         # iterate over children, adding links to all of them
 130         result = []
 131         result.append('<ul>')
 132         children = _getPages(self.macro.request, '^%s/' % parent)
 133         for child in children:
 134             # display short page name, leaving out the parent path
 135             # (and make sure the name doesn't get wrapped)
 136             shortname = child[len(parent)+1:]
 137 
 138             if shortname.count('/') > 0:
 139                 shortname = re.sub('/.*$', '', shortname)
 140                 child = parent + '/' + shortname
 141 
 142             if parents.has_key(child):
 143                 continue
 144 
 145             result.append('<li>')
 146 
 147             if child == self.pagename:
 148                 # do not link to focus
 149                 result.append(self.macro.formatter.text(shortname))
 150             else:
 151                 # link to child
 152                 result.append(Page(self.macro.request, child).link_to(self.macro.request, text=shortname, querystr=self.querystr))
 153 
 154             result.append('</li>')
 155             result.append(self.do_tree(root=child, currentdepth=currentdepth+1, parents=parents))
 156 
 157         result.append('</ul>')
 158         return ''.join(result)
 159 
 160 
 161 def execute(macro, args):
 162     # get HTML code with the links
 163     navi = Navitree(macro, args or '').dispatch()
 164 
 165     if navi:
 166         return '%s' % navi
 167 
 168     # navigation disabled in plain print mode
 169     return ''

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] (2007-02-21 12:13:18, 5.5 KB) [[attachment:Navitree-GunnarWrobel.py]]
  • [get | view] (2006-04-18 15:27:52, 5.2 KB) [[attachment:Navitree.py]]
 All files | Selected Files: delete move to page copy to page

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