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