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