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