1.1 --- a/macros/CategoryMenu.py Sat Nov 01 21:13:16 2008 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,197 +0,0 @@
1.4 -# -*- coding: iso-8859-1 -*-
1.5 -"""
1.6 - MoinMoin - CategoryMenu Macro
1.7 -
1.8 - @copyright: 2008 by Paul Boddie <paul@boddie.org.uk>
1.9 - @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
1.10 - 2005-2008 MoinMoin:ThomasWaldmann.
1.11 - @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.12 -"""
1.13 -
1.14 -from MoinMoin.Page import Page
1.15 -from MoinMoin import wikiutil, search, version
1.16 -import re
1.17 -
1.18 -__version__ = "0.1"
1.19 -
1.20 -Dependencies = ['pages']
1.21 -
1.22 -# Regular expressions where MoinMoin does not provide the required support.
1.23 -
1.24 -category_regexp = None
1.25 -
1.26 -# Utility functions.
1.27 -
1.28 -def isMoin15():
1.29 - return version.release.startswith("1.5.")
1.30 -
1.31 -def getCategoryPattern(request):
1.32 - global category_regexp
1.33 -
1.34 - try:
1.35 - return request.cfg.cache.page_category_regexact
1.36 - except AttributeError:
1.37 -
1.38 - # Use regular expression from MoinMoin 1.7.1 otherwise.
1.39 -
1.40 - if category_regexp is None:
1.41 - category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE)
1.42 - return category_regexp
1.43 -
1.44 -# The main activity functions.
1.45 -
1.46 -def getCategories(request):
1.47 -
1.48 - """
1.49 - From the AdvancedSearch macro, return a list of category page names using
1.50 - the given 'request'.
1.51 - """
1.52 -
1.53 - # This will return all pages with "Category" in the title.
1.54 -
1.55 - cat_filter = getCategoryPattern(request).search
1.56 - pagenames = request.rootpage.getPageList(filter=cat_filter)
1.57 - pagenames.sort()
1.58 - return pagenames
1.59 -
1.60 -def getCategoryMapping(category_pagenames, request):
1.61 -
1.62 - """
1.63 - For the given 'category_pagenames' return a list of tuples of the form
1.64 - (category name, category page name) using the given 'request'.
1.65 - """
1.66 -
1.67 - cat_pattern = getCategoryPattern(request)
1.68 - mapping = []
1.69 - for pagename in category_pagenames:
1.70 - name = cat_pattern.match(pagename).group("key")
1.71 - if name != "Category":
1.72 - mapping.append((name, pagename))
1.73 - mapping.sort()
1.74 - return mapping
1.75 -
1.76 -def getPages(pagename, request):
1.77 -
1.78 - "Return the links minus category links for 'pagename' using the 'request'."
1.79 -
1.80 - query = search.QueryParser().parse_query('"%s"' % pagename)
1.81 - if isMoin15():
1.82 - results = search.searchPages(request, query)
1.83 - results.sortByPagename()
1.84 - else:
1.85 - results = search.searchPages(request, query, "page_name")
1.86 -
1.87 - cat_pattern = getCategoryPattern(request)
1.88 - pages = []
1.89 - for page in results.hits:
1.90 - if not cat_pattern.match(page.page_name):
1.91 - pages.append(page)
1.92 - return pages
1.93 -
1.94 -def execute(macro, args):
1.95 -
1.96 - """
1.97 - Execute the 'macro' with the given 'args': an optional list of selected
1.98 - category names (categories whose pages are to be shown).
1.99 - """
1.100 -
1.101 - request = macro.request
1.102 - fmt = macro.formatter
1.103 - page = fmt.page
1.104 -
1.105 - # Interpret the arguments.
1.106 -
1.107 - try:
1.108 - selected_category_names = wikiutil.parse_quoted_separated(args, name_value=False)
1.109 - except AttributeError:
1.110 - selected_category_names = args.split(",")
1.111 -
1.112 - selected_category_names = [arg for arg in selected_category_names if arg]
1.113 -
1.114 - # Get the categories.
1.115 -
1.116 - categories = getCategoryMapping(getCategories(request), request)
1.117 -
1.118 - # Generate a menu with the categories, together with expanded submenus for
1.119 - # the categories employed by the current page, the category represented by
1.120 - # the current page, or for those categories specified in the macro
1.121 - # arguments.
1.122 -
1.123 - output = []
1.124 - output.append(fmt.bullet_list(on=1, attr={"class" : "category-menu"}))
1.125 -
1.126 - for category in categories:
1.127 - category_name, category_pagename = category
1.128 -
1.129 - pages_in_category = getPages(category_pagename, request)
1.130 - pagenames_in_category = [p.page_name for p in pages_in_category]
1.131 - page_is_category = page.page_name == category_pagename
1.132 -
1.133 - # Generate the submenu where appropriate.
1.134 -
1.135 - if selected_category_names and category_name in selected_category_names or \
1.136 - not selected_category_names and (
1.137 - page_is_category or page.page_name in pagenames_in_category):
1.138 -
1.139 - if page_is_category:
1.140 - output.append(fmt.listitem(on=1, attr={"class" : "selected current"}))
1.141 - output.append(fmt.text(category_name))
1.142 - else:
1.143 - output.append(fmt.listitem(on=1, attr={"class" : "selected"}))
1.144 - output.append(fmt.pagelink(on=1, pagename=category_pagename))
1.145 - output.append(fmt.text(category_name))
1.146 - output.append(fmt.pagelink(on=0, pagename=category_pagename))
1.147 -
1.148 - output.append(fmt.bullet_list(on=1, attr={"class" : "category-submenu"}))
1.149 -
1.150 - # Visit each page in the category.
1.151 -
1.152 - last_parts = []
1.153 -
1.154 - for page_in_category in pages_in_category:
1.155 - pagename = page_in_category.page_name
1.156 -
1.157 - if page.page_name == pagename:
1.158 - output.append(fmt.listitem(on=1, attr={"class" : "selected"}))
1.159 - else:
1.160 - output.append(fmt.listitem(on=1))
1.161 - output.append(fmt.pagelink(on=1, pagename=pagename))
1.162 -
1.163 - # Abbreviate long hierarchical names.
1.164 -
1.165 - parts = pagename.split("/")
1.166 - common = 0
1.167 - for last, current in map(None, last_parts, parts):
1.168 - if last == current:
1.169 - common += 1
1.170 - else:
1.171 - break
1.172 -
1.173 - # Use an em-dash to indicate subpages.
1.174 -
1.175 - prefix = u"\u2014" * common
1.176 - suffix = "/".join(parts[common:])
1.177 -
1.178 - output.append(fmt.text("%s %s" % (prefix, suffix)))
1.179 - output.append(fmt.pagelink(on=0, pagename=pagename))
1.180 - output.append(fmt.listitem(on=0))
1.181 -
1.182 - last_parts = parts
1.183 -
1.184 - output.append(fmt.bullet_list(on=0))
1.185 - output.append(fmt.listitem(on=0))
1.186 -
1.187 - # Otherwise generate a simple link.
1.188 -
1.189 - else:
1.190 - output.append(fmt.listitem(on=1))
1.191 - output.append(fmt.pagelink(on=1, pagename=category_pagename))
1.192 - output.append(fmt.text(category_name))
1.193 - output.append(fmt.pagelink(on=0, pagename=category_pagename))
1.194 - output.append(fmt.listitem(on=0))
1.195 -
1.196 - output.append(fmt.bullet_list(on=0))
1.197 -
1.198 - return ''.join(output)
1.199 -
1.200 -# vim: tabstop=4 expandtab shiftwidth=4