# -*- coding: iso-8859-1 -*-
"""
    ChangeLink
    renames virtual [[attachment:file.ext||&do=get]] to [[attachment:file.ext||&do=view]]

    @copyright: 2008 MoinMoin:ReimarBauer
    @license: GNU GPL, see COPYING for details.
"""
from MoinMoin import config, wikiutil
from MoinMoin.Page import Page
from MoinMoin.parser.text_moin_wiki import Parser as WikiParser

def execute(pagename, request):
    _ = request.getText
    msg = None

    if not request.user.may.read(pagename):
        msg = _("You are not allowed to view this page!")

    if msg:
        request.theme.add_msg(msg, state)
        Page(request, pagename).send_page()
        return

    request.formatter.page = Page(request, pagename)
    raw = Page(request, pagename).get_raw_body()
    # of course we need some regex from the parser here to handle attachment markup
    # that is just to show the idea
    raw = raw.replace('&do=get', '&do=view')

    html = wikiutil.renderText(request, WikiParser, raw)

    mimetype = "text/html"
    request.emit_http_headers(["Content-Type: %s; charset=%s" % (mimetype, config.charset)])

    request.theme.send_title(pagename,
                             pagename=pagename,
                             )

    request.write(request.formatter.startContent("content"))

    request.write(request.formatter.rawHTML(html))
    request.write(request.formatter.endContent())
    request.write(request.theme.send_footer(pagename))
    request.theme.send_closing_html()
