# HG changeset patch # User Paul Boddie # Date 1277681961 -7200 # Node ID 9cc135e6bfee53800b1c15cd7bc3fbdb885e13a1 # Parent 64514a01af2b20d0bc2f6c1bdaa59d25de8c0689 Started to introduce paging support. Fixed the option extraction function to actually use the supplied pattern. diff -r 64514a01af2b -r 9cc135e6bfee ImprovedMoinSearch.py --- a/ImprovedMoinSearch.py Fri Jun 25 01:48:21 2010 +0200 +++ b/ImprovedMoinSearch.py Mon Jun 28 01:39:21 2010 +0200 @@ -8,6 +8,7 @@ from MoinMoin.search import searchPages from MoinMoin.Page import Page +from MoinMoin import wikiutil import re heading_regexp = re.compile(r"^(?P=+)(?P.*?)(?P=level)$", re.UNICODE | re.MULTILINE) @@ -44,15 +45,23 @@ return None -def formatResultPages(request, formatter, pages, paging, format): +def formatResultPages(request, formatter, pages, paging, format, page_from=0): """ Using the given 'request' and 'formatter', return a formatted string showing the result 'pages', providing paging controls when 'paging' is set to a true value, and providing page details according to the given 'format'. + + If the optional 'pages_from' parameter is set, the result pages from the + given result (specified within a range from 0 to the length of the 'pages' + collection) will be shown. """ - m = heading_options_regexp.search(format) + if format: + m = heading_options_regexp.search(format) + else: + m = None + if m: format = "heading" min_level = int_or_none(m.group("min")) @@ -60,11 +69,22 @@ else: format = "name" + # Use paging only when there are enough results. + + results_per_page = request.cfg.search_results_per_page + paging = paging and len(pages) > results_per_page + + if paging: + pages_to_show = pages[page_from:page_from + results_per_page] + else: + pages_to_show = pages + + # Prepare the output. + output = [] - output.append(formatter.number_list(on=1)) - for page in pages: + for page in pages_to_show: output.append(formatter.listitem(on=1)) if format == "heading": @@ -79,8 +99,65 @@ output.append(formatter.number_list(on=0)) + # Show paging navigation. + + if paging: + output.append(formatPagingNavigation(request, formatter, pages, page_from)) + return "".join(output) +def formatPagingNavigation(request, formatter, pages, page_from=0): + + """ + Using the given 'request' and 'formatter', return a formatted string showing + the paging navigation for the result 'pages', according to the 'page_from' + indicator which provides the current position in the result set. + """ + + _ = request.getText + + output = [] + + results_per_page = request.cfg.search_results_per_page + number_of_results = len(pages) + + pages_total = number_of_results / results_per_page + pages_before = page_from / results_per_page + pages_after = ((number_of_results - page_from) / results_per_page) - 1 + + querydict = wikiutil.parseQueryString(request.query_string) + + output.append(formatter.paragraph(on=1)) + output.append(formatter.text(_("Result pages:"))) + output.append(formatter.text(" ")) + + n = 0 + while n < pages_before: + output.append(formatter.pagelink(on=1, querystr=getPagingQueryString(querydict, n * results_per_page))) + output.append(formatter.text(str(n + 1))) + output.append(formatter.pagelink(on=0)) + output.append(formatter.text(" ")) + n += 1 + + output.append(formatter.text(str(n + 1))) + output.append(formatter.text(" ")) + n += 1 + + while n < pages_total: + output.append(formatter.pagelink(on=1, querystr=getPagingQueryString(querydict, n * results_per_page))) + output.append(formatter.text(str(n + 1))) + output.append(formatter.pagelink(on=0)) + output.append(formatter.text(" ")) + n += 1 + + output.append(formatter.paragraph(on=0)) + + return "".join(output) + +def getPagingQueryString(querydict, page_from): + querydict["from"] = page_from + return wikiutil.makeQueryString(querydict) + def int_or_none(x): if x is None: return x diff -r 64514a01af2b -r 9cc135e6bfee macros/PageListPlus.py --- a/macros/PageListPlus.py Fri Jun 25 01:48:21 2010 +0200 +++ b/macros/PageListPlus.py Mon Jun 28 01:39:21 2010 +0200 @@ -25,7 +25,7 @@ (edited_query, value_of_group). """ - m = format_pattern.search(query) + m = pattern.search(query) if m: value = m.group(group) query = query[:m.start()] + query[m.end():]