Attachment 'Navitree-GunnarWrobel.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 try:
106 depth = int(self.args[1])
107 except (IndexError, TypeError, ValueError):
108 depth = 0
109 return self.do_tree(root=self.pagename, currentdepth=0, parents={}, depth = depth)
110
111 def do_selectedtree(self):
112 page = self.args[1]
113 try:
114 depth = int(self.args[2])
115 except (IndexError, TypeError, ValueError):
116 depth = 0
117 return self.do_tree(root=page, currentdepth=0, parents={}, depth = depth)
118
119 def do_tree(self, root=None, currentdepth=0, parents={}, depth = 0):
120 """ Navigate to subpages from a parent page.
121 """
122 _ = self._
123
124 # get parent page name if no root was specified
125 parent = root or _getParent(self.pagename)
126 if not parent:
127 return (self.macro.formatter.sysmsg(1) +
128 self.macro.formatter.text(_('No parent page found!'))+
129 self.macro.formatter.sysmsg(0))
130 else:
131 parents[parent] = 1
132
133 # limit depth if a depth was specified
134 if depth and currentdepth >= depth:
135 return ''
136
137 # iterate over children, adding links to all of them
138 result = []
139 result.append('<ul>')
140 children = _getPages(self.macro.request, '^%s/' % parent)
141 for child in children:
142 # display short page name, leaving out the parent path
143 # (and make sure the name doesn't get wrapped)
144 shortname = child[len(parent)+1:]
145
146 if shortname.count('/') > 0:
147 shortname = re.sub('/.*$', '', shortname)
148 child = parent + '/' + shortname
149
150 if parents.has_key(child):
151 continue
152
153 result.append('<li>')
154
155 if child == self.pagename:
156 # do not link to focus
157 result.append(self.macro.formatter.text(shortname))
158 else:
159 # link to child
160 result.append(Page(self.macro.request, child).link_to(self.macro.request, text=shortname, querystr=self.querystr))
161
162 result.append('</li>')
163 result.append(self.do_tree(root=child, currentdepth=currentdepth+1, parents=parents, depth = depth))
164
165 result.append('</ul>')
166 return ''.join(result)
167
168
169 def execute(macro, args):
170 # get HTML code with the links
171 navi = Navitree(macro, args or '').dispatch()
172
173 if navi:
174 return '%s' % navi
175
176 # navigation disabled in plain print mode
177 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.You are not allowed to attach a file to this page.