view data/plugin/macro/ @ 526:56cbdae11f25

macro.ListPages: new keyword list_type for selecting number or bullet list
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 16 May 2010 13:26:17 +0200
parents 2bf20bdbbde9
children 09b97ed541e2
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - ListPages

    print a list of pages whose title matches the filter term of getPageList

    @copyright: @copyright: 2001-2003 Juergen Hermann <>,
                2003-2008 MoinMoin:ThomasWaldmann
                2009-2010 MoinMoin:ReimarBauer
    @license: GNU GPL, see COPYING for details.

Dependencies = ["namespace"]

import re
from MoinMoin import search, wikiutil

def macro_ListPages(macro, search_term=u'.+', list_type=("number_list", "bullet_list")):
    lists pages
    @param search_term: regex for searching of page titles
    @param list_type: select type of list in between number_list and bullet_list
    _ = macro._
    case = 0

    # With whitespace argument, return same error message as FullSearch
    if not search_term.strip():
        msg = _('Please use a more selective search term instead of {{{"%s"}}}', wiki=True) % search_term
        return '<span class="error">%s</span>' % msg

    # Return a title search for needle, sorted by name.
    # based on rootpage.getPageList because search does also list attachments
        rootpage = macro.request.rootpage
        filterfn = re.compile(ur"%s" % search_term, re.U).match
        pageobjs = rootpage.getPageList(include_underlay=False, exists=1, filter=filterfn, return_objects=True)
        f = macro.request.formatter
        ret = [''.join([f.listitem(1),
               f.url(1, href=page.url(macro.request, escape=0)),
               f.url(0)]) for page in pageobjs]
        if list_type == "number_list":
            ret = ''.join([f.number_list(1), ''.join(ret), f.number_list(0)])
        if list_type == "bullet_list":
            ret = ''.join([f.bullet_list(1), ''.join(ret), f.bullet_list(0)])
    except ValueError:
        # same error as in MoinMoin/action/, keep it that way!
        ret = ''.join([macro.formatter.text('<<PageList('),
                      _('Your search query {{{"%s"}}} is invalid. Please refer to '
                        'HelpOnSearching for more information.', wiki=True,
                        percent=True) % wikiutil.escape(search_term),
    return ret