# HG changeset patch # User Paul Boddie # Date 1284416491 -7200 # Node ID f036b082e99b59be0e035e32dbaa0613ed31fc21 # Parent 5798830a2bc3f0a3c290653a63da41753efca9ef Introduced search continuation for actions so that a paragraph requested after a heading will follow that heading, not occur at the start of the page. Corrected paragraph numbers to start at 1. diff -r 5798830a2bc3 -r f036b082e99b ImprovedMoinSearch.py --- a/ImprovedMoinSearch.py Sun Sep 05 01:49:50 2010 +0200 +++ b/ImprovedMoinSearch.py Tue Sep 14 00:21:31 2010 +0200 @@ -32,16 +32,23 @@ results = searchPages(request, query, **kw) return results.hits -def getFirstPageHeading(request, page, min_level=None, max_level=None): +def getFirstPageHeading(request, page, start=0, min_level=None, max_level=None): """ Using the given 'request', return the first heading in the given 'page' + from the given 'start' point (optional, defaulting to the start of the page) having a heading level of at least 'min_level' (which is undefined if not specified) and at most 'max_level' (which is undefined if not specified). + + A tuple containing the heading and the span (the start offset and the end + offset as a tuple) is returned for a successful retrieval. Otherwise, None + is returned. """ full_page = Page(request, page.page_name) body = full_page.get_raw_body() + if start != 0: + body = body[start:] for match in heading_regexp.finditer(body): level = len(match.group("level")) @@ -49,18 +56,20 @@ if (min_level is None or level >= min_level) and \ (max_level is None or level <= max_level): - return match.group("heading") + return match.group("heading"), match.span() return None -def getParagraph(request, page, number=None): +def getParagraph(request, page, start=0, number=None): full_page = Page(request, page.page_name) body = full_page.get_raw_body() + if start != 0: + body = body[start:] for i, match in enumerate(paragraph_regexp.finditer(body)): - if number is None or i == number: - return match.group("paragraph") + if number is None or i == max(0, number - 1): + return match.group("paragraph"), match.span() return None @@ -108,18 +117,28 @@ for page in pages_to_show: output.append(formatter.listitem(on=1)) + start = 0 first = 1 for action, args in actions: - if first: - output.append(formatter.pagelink(on=1, pagename=page.page_name)) - else: - output.append(" ") + result = action(request, page, start, *args) + + if result is not None: + if first: + output.append(formatter.pagelink(on=1, pagename=page.page_name)) + else: + output.append(" ") - text = action(request, page, *args) - output.append(formatter.text(text)) + text, span = result + output.append(formatter.text(text)) + + # Position the search for the next action. - if first: - output.append(formatter.pagelink(on=0)) + _start, _end = span + start = _end + 1 + + if first: + output.append(formatter.pagelink(on=0)) + first = 0 output.append(formatter.listitem(on=0))