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@0 | 11 | import re |
paul@0 | 12 | |
paul@0 | 13 | heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE) |
paul@0 | 14 | heading_options_regexp = re.compile(r"heading\s*(?P<min>\d+)?(?:\s*-\s*(?P<max>\d+))?", re.UNICODE) |
paul@0 | 15 | |
paul@0 | 16 | def getSearchResultPages(request, query, **kw): |
paul@0 | 17 | |
paul@0 | 18 | """ |
paul@0 | 19 | Return matching pages using the given 'request' and search 'query'. Optional |
paul@0 | 20 | keyword arguments are passed to the underlying search infrastructure. |
paul@0 | 21 | """ |
paul@0 | 22 | |
paul@0 | 23 | results = searchPages(request, query, **kw) |
paul@0 | 24 | return results.hits |
paul@0 | 25 | |
paul@0 | 26 | def getFirstPageHeading(request, page, min_level=1, max_level=1): |
paul@0 | 27 | |
paul@0 | 28 | """ |
paul@0 | 29 | Using the given 'request', return the first heading in the given 'page' |
paul@0 | 30 | having a heading level of at least 'min_level' (which is 1 if not specified) |
paul@0 | 31 | and at most 'max_level' (which is 1 if not specified). |
paul@0 | 32 | """ |
paul@0 | 33 | |
paul@0 | 34 | full_page = Page(request, page.page_name) |
paul@0 | 35 | body = full_page.get_raw_body() |
paul@0 | 36 | |
paul@0 | 37 | for match in heading_regexp.finditer(body): |
paul@0 | 38 | level = len(match.group("level")) |
paul@0 | 39 | |
paul@0 | 40 | if (min_level is None or level >= min_level) and \ |
paul@0 | 41 | (max_level is None or level <= max_level): |
paul@0 | 42 | |
paul@0 | 43 | return match.group("heading") |
paul@0 | 44 | |
paul@0 | 45 | return None |
paul@0 | 46 | |
paul@0 | 47 | def formatResultPages(request, formatter, pages, paging, format): |
paul@0 | 48 | |
paul@0 | 49 | """ |
paul@0 | 50 | Using the given 'request' and 'formatter', return a formatted string showing |
paul@0 | 51 | the result 'pages', providing paging controls when 'paging' is set to a true |
paul@0 | 52 | value, and providing page details according to the given 'format'. |
paul@0 | 53 | """ |
paul@0 | 54 | |
paul@0 | 55 | m = heading_options_regexp.search(format) |
paul@0 | 56 | if m: |
paul@0 | 57 | format = "heading" |
paul@0 | 58 | min_level = int_or_none(m.group("min")) |
paul@0 | 59 | max_level = int_or_none(m.group("max")) |
paul@0 | 60 | else: |
paul@0 | 61 | format = "name" |
paul@0 | 62 | |
paul@0 | 63 | output = [] |
paul@0 | 64 | |
paul@0 | 65 | output.append(formatter.number_list(on=1)) |
paul@0 | 66 | |
paul@0 | 67 | for page in pages: |
paul@0 | 68 | output.append(formatter.listitem(on=1)) |
paul@0 | 69 | |
paul@0 | 70 | if format == "heading": |
paul@0 | 71 | text = getFirstPageHeading(request, page, min_level, max_level) or page.page_name |
paul@0 | 72 | else: |
paul@0 | 73 | text = page.page_name |
paul@0 | 74 | |
paul@0 | 75 | output.append(formatter.pagelink(on=1, pagename=page.page_name)) |
paul@0 | 76 | output.append(formatter.text(text)) |
paul@0 | 77 | output.append(formatter.pagelink(on=0)) |
paul@0 | 78 | output.append(formatter.listitem(on=0)) |
paul@0 | 79 | |
paul@0 | 80 | output.append(formatter.number_list(on=0)) |
paul@0 | 81 | |
paul@0 | 82 | return "".join(output) |
paul@0 | 83 | |
paul@0 | 84 | def int_or_none(x): |
paul@0 | 85 | if x is None: |
paul@0 | 86 | return x |
paul@0 | 87 | else: |
paul@0 | 88 | return int(x) |
paul@0 | 89 | |
paul@0 | 90 | # vim: tabstop=4 expandtab shiftwidth=4 |