# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Create an link to a slide of a SlideShow

    Usage:

        [[GoToSlide(text)]]
        
        or if the text is a number use:
        
        [[GoToSlide(,text)]]

        [[GoToSlide(slide)]]

        Create a link to page with ?action=SlideShow&n=slide and the text action

        [[Action(slide, text)]]

        Same with custom text.

    @copyright: 2008 by Richard Flieger
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin import wikiutil
from MoinMoin.Page import Page

Dependencies = ["language"]


class Link:
    """ Link - link to a slide of a slideshow """

    arguments = ['slide', 'text']

    def __init__(self, macro, args):
        self.macro = macro
        self.request = macro.request
        self.args = self.getArgs(args)

    def getArgs(self, string):
        """ 
        @param string: string from the wiki markup [[NewPage(string)]]
        @rtype: dict
        @return: dictionary with macro options
        """
        if not string:
            return {}
        args = [s.strip() for s in string.split(',')]
        args = dict(zip(self.arguments, args))
        return args

    def renderInText(self):
        """ Render macro in text context

        The parser should decide what to do if this macro is placed in a
        paragraph context.
        """
        _ = self.request.getText

        # Default to show page instead of an error message (too lazy to
        # do an error message now).
        action = self.args.get('action', 'show')
        
        # Use translated text or action name
        slide = 0
        # Escape user input
        slide = self.args.get('slide', slide)
        slide = wikiutil.escape(slide, 1)
        text = self.args.get('text', slide)
        text = _(text, formatted=False)       
        text = wikiutil.escape(text, 1)

        # Create link
        formatter = self.macro.formatter
        page = wikiutil.quoteWikinameURL(formatter.page.page_name)
        url = '%s?action=SlideShow&n=%s' % (page, slide)
        link = wikiutil.link_tag(self.request, url, text=text,
                                 formatter=formatter)
        return link


def execute(macro, args):
    """ Temporary glue code to use with moin current macro system """
    return Link(macro, args).renderInText()


