paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - CategoryMenu Macro |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2008 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@0 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@0 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 9 | """ |
paul@0 | 10 | |
paul@0 | 11 | from MoinMoin.Page import Page |
paul@0 | 12 | from MoinMoin import wikiutil, search, version |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@0 | 15 | category_regexp = None |
paul@0 | 16 | |
paul@0 | 17 | Dependencies = ['pages'] |
paul@0 | 18 | |
paul@0 | 19 | def isMoin15(): |
paul@0 | 20 | return version.release.startswith("1.5.") |
paul@0 | 21 | |
paul@0 | 22 | def getCategoryPattern(request): |
paul@0 | 23 | global category_regexp |
paul@0 | 24 | |
paul@0 | 25 | try: |
paul@0 | 26 | return request.cfg.cache.page_category_regexact |
paul@0 | 27 | except AttributeError: |
paul@0 | 28 | |
paul@0 | 29 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@0 | 30 | |
paul@0 | 31 | if category_regexp is None: |
paul@0 | 32 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@0 | 33 | return category_regexp |
paul@0 | 34 | |
paul@0 | 35 | def getCategories(request): |
paul@0 | 36 | |
paul@0 | 37 | """ |
paul@0 | 38 | From the AdvancedSearch macro, return a list of category page names using |
paul@0 | 39 | the given 'request'. |
paul@0 | 40 | """ |
paul@0 | 41 | |
paul@0 | 42 | # This will return all pages with "Category" in the title. |
paul@0 | 43 | |
paul@0 | 44 | cat_filter = getCategoryPattern(request).search |
paul@0 | 45 | pagenames = request.rootpage.getPageList(filter=cat_filter) |
paul@0 | 46 | pagenames.sort() |
paul@0 | 47 | return pagenames |
paul@0 | 48 | |
paul@0 | 49 | def getCategoryMapping(category_pagenames, request): |
paul@0 | 50 | |
paul@0 | 51 | """ |
paul@0 | 52 | For the given 'category_pagenames' return a list of tuples of the form |
paul@0 | 53 | (category name, category page name) using the given 'request'. |
paul@0 | 54 | """ |
paul@0 | 55 | |
paul@0 | 56 | cat_pattern = getCategoryPattern(request) |
paul@0 | 57 | mapping = [] |
paul@0 | 58 | for pagename in category_pagenames: |
paul@0 | 59 | name = cat_pattern.match(pagename).group("key") |
paul@0 | 60 | if name != "Category": |
paul@0 | 61 | mapping.append((name, pagename)) |
paul@0 | 62 | mapping.sort() |
paul@0 | 63 | return mapping |
paul@0 | 64 | |
paul@0 | 65 | def getPages(pagename, request): |
paul@0 | 66 | |
paul@0 | 67 | "Return the links minus category links for 'pagename' using the 'request'." |
paul@0 | 68 | |
paul@0 | 69 | query = search.QueryParser().parse_query('"%s"' % pagename) |
paul@0 | 70 | if isMoin15(): |
paul@0 | 71 | results = search.searchPages(request, query) |
paul@0 | 72 | results.sortByPagename() |
paul@0 | 73 | else: |
paul@0 | 74 | results = search.searchPages(request, query, "page_name") |
paul@0 | 75 | |
paul@0 | 76 | cat_pattern = getCategoryPattern(request) |
paul@0 | 77 | pages = [] |
paul@0 | 78 | for page in results.hits: |
paul@0 | 79 | if not cat_pattern.match(page.page_name): |
paul@0 | 80 | pages.append(page) |
paul@0 | 81 | return pages |
paul@0 | 82 | |
paul@0 | 83 | def execute(macro, args): |
paul@0 | 84 | request = macro.request |
paul@0 | 85 | fmt = macro.formatter |
paul@0 | 86 | page = fmt.page |
paul@0 | 87 | |
paul@0 | 88 | # Interpret the arguments. |
paul@0 | 89 | |
paul@0 | 90 | try: |
paul@0 | 91 | selected_category_names = wikiutil.parse_quoted_separated(args, name_value=False) |
paul@0 | 92 | except AttributeError: |
paul@0 | 93 | selected_category_names = args.split(",") |
paul@0 | 94 | |
paul@0 | 95 | selected_category_names = [arg for arg in selected_category_names if arg] |
paul@0 | 96 | |
paul@0 | 97 | # Get the categories. |
paul@0 | 98 | |
paul@0 | 99 | categories = getCategoryMapping(getCategories(request), request) |
paul@0 | 100 | |
paul@0 | 101 | # Generate a menu with the categories, together with expanded submenus for |
paul@0 | 102 | # the categories employed by the current page or as requested in the macro |
paul@0 | 103 | # arguments. |
paul@0 | 104 | |
paul@0 | 105 | output = [] |
paul@0 | 106 | output.append(fmt.bullet_list(on=1, attr={"class" : "category-menu"})) |
paul@0 | 107 | |
paul@0 | 108 | for category in categories: |
paul@0 | 109 | category_name, category_pagename = category |
paul@0 | 110 | |
paul@0 | 111 | pages_in_category = getPages(category_pagename, request) |
paul@0 | 112 | pagenames_in_category = [p.page_name for p in pages_in_category] |
paul@0 | 113 | |
paul@0 | 114 | # Generate the submenu where appropriate. |
paul@0 | 115 | |
paul@0 | 116 | if selected_category_names and category_name in selected_category_names or \ |
paul@0 | 117 | not selected_category_names and page.page_name in pagenames_in_category: |
paul@0 | 118 | |
paul@0 | 119 | output.append(fmt.listitem(on=1, attr={"class" : "category-menu-item-selected"})) |
paul@0 | 120 | output.append(fmt.text(category_name)) |
paul@0 | 121 | |
paul@0 | 122 | output.append(fmt.bullet_list(on=1, attr={"class" : "category-submenu"})) |
paul@0 | 123 | |
paul@0 | 124 | # Visit each page in the category. |
paul@0 | 125 | |
paul@0 | 126 | for page_in_category in pages_in_category: |
paul@0 | 127 | pagename = page_in_category.page_name |
paul@0 | 128 | |
paul@0 | 129 | output.append(fmt.listitem(on=1)) |
paul@0 | 130 | output.append(fmt.pagelink(on=1, pagename=pagename)) |
paul@0 | 131 | output.append(fmt.text(pagename)) |
paul@0 | 132 | output.append(fmt.pagelink(on=0, pagename=pagename)) |
paul@0 | 133 | output.append(fmt.listitem(on=0)) |
paul@0 | 134 | |
paul@0 | 135 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 136 | output.append(fmt.listitem(on=0)) |
paul@0 | 137 | |
paul@0 | 138 | # Otherwise generate a simple link. |
paul@0 | 139 | |
paul@0 | 140 | else: |
paul@0 | 141 | output.append(fmt.listitem(on=1, attr={"class" : "category-menu-item"})) |
paul@0 | 142 | output.append(fmt.pagelink(on=1, pagename=category_pagename)) |
paul@0 | 143 | output.append(fmt.text(category_name)) |
paul@0 | 144 | output.append(fmt.pagelink(on=0, pagename=category_pagename)) |
paul@0 | 145 | output.append(fmt.listitem(on=0)) |
paul@0 | 146 | |
paul@0 | 147 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 148 | |
paul@0 | 149 | return ''.join(output) |
paul@0 | 150 | |
paul@0 | 151 | # vim: tabstop=4 expandtab shiftwidth=4 |