I wanted to create a dynamic portal like function for my wiki, where the front page would include a couple of personal pages belonging to the logged in user that is lookin at the page.. so I created this macro... UserIncludePage.py

"""
    MoinMoin - UserIncludePage macro

    version 1.0
    JonathanDietrich

    Allows for dynamic additions based on UserName
    [[UserIncludePage(page, "heading", level, force)]]

        page will be added to the end of the the current user's name
        heading is the standard include heading
        level is the standard include level
        force if set to force or 1 will force the page to show, even if it doesn't currently exist

    [[UserIncludePage(/Notes,'Personal Notes',2, 1)]]

       this will include UserName/Notes with the heading of Personal Notes down to level 2
       with the force set to 1, this page will be included even if it doesn't exist


    $Id$
"""

import re
from MoinMoin import config
from MoinMoin import wikiutil
import MoinMoin.macro.IncludePages
from MoinMoin.Page import Page

_arg_level = r',\s*(?P<level>\d+)'
_arg_force  = r',\s*(?P<force>.+?)'
_arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
_args_re_pattern = r'^(?P<pattern>[^,]+)?(%s)?(%s)?(%s)?$' % (_arg_heading, _arg_level, _arg_force )


def execute(macro, text, args_re=re.compile(_args_re_pattern)):

    # parse and check arguments
    args = args_re.match(text)
    if not args:
        return ('<p><strong class="error">%s</strong></p>' %
            _('Invalid include calendar page arguments "%s"!')) % (text,)

    # get the arguments
    username = macro.request.user.name
    if args.group('pattern') is None:
        pattern = username
    else:
        pattern = username + args.group('pattern')
    if args.group('level') is None:
        level = 1
    else:
        level = int(args.group('level'))
    if  args.group('htext') is None:
         heading = ""
    else:
         heading =   args.group('htext')
    params = '%s, "%s", %d' % (pattern, heading, level)
    targetpage = Page(pattern)
    if args.group('force') is None:
        force = 0
    elif args.group('force') == '1' or args.group('force') == 'force'  :
        force = 1
    else:
        force = 0
    if  (targetpage.exists() or force )  and macro.request.user.valid :
     return MoinMoin.macro.Include.execute(macro, params)
    else:
     return ''

MoinMoin: JonathanDietrich/UserIncludePage (last edited 2007-10-29 19:20:45 by localhost)