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@3 | 35 | def getFirstPageHeading(request, page, 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@3 | 39 | having a heading level of at least 'min_level' (which is undefined if not |
paul@3 | 40 | specified) and at most 'max_level' (which is undefined if not specified). |
paul@0 | 41 | """ |
paul@0 | 42 | |
paul@0 | 43 | full_page = Page(request, page.page_name) |
paul@0 | 44 | body = full_page.get_raw_body() |
paul@0 | 45 | |
paul@0 | 46 | for match in heading_regexp.finditer(body): |
paul@0 | 47 | level = len(match.group("level")) |
paul@0 | 48 | |
paul@0 | 49 | if (min_level is None or level >= min_level) and \ |
paul@0 | 50 | (max_level is None or level <= max_level): |
paul@0 | 51 | |
paul@0 | 52 | return match.group("heading") |
paul@0 | 53 | |
paul@0 | 54 | return None |
paul@0 | 55 | |
paul@3 | 56 | def getParagraph(request, page, number=None): |
paul@3 | 57 | |
paul@3 | 58 | full_page = Page(request, page.page_name) |
paul@3 | 59 | body = full_page.get_raw_body() |
paul@3 | 60 | |
paul@3 | 61 | for i, match in enumerate(paragraph_regexp.finditer(body)): |
paul@3 | 62 | if number is None or i == number: |
paul@3 | 63 | return match.group("paragraph") |
paul@3 | 64 | |
paul@3 | 65 | return None |
paul@3 | 66 | |
paul@3 | 67 | def getPageName(request, page): |
paul@3 | 68 | return page.page_name |
paul@3 | 69 | |
paul@1 | 70 | def formatResultPages(request, formatter, pages, paging, format, page_from=0): |
paul@0 | 71 | |
paul@0 | 72 | """ |
paul@0 | 73 | Using the given 'request' and 'formatter', return a formatted string showing |
paul@0 | 74 | the result 'pages', providing paging controls when 'paging' is set to a true |
paul@0 | 75 | value, and providing page details according to the given 'format'. |
paul@1 | 76 | |
paul@1 | 77 | If the optional 'pages_from' parameter is set, the result pages from the |
paul@1 | 78 | given result (specified within a range from 0 to the length of the 'pages' |
paul@1 | 79 | collection) will be shown. |
paul@0 | 80 | """ |
paul@0 | 81 | |
paul@3 | 82 | actions = [] |
paul@1 | 83 | |
paul@3 | 84 | if format: |
paul@3 | 85 | for match in format_options_regexp.finditer(format): |
paul@3 | 86 | if match.group("heading"): |
paul@3 | 87 | actions.append((getFirstPageHeading, map(int_or_none, (match.group("min_heading"), match.group("max_heading"))))) |
paul@3 | 88 | elif match.group("paragraph"): |
paul@3 | 89 | actions.append((getParagraph, map(int_or_none, (match.group("paragraph_number"),)))) |
paul@0 | 90 | else: |
paul@3 | 91 | actions.append((getPageName, ())) |
paul@0 | 92 | |
paul@1 | 93 | # Use paging only when there are enough results. |
paul@1 | 94 | |
paul@1 | 95 | results_per_page = request.cfg.search_results_per_page |
paul@1 | 96 | paging = paging and len(pages) > results_per_page |
paul@1 | 97 | |
paul@1 | 98 | if paging: |
paul@1 | 99 | pages_to_show = pages[page_from:page_from + results_per_page] |
paul@1 | 100 | else: |
paul@1 | 101 | pages_to_show = pages |
paul@1 | 102 | |
paul@1 | 103 | # Prepare the output. |
paul@1 | 104 | |
paul@0 | 105 | output = [] |
paul@2 | 106 | output.append(formatter.number_list(on=1, start=page_from + 1)) |
paul@0 | 107 | |
paul@1 | 108 | for page in pages_to_show: |
paul@0 | 109 | output.append(formatter.listitem(on=1)) |
paul@0 | 110 | |
paul@3 | 111 | first = 1 |
paul@3 | 112 | for action, args in actions: |
paul@3 | 113 | if first: |
paul@3 | 114 | output.append(formatter.pagelink(on=1, pagename=page.page_name)) |
paul@3 | 115 | else: |
paul@3 | 116 | output.append(" ") |
paul@0 | 117 | |
paul@3 | 118 | text = action(request, page, *args) |
paul@3 | 119 | output.append(formatter.text(text)) |
paul@3 | 120 | |
paul@3 | 121 | if first: |
paul@3 | 122 | output.append(formatter.pagelink(on=0)) |
paul@3 | 123 | first = 0 |
paul@3 | 124 | |
paul@0 | 125 | output.append(formatter.listitem(on=0)) |
paul@0 | 126 | |
paul@0 | 127 | output.append(formatter.number_list(on=0)) |
paul@0 | 128 | |
paul@1 | 129 | # Show paging navigation. |
paul@1 | 130 | |
paul@1 | 131 | if paging: |
paul@1 | 132 | output.append(formatPagingNavigation(request, formatter, pages, page_from)) |
paul@1 | 133 | |
paul@0 | 134 | return "".join(output) |
paul@0 | 135 | |
paul@1 | 136 | def formatPagingNavigation(request, formatter, pages, page_from=0): |
paul@1 | 137 | |
paul@1 | 138 | """ |
paul@1 | 139 | Using the given 'request' and 'formatter', return a formatted string showing |
paul@1 | 140 | the paging navigation for the result 'pages', according to the 'page_from' |
paul@1 | 141 | indicator which provides the current position in the result set. |
paul@1 | 142 | """ |
paul@1 | 143 | |
paul@2 | 144 | page = formatter.page |
paul@2 | 145 | pagename = page.page_name |
paul@1 | 146 | _ = request.getText |
paul@1 | 147 | |
paul@1 | 148 | output = [] |
paul@1 | 149 | |
paul@1 | 150 | results_per_page = request.cfg.search_results_per_page |
paul@1 | 151 | number_of_results = len(pages) |
paul@1 | 152 | |
paul@1 | 153 | pages_total = number_of_results / results_per_page |
paul@1 | 154 | pages_before = page_from / results_per_page |
paul@1 | 155 | pages_after = ((number_of_results - page_from) / results_per_page) - 1 |
paul@1 | 156 | |
paul@1 | 157 | querydict = wikiutil.parseQueryString(request.query_string) |
paul@1 | 158 | |
paul@1 | 159 | output.append(formatter.paragraph(on=1)) |
paul@1 | 160 | output.append(formatter.text(_("Result pages:"))) |
paul@1 | 161 | output.append(formatter.text(" ")) |
paul@1 | 162 | |
paul@1 | 163 | n = 0 |
paul@1 | 164 | while n < pages_before: |
paul@2 | 165 | output.append(formatter.pagelink(on=1, pagename=pagename, querystr=getPagingQueryString(querydict, n * results_per_page))) |
paul@1 | 166 | output.append(formatter.text(str(n + 1))) |
paul@1 | 167 | output.append(formatter.pagelink(on=0)) |
paul@1 | 168 | output.append(formatter.text(" ")) |
paul@1 | 169 | n += 1 |
paul@1 | 170 | |
paul@1 | 171 | output.append(formatter.text(str(n + 1))) |
paul@1 | 172 | output.append(formatter.text(" ")) |
paul@1 | 173 | n += 1 |
paul@1 | 174 | |
paul@2 | 175 | while n <= pages_total: |
paul@2 | 176 | output.append(formatter.pagelink(on=1, pagename=pagename, querystr=getPagingQueryString(querydict, n * results_per_page))) |
paul@1 | 177 | output.append(formatter.text(str(n + 1))) |
paul@1 | 178 | output.append(formatter.pagelink(on=0)) |
paul@1 | 179 | output.append(formatter.text(" ")) |
paul@1 | 180 | n += 1 |
paul@1 | 181 | |
paul@1 | 182 | output.append(formatter.paragraph(on=0)) |
paul@1 | 183 | |
paul@1 | 184 | return "".join(output) |
paul@1 | 185 | |
paul@1 | 186 | def getPagingQueryString(querydict, page_from): |
paul@1 | 187 | querydict["from"] = page_from |
paul@1 | 188 | return wikiutil.makeQueryString(querydict) |
paul@1 | 189 | |
paul@0 | 190 | def int_or_none(x): |
paul@0 | 191 | if x is None: |
paul@0 | 192 | return x |
paul@0 | 193 | else: |
paul@0 | 194 | return int(x) |
paul@0 | 195 | |
paul@0 | 196 | # vim: tabstop=4 expandtab shiftwidth=4 |