ImprovedMoinSearch

Changeset

4:f036b082e99b
2010-09-14 Paul Boddie raw files shortlog changelog graph 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.
ImprovedMoinSearch.py (file)
     1.1 --- a/ImprovedMoinSearch.py	Sun Sep 05 01:49:50 2010 +0200
     1.2 +++ b/ImprovedMoinSearch.py	Tue Sep 14 00:21:31 2010 +0200
     1.3 @@ -32,16 +32,23 @@
     1.4      results = searchPages(request, query, **kw)
     1.5      return results.hits
     1.6  
     1.7 -def getFirstPageHeading(request, page, min_level=None, max_level=None):
     1.8 +def getFirstPageHeading(request, page, start=0, min_level=None, max_level=None):
     1.9  
    1.10      """
    1.11      Using the given 'request', return the first heading in the given 'page'
    1.12 +    from the given 'start' point (optional, defaulting to the start of the page)
    1.13      having a heading level of at least 'min_level' (which is undefined if not
    1.14      specified) and at most 'max_level' (which is undefined if not specified).
    1.15 +
    1.16 +    A tuple containing the heading and the span (the start offset and the end
    1.17 +    offset as a tuple) is returned for a successful retrieval. Otherwise, None
    1.18 +    is returned.
    1.19      """
    1.20  
    1.21      full_page = Page(request, page.page_name)
    1.22      body = full_page.get_raw_body()
    1.23 +    if start != 0:
    1.24 +        body = body[start:]
    1.25  
    1.26      for match in heading_regexp.finditer(body):
    1.27          level = len(match.group("level"))
    1.28 @@ -49,18 +56,20 @@
    1.29          if (min_level is None or level >= min_level) and \
    1.30              (max_level is None or level <= max_level):
    1.31  
    1.32 -            return match.group("heading")
    1.33 +            return match.group("heading"), match.span()
    1.34  
    1.35      return None
    1.36  
    1.37 -def getParagraph(request, page, number=None):
    1.38 +def getParagraph(request, page, start=0, number=None):
    1.39  
    1.40      full_page = Page(request, page.page_name)
    1.41      body = full_page.get_raw_body()
    1.42 +    if start != 0:
    1.43 +        body = body[start:]
    1.44  
    1.45      for i, match in enumerate(paragraph_regexp.finditer(body)):
    1.46 -        if number is None or i == number:
    1.47 -            return match.group("paragraph")
    1.48 +        if number is None or i == max(0, number - 1):
    1.49 +            return match.group("paragraph"), match.span()
    1.50  
    1.51      return None
    1.52  
    1.53 @@ -108,18 +117,28 @@
    1.54      for page in pages_to_show:
    1.55          output.append(formatter.listitem(on=1))
    1.56  
    1.57 +        start = 0
    1.58          first = 1
    1.59          for action, args in actions:
    1.60 -            if first:
    1.61 -                output.append(formatter.pagelink(on=1, pagename=page.page_name))
    1.62 -            else:
    1.63 -                output.append(" ")
    1.64 +            result = action(request, page, start, *args)
    1.65 +
    1.66 +            if result is not None:
    1.67 +                if first:
    1.68 +                    output.append(formatter.pagelink(on=1, pagename=page.page_name))
    1.69 +                else:
    1.70 +                    output.append(" ")
    1.71  
    1.72 -            text = action(request, page, *args)
    1.73 -            output.append(formatter.text(text))
    1.74 +                text, span = result
    1.75 +                output.append(formatter.text(text))
    1.76 +
    1.77 +                # Position the search for the next action.
    1.78  
    1.79 -            if first:
    1.80 -                output.append(formatter.pagelink(on=0))
    1.81 +                _start, _end = span
    1.82 +                start = _end + 1
    1.83 +
    1.84 +                if first:
    1.85 +                    output.append(formatter.pagelink(on=0))
    1.86 +
    1.87              first = 0
    1.88  
    1.89          output.append(formatter.listitem(on=0))