paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - ImprovedMoinSearch library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2010 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | from MoinMoin.search import searchPages |
paul@0 | 10 | from MoinMoin.Page import Page |
paul@1 | 11 | from MoinMoin import wikiutil |
paul@0 | 12 | import re |
paul@0 | 13 | |
paul@0 | 14 | heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE) |
paul@3 | 15 | paragraph_regexp = re.compile(r"(?P<paragraph>(?:^[^#=\s].*$\n)+)", re.UNICODE | re.MULTILINE) |
paul@3 | 16 | |
paul@3 | 17 | def range_groups(min_name, max_name): |
paul@3 | 18 | return r"(?P<%s>\d+)?(?:\s*-\s*(?P<%s>\d+))?" % (min_name, max_name) |
paul@3 | 19 | |
paul@3 | 20 | format_options_regexp = re.compile(r"(" |
paul@3 | 21 | "(?P<heading>(heading|title|h)\s*" + range_groups("min_heading", "max_heading") + ")" |
paul@3 | 22 | "|(?P<paragraph>(paragraph|para|p)\s*(?P<paragraph_number>\d+)?)" |
paul@3 | 23 | ")", re.UNICODE) |
paul@0 | 24 | |
paul@0 | 25 | def getSearchResultPages(request, query, **kw): |
paul@0 | 26 | |
paul@0 | 27 | """ |
paul@0 | 28 | Return matching pages using the given 'request' and search 'query'. Optional |
paul@0 | 29 | keyword arguments are passed to the underlying search infrastructure. |
paul@0 | 30 | """ |
paul@0 | 31 | |
paul@0 | 32 | results = searchPages(request, query, **kw) |
paul@0 | 33 | return results.hits |
paul@0 | 34 | |
paul@4 | 35 | def getFirstPageHeading(request, page, start=0, min_level=None, max_level=None): |
paul@0 | 36 | |
paul@0 | 37 | """ |
paul@0 | 38 | Using the given 'request', return the first heading in the given 'page' |
paul@4 | 39 | from the given 'start' point (optional, defaulting to the start of the page) |
paul@3 | 40 | having a heading level of at least 'min_level' (which is undefined if not |
paul@3 | 41 | specified) and at most 'max_level' (which is undefined if not specified). |
paul@4 | 42 | |
paul@4 | 43 | A tuple containing the heading and the span (the start offset and the end |
paul@4 | 44 | offset as a tuple) is returned for a successful retrieval. Otherwise, None |
paul@4 | 45 | is returned. |
paul@0 | 46 | """ |
paul@0 | 47 | |
paul@0 | 48 | full_page = Page(request, page.page_name) |
paul@0 | 49 | body = full_page.get_raw_body() |
paul@4 | 50 | if start != 0: |
paul@4 | 51 | body = body[start:] |
paul@0 | 52 | |
paul@0 | 53 | for match in heading_regexp.finditer(body): |
paul@0 | 54 | level = len(match.group("level")) |
paul@0 | 55 | |
paul@0 | 56 | if (min_level is None or level >= min_level) and \ |
paul@0 | 57 | (max_level is None or level <= max_level): |
paul@0 | 58 | |
paul@4 | 59 | return match.group("heading"), match.span() |
paul@0 | 60 | |
paul@0 | 61 | return None |
paul@0 | 62 | |
paul@4 | 63 | def getParagraph(request, page, start=0, number=None): |
paul@3 | 64 | |
paul@3 | 65 | full_page = Page(request, page.page_name) |
paul@3 | 66 | body = full_page.get_raw_body() |
paul@4 | 67 | if start != 0: |
paul@4 | 68 | body = body[start:] |
paul@3 | 69 | |
paul@3 | 70 | for i, match in enumerate(paragraph_regexp.finditer(body)): |
paul@4 | 71 | if number is None or i == max(0, number - 1): |
paul@4 | 72 | return match.group("paragraph"), match.span() |
paul@3 | 73 | |
paul@3 | 74 | return None |
paul@3 | 75 | |
paul@3 | 76 | def getPageName(request, page): |
paul@3 | 77 | return page.page_name |
paul@3 | 78 | |
paul@1 | 79 | def formatResultPages(request, formatter, pages, paging, format, page_from=0): |
paul@0 | 80 | |
paul@0 | 81 | """ |
paul@0 | 82 | Using the given 'request' and 'formatter', return a formatted string showing |
paul@0 | 83 | the result 'pages', providing paging controls when 'paging' is set to a true |
paul@0 | 84 | value, and providing page details according to the given 'format'. |
paul@1 | 85 | |
paul@1 | 86 | If the optional 'pages_from' parameter is set, the result pages from the |
paul@1 | 87 | given result (specified within a range from 0 to the length of the 'pages' |
paul@1 | 88 | collection) will be shown. |
paul@0 | 89 | """ |
paul@0 | 90 | |
paul@3 | 91 | actions = [] |
paul@1 | 92 | |
paul@3 | 93 | if format: |
paul@3 | 94 | for match in format_options_regexp.finditer(format): |
paul@3 | 95 | if match.group("heading"): |
paul@3 | 96 | actions.append((getFirstPageHeading, map(int_or_none, (match.group("min_heading"), match.group("max_heading"))))) |
paul@3 | 97 | elif match.group("paragraph"): |
paul@3 | 98 | actions.append((getParagraph, map(int_or_none, (match.group("paragraph_number"),)))) |
paul@0 | 99 | else: |
paul@3 | 100 | actions.append((getPageName, ())) |
paul@0 | 101 | |
paul@1 | 102 | # Use paging only when there are enough results. |
paul@1 | 103 | |
paul@1 | 104 | results_per_page = request.cfg.search_results_per_page |
paul@1 | 105 | paging = paging and len(pages) > results_per_page |
paul@1 | 106 | |
paul@1 | 107 | if paging: |
paul@1 | 108 | pages_to_show = pages[page_from:page_from + results_per_page] |
paul@1 | 109 | else: |
paul@1 | 110 | pages_to_show = pages |
paul@1 | 111 | |
paul@1 | 112 | # Prepare the output. |
paul@1 | 113 | |
paul@0 | 114 | output = [] |
paul@2 | 115 | output.append(formatter.number_list(on=1, start=page_from + 1)) |
paul@0 | 116 | |
paul@1 | 117 | for page in pages_to_show: |
paul@0 | 118 | output.append(formatter.listitem(on=1)) |
paul@0 | 119 | |
paul@4 | 120 | start = 0 |
paul@3 | 121 | first = 1 |
paul@3 | 122 | for action, args in actions: |
paul@4 | 123 | result = action(request, page, start, *args) |
paul@4 | 124 | |
paul@4 | 125 | if result is not None: |
paul@4 | 126 | if first: |
paul@4 | 127 | output.append(formatter.pagelink(on=1, pagename=page.page_name)) |
paul@4 | 128 | else: |
paul@4 | 129 | output.append(" ") |
paul@0 | 130 | |
paul@4 | 131 | text, span = result |
paul@4 | 132 | output.append(formatter.text(text)) |
paul@4 | 133 | |
paul@4 | 134 | # Position the search for the next action. |
paul@3 | 135 | |
paul@4 | 136 | _start, _end = span |
paul@4 | 137 | start = _end + 1 |
paul@4 | 138 | |
paul@4 | 139 | if first: |
paul@4 | 140 | output.append(formatter.pagelink(on=0)) |
paul@4 | 141 | |
paul@3 | 142 | first = 0 |
paul@3 | 143 | |
paul@0 | 144 | output.append(formatter.listitem(on=0)) |
paul@0 | 145 | |
paul@0 | 146 | output.append(formatter.number_list(on=0)) |
paul@0 | 147 | |
paul@1 | 148 | # Show paging navigation. |
paul@1 | 149 | |
paul@1 | 150 | if paging: |
paul@1 | 151 | output.append(formatPagingNavigation(request, formatter, pages, page_from)) |
paul@1 | 152 | |
paul@0 | 153 | return "".join(output) |
paul@0 | 154 | |
paul@1 | 155 | def formatPagingNavigation(request, formatter, pages, page_from=0): |
paul@1 | 156 | |
paul@1 | 157 | """ |
paul@1 | 158 | Using the given 'request' and 'formatter', return a formatted string showing |
paul@1 | 159 | the paging navigation for the result 'pages', according to the 'page_from' |
paul@1 | 160 | indicator which provides the current position in the result set. |
paul@1 | 161 | """ |
paul@1 | 162 | |
paul@2 | 163 | page = formatter.page |
paul@2 | 164 | pagename = page.page_name |
paul@1 | 165 | _ = request.getText |
paul@1 | 166 | |
paul@1 | 167 | output = [] |
paul@1 | 168 | |
paul@1 | 169 | results_per_page = request.cfg.search_results_per_page |
paul@1 | 170 | number_of_results = len(pages) |
paul@1 | 171 | |
paul@1 | 172 | pages_total = number_of_results / results_per_page |
paul@1 | 173 | pages_before = page_from / results_per_page |
paul@1 | 174 | pages_after = ((number_of_results - page_from) / results_per_page) - 1 |
paul@1 | 175 | |
paul@1 | 176 | querydict = wikiutil.parseQueryString(request.query_string) |
paul@1 | 177 | |
paul@1 | 178 | output.append(formatter.paragraph(on=1)) |
paul@1 | 179 | output.append(formatter.text(_("Result pages:"))) |
paul@1 | 180 | output.append(formatter.text(" ")) |
paul@1 | 181 | |
paul@1 | 182 | n = 0 |
paul@1 | 183 | while n < pages_before: |
paul@2 | 184 | output.append(formatter.pagelink(on=1, pagename=pagename, querystr=getPagingQueryString(querydict, n * results_per_page))) |
paul@1 | 185 | output.append(formatter.text(str(n + 1))) |
paul@1 | 186 | output.append(formatter.pagelink(on=0)) |
paul@1 | 187 | output.append(formatter.text(" ")) |
paul@1 | 188 | n += 1 |
paul@1 | 189 | |
paul@1 | 190 | output.append(formatter.text(str(n + 1))) |
paul@1 | 191 | output.append(formatter.text(" ")) |
paul@1 | 192 | n += 1 |
paul@1 | 193 | |
paul@2 | 194 | while n <= pages_total: |
paul@2 | 195 | output.append(formatter.pagelink(on=1, pagename=pagename, querystr=getPagingQueryString(querydict, n * results_per_page))) |
paul@1 | 196 | output.append(formatter.text(str(n + 1))) |
paul@1 | 197 | output.append(formatter.pagelink(on=0)) |
paul@1 | 198 | output.append(formatter.text(" ")) |
paul@1 | 199 | n += 1 |
paul@1 | 200 | |
paul@1 | 201 | output.append(formatter.paragraph(on=0)) |
paul@1 | 202 | |
paul@1 | 203 | return "".join(output) |
paul@1 | 204 | |
paul@1 | 205 | def getPagingQueryString(querydict, page_from): |
paul@1 | 206 | querydict["from"] = page_from |
paul@1 | 207 | return wikiutil.makeQueryString(querydict) |
paul@1 | 208 | |
paul@0 | 209 | def int_or_none(x): |
paul@0 | 210 | if x is None: |
paul@0 | 211 | return x |
paul@0 | 212 | else: |
paul@0 | 213 | return int(x) |
paul@0 | 214 | |
paul@0 | 215 | # vim: tabstop=4 expandtab shiftwidth=4 |