1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ImprovedMoinSearch.py Fri Jun 25 01:48:21 2010 +0200
1.3 @@ -0,0 +1,90 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - ImprovedMoinSearch library
1.7 +
1.8 + @copyright: 2010 Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.search import searchPages
1.13 +from MoinMoin.Page import Page
1.14 +import re
1.15 +
1.16 +heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE)
1.17 +heading_options_regexp = re.compile(r"heading\s*(?P<min>\d+)?(?:\s*-\s*(?P<max>\d+))?", re.UNICODE)
1.18 +
1.19 +def getSearchResultPages(request, query, **kw):
1.20 +
1.21 + """
1.22 + Return matching pages using the given 'request' and search 'query'. Optional
1.23 + keyword arguments are passed to the underlying search infrastructure.
1.24 + """
1.25 +
1.26 + results = searchPages(request, query, **kw)
1.27 + return results.hits
1.28 +
1.29 +def getFirstPageHeading(request, page, min_level=1, max_level=1):
1.30 +
1.31 + """
1.32 + Using the given 'request', return the first heading in the given 'page'
1.33 + having a heading level of at least 'min_level' (which is 1 if not specified)
1.34 + and at most 'max_level' (which is 1 if not specified).
1.35 + """
1.36 +
1.37 + full_page = Page(request, page.page_name)
1.38 + body = full_page.get_raw_body()
1.39 +
1.40 + for match in heading_regexp.finditer(body):
1.41 + level = len(match.group("level"))
1.42 +
1.43 + if (min_level is None or level >= min_level) and \
1.44 + (max_level is None or level <= max_level):
1.45 +
1.46 + return match.group("heading")
1.47 +
1.48 + return None
1.49 +
1.50 +def formatResultPages(request, formatter, pages, paging, format):
1.51 +
1.52 + """
1.53 + Using the given 'request' and 'formatter', return a formatted string showing
1.54 + the result 'pages', providing paging controls when 'paging' is set to a true
1.55 + value, and providing page details according to the given 'format'.
1.56 + """
1.57 +
1.58 + m = heading_options_regexp.search(format)
1.59 + if m:
1.60 + format = "heading"
1.61 + min_level = int_or_none(m.group("min"))
1.62 + max_level = int_or_none(m.group("max"))
1.63 + else:
1.64 + format = "name"
1.65 +
1.66 + output = []
1.67 +
1.68 + output.append(formatter.number_list(on=1))
1.69 +
1.70 + for page in pages:
1.71 + output.append(formatter.listitem(on=1))
1.72 +
1.73 + if format == "heading":
1.74 + text = getFirstPageHeading(request, page, min_level, max_level) or page.page_name
1.75 + else:
1.76 + text = page.page_name
1.77 +
1.78 + output.append(formatter.pagelink(on=1, pagename=page.page_name))
1.79 + output.append(formatter.text(text))
1.80 + output.append(formatter.pagelink(on=0))
1.81 + output.append(formatter.listitem(on=0))
1.82 +
1.83 + output.append(formatter.number_list(on=0))
1.84 +
1.85 + return "".join(output)
1.86 +
1.87 +def int_or_none(x):
1.88 + if x is None:
1.89 + return x
1.90 + else:
1.91 + return int(x)
1.92 +
1.93 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/instmacros Fri Jun 25 01:48:21 2010 +0200
2.3 @@ -0,0 +1,13 @@
2.4 +#!/bin/bash
2.5 +
2.6 +if [[ ! $1 ]] ; then
2.7 + echo "Please specify a directory such as ../mysite/wiki or /tmp/mysite/wiki."
2.8 + echo "This should be the root of your Wiki installation and contain the data"
2.9 + echo "directory."
2.10 + echo
2.11 + echo "You may wish to uncomment and modify the chown command in this script."
2.12 + exit
2.13 +fi
2.14 +
2.15 +cp macros/*.py $1/data/plugin/macro/
2.16 +#chown www-data: $1/data/plugin/macro/*.py
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/macros/PageListPlus.py Fri Jun 25 01:48:21 2010 +0200
3.3 @@ -0,0 +1,84 @@
3.4 +# -*- coding: iso-8859-1 -*-
3.5 +"""
3.6 + MoinMoin - PageListPlus
3.7 +
3.8 + print a list of pages whose title matches the search term
3.9 +
3.10 + @copyright: 2010 Paul Boddie <paul@boddie.org.uk>
3.11 + @license: GNU GPL, see COPYING for details.
3.12 +"""
3.13 +
3.14 +Dependencies = ["pages"]
3.15 +from MoinMoin import wikiutil
3.16 +import ImprovedMoinSearch
3.17 +import re
3.18 +
3.19 +format_pattern = re.compile("""format:(?P<quote>['"])(?P<format>.*?)(?P=quote)""")
3.20 +paging_pattern = re.compile("""paging:(?P<paging>on|off|yes|no|true|false)""")
3.21 +
3.22 +def extract_option(query, pattern, group):
3.23 +
3.24 + """
3.25 + Remove from 'query', using the given regular expression 'pattern' object,
3.26 + a particular form of argument text, accessing such text through the group
3.27 + with the given 'group' name, and returning a tuple of the form
3.28 + (edited_query, value_of_group).
3.29 + """
3.30 +
3.31 + m = format_pattern.search(query)
3.32 + if m:
3.33 + value = m.group(group)
3.34 + query = query[:m.start()] + query[m.end():]
3.35 + else:
3.36 + value = None
3.37 + return query, value
3.38 +
3.39 +# Macro functions.
3.40 +
3.41 +def execute(macro, args):
3.42 +
3.43 + """
3.44 + Execute the 'macro' with the given 'args':
3.45 + """
3.46 +
3.47 + request = macro.request
3.48 + formatter = macro.formatter
3.49 + _ = macro._
3.50 +
3.51 + # If called with empty or no argument, default to regex search for .+, the full page list.
3.52 +
3.53 + needle = wikiutil.get_unicode(request, args, 'needle', u'regex:.+')
3.54 +
3.55 + # Extract format and paging options.
3.56 +
3.57 + needle, format = extract_option(needle, format_pattern, "format")
3.58 + needle, paging = extract_option(needle, paging_pattern, "paging")
3.59 +
3.60 + # With whitespace argument, return same error message as FullSearch.
3.61 +
3.62 + if not needle.strip():
3.63 + err = _('Please use a more selective search term instead of {{{"%s"}}}', wiki=True) % needle
3.64 + return '<span class="error">%s</span>' % err
3.65 +
3.66 + # Return a title search for needle, sorted by name.
3.67 +
3.68 + try:
3.69 + pages = ImprovedMoinSearch.getSearchResultPages(request, needle, titlesearch=1, sort='page_name')
3.70 +
3.71 + # Format the results.
3.72 +
3.73 + ret = ImprovedMoinSearch.formatResultPages(request, formatter, pages, paging, format)
3.74 +
3.75 + except ValueError:
3.76 +
3.77 + # Same error as in MoinMoin/action/fullsearch.py, keep it that way!
3.78 +
3.79 + ret = ''.join([formatter.text('<<PageListPlus('),
3.80 + _('Your search query {{{"%s"}}} is invalid. Please refer to '
3.81 + 'HelpOnSearching for more information.', wiki=True,
3.82 + percent=True) % wikiutil.escape(needle),
3.83 + formatter.text(')>>')])
3.84 +
3.85 + return ret
3.86 +
3.87 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/setup.py Fri Jun 25 01:48:21 2010 +0200
4.3 @@ -0,0 +1,13 @@
4.4 +#! /usr/bin/env python
4.5 +
4.6 +from distutils.core import setup
4.7 +
4.8 +setup(
4.9 + name = "ImprovedMoinSearch",
4.10 + description = "Augment MoinMoin search support with enhanced search-related macros",
4.11 + author = "Paul Boddie",
4.12 + author_email = "paul@boddie.org.uk",
4.13 + url = "http://moinmo.in/MacroMarket/ImprovedMoinSearch",
4.14 + version = "0.1",
4.15 + py_modules = ["ImprovedMoinSearch"]
4.16 + )