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@6 | 15 | __version__ = "0.1" |
paul@6 | 16 | |
paul@6 | 17 | Dependencies = ['pages'] |
paul@6 | 18 | |
paul@6 | 19 | # Regular expressions where MoinMoin does not provide the required support. |
paul@6 | 20 | |
paul@0 | 21 | category_regexp = None |
paul@0 | 22 | |
paul@6 | 23 | # Utility functions. |
paul@0 | 24 | |
paul@0 | 25 | def isMoin15(): |
paul@0 | 26 | return version.release.startswith("1.5.") |
paul@0 | 27 | |
paul@0 | 28 | def getCategoryPattern(request): |
paul@0 | 29 | global category_regexp |
paul@0 | 30 | |
paul@0 | 31 | try: |
paul@0 | 32 | return request.cfg.cache.page_category_regexact |
paul@0 | 33 | except AttributeError: |
paul@0 | 34 | |
paul@0 | 35 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@0 | 36 | |
paul@0 | 37 | if category_regexp is None: |
paul@0 | 38 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@0 | 39 | return category_regexp |
paul@0 | 40 | |
paul@6 | 41 | # The main activity functions. |
paul@6 | 42 | |
paul@0 | 43 | def getCategories(request): |
paul@0 | 44 | |
paul@0 | 45 | """ |
paul@0 | 46 | From the AdvancedSearch macro, return a list of category page names using |
paul@0 | 47 | the given 'request'. |
paul@0 | 48 | """ |
paul@0 | 49 | |
paul@0 | 50 | # This will return all pages with "Category" in the title. |
paul@0 | 51 | |
paul@0 | 52 | cat_filter = getCategoryPattern(request).search |
paul@12 | 53 | return request.rootpage.getPageList(filter=cat_filter) |
paul@0 | 54 | |
paul@0 | 55 | def getCategoryMapping(category_pagenames, request): |
paul@0 | 56 | |
paul@0 | 57 | """ |
paul@0 | 58 | For the given 'category_pagenames' return a list of tuples of the form |
paul@0 | 59 | (category name, category page name) using the given 'request'. |
paul@0 | 60 | """ |
paul@0 | 61 | |
paul@0 | 62 | cat_pattern = getCategoryPattern(request) |
paul@0 | 63 | mapping = [] |
paul@0 | 64 | for pagename in category_pagenames: |
paul@0 | 65 | name = cat_pattern.match(pagename).group("key") |
paul@0 | 66 | if name != "Category": |
paul@0 | 67 | mapping.append((name, pagename)) |
paul@0 | 68 | mapping.sort() |
paul@0 | 69 | return mapping |
paul@0 | 70 | |
paul@0 | 71 | def getPages(pagename, request): |
paul@0 | 72 | |
paul@0 | 73 | "Return the links minus category links for 'pagename' using the 'request'." |
paul@0 | 74 | |
paul@12 | 75 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@0 | 76 | if isMoin15(): |
paul@0 | 77 | results = search.searchPages(request, query) |
paul@0 | 78 | results.sortByPagename() |
paul@0 | 79 | else: |
paul@0 | 80 | results = search.searchPages(request, query, "page_name") |
paul@0 | 81 | |
paul@0 | 82 | cat_pattern = getCategoryPattern(request) |
paul@0 | 83 | pages = [] |
paul@0 | 84 | for page in results.hits: |
paul@0 | 85 | if not cat_pattern.match(page.page_name): |
paul@0 | 86 | pages.append(page) |
paul@0 | 87 | return pages |
paul@0 | 88 | |
paul@0 | 89 | def execute(macro, args): |
paul@6 | 90 | |
paul@6 | 91 | """ |
paul@6 | 92 | Execute the 'macro' with the given 'args': an optional list of selected |
paul@6 | 93 | category names (categories whose pages are to be shown). |
paul@6 | 94 | """ |
paul@6 | 95 | |
paul@0 | 96 | request = macro.request |
paul@0 | 97 | fmt = macro.formatter |
paul@0 | 98 | page = fmt.page |
paul@0 | 99 | |
paul@0 | 100 | # Interpret the arguments. |
paul@0 | 101 | |
paul@0 | 102 | try: |
paul@0 | 103 | selected_category_names = wikiutil.parse_quoted_separated(args, name_value=False) |
paul@0 | 104 | except AttributeError: |
paul@0 | 105 | selected_category_names = args.split(",") |
paul@0 | 106 | |
paul@0 | 107 | selected_category_names = [arg for arg in selected_category_names if arg] |
paul@0 | 108 | |
paul@0 | 109 | # Get the categories. |
paul@0 | 110 | |
paul@0 | 111 | categories = getCategoryMapping(getCategories(request), request) |
paul@0 | 112 | |
paul@0 | 113 | # Generate a menu with the categories, together with expanded submenus for |
paul@2 | 114 | # the categories employed by the current page, the category represented by |
paul@2 | 115 | # the current page, or for those categories specified in the macro |
paul@0 | 116 | # arguments. |
paul@0 | 117 | |
paul@0 | 118 | output = [] |
paul@0 | 119 | output.append(fmt.bullet_list(on=1, attr={"class" : "category-menu"})) |
paul@0 | 120 | |
paul@0 | 121 | for category in categories: |
paul@0 | 122 | category_name, category_pagename = category |
paul@0 | 123 | |
paul@0 | 124 | pages_in_category = getPages(category_pagename, request) |
paul@0 | 125 | pagenames_in_category = [p.page_name for p in pages_in_category] |
paul@2 | 126 | page_is_category = page.page_name == category_pagename |
paul@0 | 127 | |
paul@0 | 128 | # Generate the submenu where appropriate. |
paul@0 | 129 | |
paul@0 | 130 | if selected_category_names and category_name in selected_category_names or \ |
paul@2 | 131 | not selected_category_names and ( |
paul@2 | 132 | page_is_category or page.page_name in pagenames_in_category): |
paul@0 | 133 | |
paul@2 | 134 | if page_is_category: |
paul@2 | 135 | output.append(fmt.listitem(on=1, attr={"class" : "selected current"})) |
paul@2 | 136 | output.append(fmt.text(category_name)) |
paul@2 | 137 | else: |
paul@2 | 138 | output.append(fmt.listitem(on=1, attr={"class" : "selected"})) |
paul@2 | 139 | output.append(fmt.pagelink(on=1, pagename=category_pagename)) |
paul@2 | 140 | output.append(fmt.text(category_name)) |
paul@2 | 141 | output.append(fmt.pagelink(on=0, pagename=category_pagename)) |
paul@0 | 142 | |
paul@0 | 143 | output.append(fmt.bullet_list(on=1, attr={"class" : "category-submenu"})) |
paul@0 | 144 | |
paul@0 | 145 | # Visit each page in the category. |
paul@0 | 146 | |
paul@5 | 147 | last_parts = [] |
paul@5 | 148 | |
paul@0 | 149 | for page_in_category in pages_in_category: |
paul@0 | 150 | pagename = page_in_category.page_name |
paul@0 | 151 | |
paul@1 | 152 | if page.page_name == pagename: |
paul@2 | 153 | output.append(fmt.listitem(on=1, attr={"class" : "selected"})) |
paul@1 | 154 | else: |
paul@1 | 155 | output.append(fmt.listitem(on=1)) |
paul@0 | 156 | output.append(fmt.pagelink(on=1, pagename=pagename)) |
paul@5 | 157 | |
paul@5 | 158 | # Abbreviate long hierarchical names. |
paul@5 | 159 | |
paul@5 | 160 | parts = pagename.split("/") |
paul@5 | 161 | common = 0 |
paul@5 | 162 | for last, current in map(None, last_parts, parts): |
paul@5 | 163 | if last == current: |
paul@5 | 164 | common += 1 |
paul@5 | 165 | else: |
paul@5 | 166 | break |
paul@5 | 167 | |
paul@6 | 168 | # Use an em-dash to indicate subpages. |
paul@6 | 169 | |
paul@5 | 170 | prefix = u"\u2014" * common |
paul@5 | 171 | suffix = "/".join(parts[common:]) |
paul@5 | 172 | |
paul@5 | 173 | output.append(fmt.text("%s %s" % (prefix, suffix))) |
paul@0 | 174 | output.append(fmt.pagelink(on=0, pagename=pagename)) |
paul@0 | 175 | output.append(fmt.listitem(on=0)) |
paul@0 | 176 | |
paul@5 | 177 | last_parts = parts |
paul@5 | 178 | |
paul@0 | 179 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 180 | output.append(fmt.listitem(on=0)) |
paul@0 | 181 | |
paul@0 | 182 | # Otherwise generate a simple link. |
paul@0 | 183 | |
paul@0 | 184 | else: |
paul@2 | 185 | output.append(fmt.listitem(on=1)) |
paul@0 | 186 | output.append(fmt.pagelink(on=1, pagename=category_pagename)) |
paul@0 | 187 | output.append(fmt.text(category_name)) |
paul@0 | 188 | output.append(fmt.pagelink(on=0, pagename=category_pagename)) |
paul@0 | 189 | output.append(fmt.listitem(on=0)) |
paul@0 | 190 | |
paul@0 | 191 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 192 | |
paul@0 | 193 | return ''.join(output) |
paul@0 | 194 | |
paul@0 | 195 | # vim: tabstop=4 expandtab shiftwidth=4 |