00001
00002 """
00003 MoinMoin - Navigation Macro
00004
00005 @copyright: 2003 by Jürgen Hermann <jh@web.de>
00006 @license: GNU GPL, see COPYING for details.
00007 """
00008
00009 import re, string
00010 from MoinMoin import config
00011
00012 from MoinMoin.Page import Page
00013
00014 from MoinMoin import log
00015 logging = log.getLogger(__name__)
00016
00017 Dependencies = ["namespace"]
00018
00019 from MoinMoin.formatter.pagelinks import Formatter
00020
00021 class MyFormatter(Formatter):
00022 """ Collect pagelinks and format nothing :-) """
00023 def __init__(self, request, store_pagelinks):
00024 Formatter.__init__(self, request, store_pagelinks=store_pagelinks)
00025
00026 self._text = []
00027
00028 def pagelink(self, on, pagename='', page=None, **kw):
00029
00030
00031 if not self._store_pagelinks or on or kw.get('generated'):
00032 self._text = []
00033 return self.null()
00034
00035 if not pagename and page:
00036 pagename = page.page_name
00037 pagename = self.request.normalizePagename(pagename)
00038 if pagename and pagename not in self.pagelinks:
00039 if self._text:
00040 linktext = string.join(self._text)
00041 else:
00042 linktext = 'Link'
00043
00044 self.pagelinks.append((pagename, linktext))
00045
00046 self._text = []
00047
00048 return self.null()
00049
00050 def null(self, *args, **kw):
00051 return ''
00052
00053
00054
00055
00056 set_highlight_re = rawHTML = url = image = smiley = text = null
00057 strong = emphasis = underline = highlight = sup = sub = strike = null
00058 code = preformatted = small = big = code_area = code_line = null
00059 code_token = linebreak = paragraph = rule = icon = null
00060 number_list = bullet_list = listitem = definition_list = null
00061 definition_term = definition_desc = heading = table = null
00062 table_row = table_cell = attachment_link = attachment_image = attachment_drawing = null
00063 transclusion = transclusion_param = null
00064
00065 def text(self, s, **kw):
00066 self._text.append(s)
00067 return ''
00068
00069
00070
00071 class MyPage(Page):
00072 def myparsePageLinks(self, request):
00073 pagename = self.page_name
00074 if request.parsePageLinks_running.get(pagename, False):
00075
00076 return []
00077
00078
00079
00080 request.parsePageLinks_running[pagename] = True
00081
00082 request.clock.start('parsePageLinks')
00083
00084 class Null:
00085 def write(self, data):
00086 pass
00087
00088 request.redirect(Null())
00089 request.mode_getpagelinks += 1
00090 try:
00091 try:
00092 formatter = MyFormatter(request, store_pagelinks=1)
00093 page = MyPage(request, pagename, formatter=formatter)
00094 page.send_page(content_only=1)
00095 except:
00096 logging.exception("pagelinks formatter failed, traceback follows")
00097 finally:
00098 request.mode_getpagelinks -= 1
00099
00100 request.redirect()
00101 if hasattr(request, '_fmt_hd_counters'):
00102 del request._fmt_hd_counters
00103 request.clock.stop('parsePageLinks')
00104 return formatter.pagelinks
00105
00106
00107 def _getPages(request, pagename):
00108 """ Return a (filtered) list of pages names.
00109 """
00110 page = MyPage(request, pagename)
00111
00112
00113 links = page.myparsePageLinks(request)
00114 return links
00115
00116 class Navigation:
00117 """ Dispatcher class implementing the navigation schemes.
00118 """
00119
00120
00121 PROJECTION = 'action=print&media=projection'
00122
00123 def __init__(self, macro, args):
00124 """ Prepare common values used during processing.
00125 """
00126 self.macro = macro
00127 self.args = args.split(',')
00128 self._ = self.macro.request.getText
00129
00130 self.pagename = self.macro.formatter.page.page_name
00131 self.print_mode = self.macro.request.form.has_key('action') \
00132 and self.macro.request.form['action'][0] == 'print'
00133 self.media = self.macro.request.form.get('media', [None])[0]
00134 self.querystr = self.print_mode and self.PROJECTION or ''
00135
00136 def dispatch(self):
00137 """ Return None if in plain print mode (no navigational
00138 elements in printouts), else the proper HTML code.
00139 """
00140
00141 return self.do_siblings(self.args[0])
00142
00143
00144 def do_siblings(self, parentPage, root=None):
00145 """ Navigate from a subpage to its siblings.
00146 """
00147 _ = self._
00148
00149
00150 result = []
00151 children = _getPages(self.macro.request, parentPage)
00152 for child, linktext in children:
00153
00154
00155 if child.startswith("Category"): continue
00156 shortname = child
00157 if child.startswith(parentPage):
00158 shortname = child[len(parentPage):]
00159
00160 parts = string.split(child, "/")
00161
00162 if linktext:
00163 shortname = linktext
00164 else:
00165 shortname = parts[-1]
00166
00167 if not shortname: continue
00168
00169 if child == self.pagename:
00170
00171 result.append(self.macro.formatter.text(shortname))
00172 else:
00173
00174 result.append(Page(self.macro.request, child).link_to(self.macro.request, text=shortname))
00175
00176 if len(result) >= 2:
00177 str = self.macro.formatter.strong(1) + self.macro.formatter.emphasis(1) + result[0] + self.macro.formatter.strong(0) + ": " + ' | '.join(result[1:]) + self.macro.formatter.emphasis(0)
00178 else:
00179 str = ' | '.join(result)
00180
00181
00182 if len(str) > 0:
00183 str = self.macro.formatter.span(1, css_class="macro-nav-menu") + str + self.macro.formatter.span(0) + "\n"
00184
00185 return str
00186
00187
00188
00189
00190 def execute(macro, args):
00191
00192 navi = Navigation(macro, args or '').dispatch()
00193
00194 if navi:
00195
00196 return navi
00197
00198
00199 return ''
00200