1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/macros/PageListPlus.py Fri Jun 25 01:48:21 2010 +0200
1.3 @@ -0,0 +1,84 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - PageListPlus
1.7 +
1.8 + print a list of pages whose title matches the search term
1.9 +
1.10 + @copyright: 2010 Paul Boddie <paul@boddie.org.uk>
1.11 + @license: GNU GPL, see COPYING for details.
1.12 +"""
1.13 +
1.14 +Dependencies = ["pages"]
1.15 +from MoinMoin import wikiutil
1.16 +import ImprovedMoinSearch
1.17 +import re
1.18 +
1.19 +format_pattern = re.compile("""format:(?P<quote>['"])(?P<format>.*?)(?P=quote)""")
1.20 +paging_pattern = re.compile("""paging:(?P<paging>on|off|yes|no|true|false)""")
1.21 +
1.22 +def extract_option(query, pattern, group):
1.23 +
1.24 + """
1.25 + Remove from 'query', using the given regular expression 'pattern' object,
1.26 + a particular form of argument text, accessing such text through the group
1.27 + with the given 'group' name, and returning a tuple of the form
1.28 + (edited_query, value_of_group).
1.29 + """
1.30 +
1.31 + m = format_pattern.search(query)
1.32 + if m:
1.33 + value = m.group(group)
1.34 + query = query[:m.start()] + query[m.end():]
1.35 + else:
1.36 + value = None
1.37 + return query, value
1.38 +
1.39 +# Macro functions.
1.40 +
1.41 +def execute(macro, args):
1.42 +
1.43 + """
1.44 + Execute the 'macro' with the given 'args':
1.45 + """
1.46 +
1.47 + request = macro.request
1.48 + formatter = macro.formatter
1.49 + _ = macro._
1.50 +
1.51 + # If called with empty or no argument, default to regex search for .+, the full page list.
1.52 +
1.53 + needle = wikiutil.get_unicode(request, args, 'needle', u'regex:.+')
1.54 +
1.55 + # Extract format and paging options.
1.56 +
1.57 + needle, format = extract_option(needle, format_pattern, "format")
1.58 + needle, paging = extract_option(needle, paging_pattern, "paging")
1.59 +
1.60 + # With whitespace argument, return same error message as FullSearch.
1.61 +
1.62 + if not needle.strip():
1.63 + err = _('Please use a more selective search term instead of {{{"%s"}}}', wiki=True) % needle
1.64 + return '<span class="error">%s</span>' % err
1.65 +
1.66 + # Return a title search for needle, sorted by name.
1.67 +
1.68 + try:
1.69 + pages = ImprovedMoinSearch.getSearchResultPages(request, needle, titlesearch=1, sort='page_name')
1.70 +
1.71 + # Format the results.
1.72 +
1.73 + ret = ImprovedMoinSearch.formatResultPages(request, formatter, pages, paging, format)
1.74 +
1.75 + except ValueError:
1.76 +
1.77 + # Same error as in MoinMoin/action/fullsearch.py, keep it that way!
1.78 +
1.79 + ret = ''.join([formatter.text('<<PageListPlus('),
1.80 + _('Your search query {{{"%s"}}} is invalid. Please refer to '
1.81 + 'HelpOnSearching for more information.', wiki=True,
1.82 + percent=True) % wikiutil.escape(needle),
1.83 + formatter.text(')>>')])
1.84 +
1.85 + return ret
1.86 +
1.87 +# vim: tabstop=4 expandtab shiftwidth=4