paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - PageListPlus |
paul@0 | 4 | |
paul@0 | 5 | print a list of pages whose title matches the search term |
paul@0 | 6 | |
paul@0 | 7 | @copyright: 2010 Paul Boddie <paul@boddie.org.uk> |
paul@3 | 8 | 2001-2003 Juergen Hermann <jh@web.de> |
paul@3 | 9 | 2003-2008 MoinMoin:ThomasWaldmann |
paul@3 | 10 | 2008 MoinMoin:ReimarBauer |
paul@0 | 11 | @license: GNU GPL, see COPYING for details. |
paul@0 | 12 | """ |
paul@0 | 13 | |
paul@0 | 14 | Dependencies = ["pages"] |
paul@0 | 15 | from MoinMoin import wikiutil |
paul@0 | 16 | import ImprovedMoinSearch |
paul@0 | 17 | import re |
paul@0 | 18 | |
paul@0 | 19 | format_pattern = re.compile("""format:(?P<quote>['"])(?P<format>.*?)(?P=quote)""") |
paul@0 | 20 | paging_pattern = re.compile("""paging:(?P<paging>on|off|yes|no|true|false)""") |
paul@0 | 21 | |
paul@0 | 22 | def extract_option(query, pattern, group): |
paul@0 | 23 | |
paul@0 | 24 | """ |
paul@0 | 25 | Remove from 'query', using the given regular expression 'pattern' object, |
paul@0 | 26 | a particular form of argument text, accessing such text through the group |
paul@0 | 27 | with the given 'group' name, and returning a tuple of the form |
paul@0 | 28 | (edited_query, value_of_group). |
paul@0 | 29 | """ |
paul@0 | 30 | |
paul@1 | 31 | m = pattern.search(query) |
paul@0 | 32 | if m: |
paul@0 | 33 | value = m.group(group) |
paul@0 | 34 | query = query[:m.start()] + query[m.end():] |
paul@0 | 35 | else: |
paul@0 | 36 | value = None |
paul@0 | 37 | return query, value |
paul@0 | 38 | |
paul@0 | 39 | # Macro functions. |
paul@0 | 40 | |
paul@0 | 41 | def execute(macro, args): |
paul@0 | 42 | |
paul@0 | 43 | """ |
paul@0 | 44 | Execute the 'macro' with the given 'args': |
paul@0 | 45 | """ |
paul@0 | 46 | |
paul@0 | 47 | request = macro.request |
paul@0 | 48 | formatter = macro.formatter |
paul@0 | 49 | _ = macro._ |
paul@0 | 50 | |
paul@0 | 51 | # If called with empty or no argument, default to regex search for .+, the full page list. |
paul@0 | 52 | |
paul@0 | 53 | needle = wikiutil.get_unicode(request, args, 'needle', u'regex:.+') |
paul@0 | 54 | |
paul@0 | 55 | # Extract format and paging options. |
paul@0 | 56 | |
paul@0 | 57 | needle, format = extract_option(needle, format_pattern, "format") |
paul@0 | 58 | needle, paging = extract_option(needle, paging_pattern, "paging") |
paul@0 | 59 | |
paul@0 | 60 | # With whitespace argument, return same error message as FullSearch. |
paul@0 | 61 | |
paul@0 | 62 | if not needle.strip(): |
paul@0 | 63 | err = _('Please use a more selective search term instead of {{{"%s"}}}', wiki=True) % needle |
paul@0 | 64 | return '<span class="error">%s</span>' % err |
paul@0 | 65 | |
paul@0 | 66 | # Return a title search for needle, sorted by name. |
paul@0 | 67 | |
paul@0 | 68 | try: |
paul@0 | 69 | pages = ImprovedMoinSearch.getSearchResultPages(request, needle, titlesearch=1, sort='page_name') |
paul@0 | 70 | |
paul@2 | 71 | # Get any paging information from the request. |
paul@2 | 72 | |
paul@2 | 73 | page_from = int(request.form.get("from", ["0"])[0]) |
paul@2 | 74 | |
paul@0 | 75 | # Format the results. |
paul@0 | 76 | |
paul@2 | 77 | ret = ImprovedMoinSearch.formatResultPages(request, formatter, pages, paging, format, page_from) |
paul@0 | 78 | |
paul@0 | 79 | except ValueError: |
paul@0 | 80 | |
paul@0 | 81 | # Same error as in MoinMoin/action/fullsearch.py, keep it that way! |
paul@0 | 82 | |
paul@0 | 83 | ret = ''.join([formatter.text('<<PageListPlus('), |
paul@0 | 84 | _('Your search query {{{"%s"}}} is invalid. Please refer to ' |
paul@0 | 85 | 'HelpOnSearching for more information.', wiki=True, |
paul@0 | 86 | percent=True) % wikiutil.escape(needle), |
paul@0 | 87 | formatter.text(')>>')]) |
paul@0 | 88 | |
paul@0 | 89 | return ret |
paul@0 | 90 | |
paul@0 | 91 | # vim: tabstop=4 expandtab shiftwidth=4 |