paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - CategoryMenu Macro |
paul@0 | 4 | |
paul@14 | 5 | @copyright: 2008, 2009 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@16 | 15 | __version__ = "0.3" |
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@13 | 23 | # From MoinMoin.search.queryparser... |
paul@13 | 24 | |
paul@13 | 25 | category_membership_regexp_str = r'(?m)(^-----*\s*\r?\n)(^##.*\r?\n)*^(?!##)(.*)\b%s\b' |
paul@13 | 26 | |
paul@6 | 27 | # Utility functions. |
paul@0 | 28 | |
paul@0 | 29 | def isMoin15(): |
paul@0 | 30 | return version.release.startswith("1.5.") |
paul@0 | 31 | |
paul@0 | 32 | def getCategoryPattern(request): |
paul@0 | 33 | global category_regexp |
paul@0 | 34 | |
paul@0 | 35 | try: |
paul@0 | 36 | return request.cfg.cache.page_category_regexact |
paul@0 | 37 | except AttributeError: |
paul@0 | 38 | |
paul@0 | 39 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@0 | 40 | |
paul@0 | 41 | if category_regexp is None: |
paul@0 | 42 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@0 | 43 | return category_regexp |
paul@0 | 44 | |
paul@6 | 45 | # The main activity functions. |
paul@6 | 46 | |
paul@0 | 47 | def getCategories(request): |
paul@0 | 48 | |
paul@0 | 49 | """ |
paul@0 | 50 | From the AdvancedSearch macro, return a list of category page names using |
paul@0 | 51 | the given 'request'. |
paul@0 | 52 | """ |
paul@0 | 53 | |
paul@0 | 54 | # This will return all pages with "Category" in the title. |
paul@0 | 55 | |
paul@0 | 56 | cat_filter = getCategoryPattern(request).search |
paul@12 | 57 | return request.rootpage.getPageList(filter=cat_filter) |
paul@0 | 58 | |
paul@0 | 59 | def getCategoryMapping(category_pagenames, request): |
paul@0 | 60 | |
paul@0 | 61 | """ |
paul@0 | 62 | For the given 'category_pagenames' return a list of tuples of the form |
paul@0 | 63 | (category name, category page name) using the given 'request'. |
paul@0 | 64 | """ |
paul@0 | 65 | |
paul@0 | 66 | cat_pattern = getCategoryPattern(request) |
paul@0 | 67 | mapping = [] |
paul@0 | 68 | for pagename in category_pagenames: |
paul@0 | 69 | name = cat_pattern.match(pagename).group("key") |
paul@0 | 70 | if name != "Category": |
paul@0 | 71 | mapping.append((name, pagename)) |
paul@0 | 72 | mapping.sort() |
paul@0 | 73 | return mapping |
paul@0 | 74 | |
paul@0 | 75 | def getPages(pagename, request): |
paul@0 | 76 | |
paul@0 | 77 | "Return the links minus category links for 'pagename' using the 'request'." |
paul@0 | 78 | |
paul@12 | 79 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@0 | 80 | if isMoin15(): |
paul@0 | 81 | results = search.searchPages(request, query) |
paul@0 | 82 | results.sortByPagename() |
paul@0 | 83 | else: |
paul@0 | 84 | results = search.searchPages(request, query, "page_name") |
paul@0 | 85 | |
paul@0 | 86 | cat_pattern = getCategoryPattern(request) |
paul@0 | 87 | pages = [] |
paul@0 | 88 | for page in results.hits: |
paul@0 | 89 | if not cat_pattern.match(page.page_name): |
paul@0 | 90 | pages.append(page) |
paul@0 | 91 | return pages |
paul@0 | 92 | |
paul@16 | 93 | def getPrettyPageName(request, page): |
paul@16 | 94 | |
paul@16 | 95 | """ |
paul@16 | 96 | Using 'request', return a nicely formatted title/name for the given 'page'. |
paul@16 | 97 | """ |
paul@16 | 98 | |
paul@16 | 99 | if isMoin15(): |
paul@16 | 100 | title = page.split_title(request, force=1) |
paul@16 | 101 | else: |
paul@16 | 102 | title = page.split_title(force=1) |
paul@16 | 103 | |
paul@16 | 104 | return title.replace("_", " ").replace("/", u" ? ") |
paul@16 | 105 | |
paul@16 | 106 | def linkToPage(request, page, text, query_string=None): |
paul@14 | 107 | |
paul@16 | 108 | """ |
paul@16 | 109 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@16 | 110 | optional 'query_string'. |
paul@16 | 111 | """ |
paul@16 | 112 | |
paul@16 | 113 | text = wikiutil.escape(text) |
paul@14 | 114 | |
paul@16 | 115 | if isMoin15(): |
paul@16 | 116 | url = wikiutil.quoteWikinameURL(page.page_name) |
paul@16 | 117 | if query_string is not None: |
paul@16 | 118 | url = "%s?%s" % (url, query_string) |
paul@16 | 119 | return wikiutil.link_tag(request, url, text, getattr(page, "formatter", None)) |
paul@16 | 120 | else: |
paul@16 | 121 | return page.link_to_raw(request, text, query_string) |
paul@14 | 122 | |
paul@0 | 123 | def execute(macro, args): |
paul@6 | 124 | |
paul@6 | 125 | """ |
paul@6 | 126 | Execute the 'macro' with the given 'args': an optional list of selected |
paul@6 | 127 | category names (categories whose pages are to be shown). |
paul@6 | 128 | """ |
paul@6 | 129 | |
paul@0 | 130 | request = macro.request |
paul@0 | 131 | fmt = macro.formatter |
paul@0 | 132 | page = fmt.page |
paul@0 | 133 | |
paul@0 | 134 | # Interpret the arguments. |
paul@0 | 135 | |
paul@0 | 136 | try: |
paul@14 | 137 | selected_category_names = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 138 | except AttributeError: |
paul@0 | 139 | selected_category_names = args.split(",") |
paul@0 | 140 | |
paul@0 | 141 | selected_category_names = [arg for arg in selected_category_names if arg] |
paul@0 | 142 | |
paul@0 | 143 | # Get the categories. |
paul@0 | 144 | |
paul@0 | 145 | categories = getCategoryMapping(getCategories(request), request) |
paul@0 | 146 | |
paul@0 | 147 | # Generate a menu with the categories, together with expanded submenus for |
paul@2 | 148 | # the categories employed by the current page, the category represented by |
paul@2 | 149 | # the current page, or for those categories specified in the macro |
paul@0 | 150 | # arguments. |
paul@0 | 151 | |
paul@0 | 152 | output = [] |
paul@0 | 153 | output.append(fmt.bullet_list(on=1, attr={"class" : "category-menu"})) |
paul@0 | 154 | |
paul@0 | 155 | for category in categories: |
paul@0 | 156 | category_name, category_pagename = category |
paul@0 | 157 | |
paul@14 | 158 | # Work out whether the current page belongs to this category. |
paul@14 | 159 | |
paul@2 | 160 | page_is_category = page.page_name == category_pagename |
paul@13 | 161 | category_membership_regexp = re.compile(category_membership_regexp_str % category_pagename) |
paul@14 | 162 | page_is_in_category = category_membership_regexp.search(page.get_raw_body()) |
paul@0 | 163 | |
paul@0 | 164 | # Generate the submenu where appropriate. |
paul@0 | 165 | |
paul@0 | 166 | if selected_category_names and category_name in selected_category_names or \ |
paul@14 | 167 | not selected_category_names and (page_is_category or page_is_in_category): |
paul@0 | 168 | |
paul@2 | 169 | if page_is_category: |
paul@2 | 170 | output.append(fmt.listitem(on=1, attr={"class" : "selected current"})) |
paul@2 | 171 | output.append(fmt.text(category_name)) |
paul@2 | 172 | else: |
paul@2 | 173 | output.append(fmt.listitem(on=1, attr={"class" : "selected"})) |
paul@2 | 174 | output.append(fmt.pagelink(on=1, pagename=category_pagename)) |
paul@2 | 175 | output.append(fmt.text(category_name)) |
paul@2 | 176 | output.append(fmt.pagelink(on=0, pagename=category_pagename)) |
paul@0 | 177 | |
paul@0 | 178 | output.append(fmt.bullet_list(on=1, attr={"class" : "category-submenu"})) |
paul@0 | 179 | |
paul@13 | 180 | # Get the pages and page names in the category. |
paul@13 | 181 | |
paul@13 | 182 | pages_in_category = getPages(category_pagename, request) |
paul@13 | 183 | |
paul@0 | 184 | # Visit each page in the category. |
paul@0 | 185 | |
paul@5 | 186 | last_parts = [] |
paul@5 | 187 | |
paul@0 | 188 | for page_in_category in pages_in_category: |
paul@0 | 189 | pagename = page_in_category.page_name |
paul@0 | 190 | |
paul@14 | 191 | # Get a real page, not a result page. |
paul@14 | 192 | |
paul@14 | 193 | real_page_in_category = Page(request, pagename) |
paul@14 | 194 | |
paul@14 | 195 | # Get a pretty version of the page name. |
paul@16 | 196 | # NOTE: MoinMoin 1.5: request supplied. |
paul@14 | 197 | |
paul@16 | 198 | pretty_pagename = getPrettyPageName(request, real_page_in_category) |
paul@14 | 199 | |
paul@1 | 200 | if page.page_name == pagename: |
paul@2 | 201 | output.append(fmt.listitem(on=1, attr={"class" : "selected"})) |
paul@1 | 202 | else: |
paul@1 | 203 | output.append(fmt.listitem(on=1)) |
paul@5 | 204 | |
paul@5 | 205 | # Abbreviate long hierarchical names. |
paul@5 | 206 | |
paul@16 | 207 | parts = pretty_pagename.split(u" ? ") |
paul@5 | 208 | common = 0 |
paul@5 | 209 | for last, current in map(None, last_parts, parts): |
paul@5 | 210 | if last == current: |
paul@5 | 211 | common += 1 |
paul@5 | 212 | else: |
paul@5 | 213 | break |
paul@5 | 214 | |
paul@16 | 215 | # Use the arrows to indicate subpages. |
paul@6 | 216 | |
paul@16 | 217 | prefix = u" ? " * common |
paul@16 | 218 | suffix = u" ? ".join(parts[common:]) |
paul@5 | 219 | |
paul@14 | 220 | output.append(fmt.text(prefix)) |
paul@14 | 221 | |
paul@14 | 222 | # Link to the page using the pretty name. |
paul@14 | 223 | |
paul@16 | 224 | output.append(linkToPage(request, real_page_in_category, suffix)) |
paul@0 | 225 | output.append(fmt.listitem(on=0)) |
paul@0 | 226 | |
paul@5 | 227 | last_parts = parts |
paul@5 | 228 | |
paul@0 | 229 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 230 | output.append(fmt.listitem(on=0)) |
paul@0 | 231 | |
paul@0 | 232 | # Otherwise generate a simple link. |
paul@0 | 233 | |
paul@0 | 234 | else: |
paul@2 | 235 | output.append(fmt.listitem(on=1)) |
paul@0 | 236 | output.append(fmt.pagelink(on=1, pagename=category_pagename)) |
paul@0 | 237 | output.append(fmt.text(category_name)) |
paul@0 | 238 | output.append(fmt.pagelink(on=0, pagename=category_pagename)) |
paul@0 | 239 | output.append(fmt.listitem(on=0)) |
paul@0 | 240 | |
paul@0 | 241 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 242 | |
paul@0 | 243 | return ''.join(output) |
paul@0 | 244 | |
paul@0 | 245 | # vim: tabstop=4 expandtab shiftwidth=4 |