1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ImprovedMoinSearch.py Fri Jun 25 01:48:21 2010 +0200
1.3 @@ -0,0 +1,90 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - ImprovedMoinSearch library
1.7 +
1.8 + @copyright: 2010 Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.search import searchPages
1.13 +from MoinMoin.Page import Page
1.14 +import re
1.15 +
1.16 +heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE)
1.17 +heading_options_regexp = re.compile(r"heading\s*(?P<min>\d+)?(?:\s*-\s*(?P<max>\d+))?", re.UNICODE)
1.18 +
1.19 +def getSearchResultPages(request, query, **kw):
1.20 +
1.21 + """
1.22 + Return matching pages using the given 'request' and search 'query'. Optional
1.23 + keyword arguments are passed to the underlying search infrastructure.
1.24 + """
1.25 +
1.26 + results = searchPages(request, query, **kw)
1.27 + return results.hits
1.28 +
1.29 +def getFirstPageHeading(request, page, min_level=1, max_level=1):
1.30 +
1.31 + """
1.32 + Using the given 'request', return the first heading in the given 'page'
1.33 + having a heading level of at least 'min_level' (which is 1 if not specified)
1.34 + and at most 'max_level' (which is 1 if not specified).
1.35 + """
1.36 +
1.37 + full_page = Page(request, page.page_name)
1.38 + body = full_page.get_raw_body()
1.39 +
1.40 + for match in heading_regexp.finditer(body):
1.41 + level = len(match.group("level"))
1.42 +
1.43 + if (min_level is None or level >= min_level) and \
1.44 + (max_level is None or level <= max_level):
1.45 +
1.46 + return match.group("heading")
1.47 +
1.48 + return None
1.49 +
1.50 +def formatResultPages(request, formatter, pages, paging, format):
1.51 +
1.52 + """
1.53 + Using the given 'request' and 'formatter', return a formatted string showing
1.54 + the result 'pages', providing paging controls when 'paging' is set to a true
1.55 + value, and providing page details according to the given 'format'.
1.56 + """
1.57 +
1.58 + m = heading_options_regexp.search(format)
1.59 + if m:
1.60 + format = "heading"
1.61 + min_level = int_or_none(m.group("min"))
1.62 + max_level = int_or_none(m.group("max"))
1.63 + else:
1.64 + format = "name"
1.65 +
1.66 + output = []
1.67 +
1.68 + output.append(formatter.number_list(on=1))
1.69 +
1.70 + for page in pages:
1.71 + output.append(formatter.listitem(on=1))
1.72 +
1.73 + if format == "heading":
1.74 + text = getFirstPageHeading(request, page, min_level, max_level) or page.page_name
1.75 + else:
1.76 + text = page.page_name
1.77 +
1.78 + output.append(formatter.pagelink(on=1, pagename=page.page_name))
1.79 + output.append(formatter.text(text))
1.80 + output.append(formatter.pagelink(on=0))
1.81 + output.append(formatter.listitem(on=0))
1.82 +
1.83 + output.append(formatter.number_list(on=0))
1.84 +
1.85 + return "".join(output)
1.86 +
1.87 +def int_or_none(x):
1.88 + if x is None:
1.89 + return x
1.90 + else:
1.91 + return int(x)
1.92 +
1.93 +# vim: tabstop=4 expandtab shiftwidth=4