# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - ShortText shows some text of a wiki page 

    PURPOSE:
        This macro is used to show the first 180 chars of a wikipage without processing instructions. 

    SYNTAX:
        <<ShortText(WikiName [,length=180])>>

    PROCEDURE:
       This routine needs the macro RecommendPage

       Please remove the version number from the filename.

    MODIFICATION HISTORY:
        @copyright: 2005 by MoinMoin:ReimarBauer
        @license: GNU GPL, see COPYING for details.
        Version: 1.3.4-1
        2005-09-09 : Version 1.3.5-2 : bug fixed if acl does not allow to read the routine has crashed before
        2007-01-10 : Version 1.6.0-3 : ported to 1.6 and added additional parameter for length, removed dependency to RecommendedPage


"""
Dependencies = ["pages"]

from MoinMoin.Page import Page
from MoinMoin import wikiutil

def macro_ShortText(macro, pagename=unicode, length=180):
    request = macro.request
    _ = request.getText

    if request.user.may.read(pagename):
        raw = Page(request, pagename).get_data()
        limit = min([len(raw), length])
        return request.formatter.text(raw[:length])
    else:
      return _("You are not allowed to view this page.")


def execute(macro, args):
    try:
        return wikiutil.invoke_extension_function(
                   macro.request, macro_ShortText,
                   args, [macro])
    except ValueError, err:
        return macro.request.formatter.text(
                   "<<Example: %s>>" % err.args[0])

