/usr/share/pyshared/MoinMoin/macro/Navigation.py is in python-moinmoin 1.9.3-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | # -*- coding: iso-8859-1 -*-
"""
MoinMoin - Navigation Macro
@copyright: 2003 Juergen Hermann <jh@web.de>
2008 MoinMoin:RadomirDopieralski
@license: GNU GPL, see COPYING for details.
"""
import re
from MoinMoin.Page import Page
from MoinMoin import wikiutil
Dependencies = ["namespace"]
# helpers
#!!! refactor these to an util module?
def _getParent(pagename):
""" Return parent of pagename.
"""
pos = pagename.rfind('/')
if pos >= 0:
return pagename[:pos]
return None
def _getPages(request, filter_regex=None):
""" Return a (filtered) list of pages names.
"""
filterfn = None
if filter_regex:
filterfn = re.compile(filter_regex).match
pages = request.rootpage.getPageList(filter=filterfn)
pages.sort()
return pages
def _getLinks(request, pagename, filter_regex=None):
""" Return pagename for up, first, prev, next, last; each can be None.
"""
pos, first, prev, next, last = 0, None, None, None, None
all_pages = _getPages(request, filter_regex)
size = len(all_pages)
if all_pages:
try:
pos = all_pages.index(pagename)
except ValueError:
# this should never happen in theory, but let's be sure
pass
else:
if pos > 0:
first = all_pages[0]
prev = all_pages[pos-1]
if pos+1 < len(all_pages):
next = all_pages[pos+1]
last = all_pages[-1]
return pos, size, (first, prev, next, last)
class Navigation:
""" Dispatcher class implementing the navigation schemes.
"""
# querystring for slideshow links
PROJECTION = {'action': 'print', 'media': 'projection', }
def __init__(self, macro, scheme, depth):
""" Prepare common values used during processing.
"""
self.macro = macro
self.scheme = scheme
self.depth = depth
self._ = self.macro.request.getText
self.pagename = self.macro.formatter.page.page_name
self.print_mode = self.macro.request.action == 'print'
self.media = self.macro.request.values.get('media')
self.querystr = self.print_mode and self.PROJECTION or {}
def dispatch(self):
""" Return None if in plain print mode (no navigational
elements in printouts), else the proper HTML code.
"""
if self.print_mode and self.media != 'projection':
return None
return getattr(self, 'do_%s' % self.scheme, self.badscheme)()
def badscheme(self):
""" Bad scheme argument.
"""
_ = self._
return (self.macro.formatter.sysmsg(1) +
self.macro.formatter.text(
_("Unsupported navigation scheme '%(scheme)s'!") %
{'scheme': self.scheme}) +
self.macro.formatter.sysmsg(0))
def do_children(self):
""" Navigate to subpages from a parent page.
"""
# delegate to siblings code, setting the parent explicitely
return self.do_siblings(root=self.pagename)
def do_siblings(self, root=None):
""" Navigate from a subpage to its siblings.
"""
_ = self._
request = self.macro.request
# get parent page name
parent = root or _getParent(self.pagename)
if not parent:
return (self.macro.formatter.sysmsg(1) +
self.macro.formatter.text(_('No parent page found!'))+
self.macro.formatter.sysmsg(0))
# iterate over children, adding links to all of them
result = []
children = _getPages(request, '^%s/' % re.escape(parent))
for child in children:
# display short page name, leaving out the parent path
# (and make sure the name doesn't get wrapped)
shortname = child[len(parent):]
# possibly limit depth
if self.depth and shortname.count('/') > self.depth:
continue
if child == self.pagename:
# do not link to focus
result.append(self.macro.formatter.text(shortname))
else:
# link to sibling / child
result.append(Page(request, child).link_to(request, text=shortname, querystr=self.querystr))
result.append(' ')
return ''.join(result)
def do_slideshow(self, focus=None):
""" Slideshow master page links.
If `focus` is set, it is the name of a slide page; these only
get the mode toggle and edit links.
"""
_ = self._
curpage = focus or self.pagename
result = []
request = self.macro.request
pg = Page(request, curpage)
if self.print_mode:
# projection mode
label = _('Wiki')
toggle = {}
result.append(pg.link_to(request, text=_('Edit'), querystr={'action': 'edit'}))
result.append(' ')
else:
# wiki mode
label = _('Slideshow')
toggle = self.PROJECTION
# add mode toggle link
result.append(pg.link_to(request, text=label, querystr=toggle))
# leave out the following on slide pages
if focus is None:
children = _getPages(request, '^%s/' % re.escape(self.pagename))
if children:
# add link to first child if one exists
result.append(' ')
result.append(Page(request, children[0]).link_to(request, text=_('Start'), querystr=self.querystr))
return ''.join(result)
def do_slides(self, root=None):
""" Navigate within a slide show.
"""
_ = self._
request = self.macro.request
parent = root or _getParent(self.pagename)
if not parent:
return (self.macro.formatter.sysmsg(1) +
self.macro.formatter.text(_('No parent page found!')) +
self.macro.formatter.sysmsg(0))
# prepare link generation
result = []
labels = ['^', '|<', '<<', '>>', '>|']
filter_regex = '^%s/' % re.escape(parent)
pos, size, links = _getLinks(request, self.pagename, filter_regex)
pos += 1
links = zip(labels, (parent, ) + links)
# generate links to neighborhood
for label, name in links:
result.append(' ')
if name:
# active link
result.append(Page(request, name).link_to(request, text=label, querystr=self.querystr))
else:
# ghosted link
result.append(self.macro.formatter.text(label))
result.append(' ')
# position indicator in the middle
if label == labels[2]:
result.append(_('Slide %(pos)d of %(size)d') % {'pos': pos, 'size': size})
return self.do_slideshow(focus=self.pagename) + ''.join(result)
def macro_Navigation(macro,
scheme=wikiutil.required_arg((u'children', u'siblings',
u'slideshow', u'slides')),
depth=0):
# get HTML code with the links
navi = Navigation(macro, scheme, depth).dispatch()
if navi:
# return links packed into a table
return u'<table class="navigation"><tr><td>%s</td></tr></table>' % navi
# navigation disabled in plain print mode
return u''
|