view data/plugin/macro/ @ 527:09b97ed541e2

macro.ListPages: keyword for selecting link text from pagename or subpage added
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 16 May 2010 14:27:24 +0200
parents 56cbdae11f25
children 814c6075c0b0
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 _subpage(pagename):
    returns the last last segement of a pagename
    @param pagename: the pagename
    segments = pagename.split('/')
    return segments[-1]

def macro_ListPages(macro, search_term=u'.+', list_type=("number_list", "bullet_list"),
                    link=("pagename", "subpage")):
    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
    @param link: change the link from pagename to subpage for shorter links
    _ = 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
        if link == "pagename":
            ret = [''.join([f.listitem(1),
                   f.url(1, href=page.url(macro.request, escape=0)),
                   f.url(0)]) for page in pageobjs]
        if link == "subpage":
             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