# -*- coding: utf-8 -*-
"""
MoinMoin - HelpTitle macro

This macro allows you to mouseover arbitrary words and phrases and
(depending on your browser support) get tooltip help text.

Making text with a tooltip:
    
    [[HelpTitle(this is the text you see, this is the tooltip)]]

Both parameters are required, or it's kinda pointless.  Also, no
commas or quotes in either the text or the tooltip.  It breaks things.
        
@copyright: 2005 by Vito Miliano <vito_moinhelptitle@hirevito.com>
@license: GNU GPL v2, see COPYING for details.
"""

class HelpTitle:
    """ Arbitrary text with tooltips """

    arguments = ['text', 'tooltip']

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

    def parseArgs(self, string):
        """ Temporary function until Oliver Graf args parser is finished

        @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 errors(self):
        """ Validate arguments and return error message

        @rtype: unicode
        @return: error message for bad argument, or None if ok.
        """
        _ = self.request.getText
        
        # Both parameters are required
        if not self.args.get('text') or not self.args.get('tooltip'):
            error = _('Missing text or tooltip.')
            # Should use abstract formatter.wikiMarkupError() call,
            # but there is no such call, so just use html.
            return u'<span class="error">%s</span>' % error
        
        return None
        
    def text(self):
        """ Return text safe for rendering """
        text = self.args.get('text', u'')
        if text:
            text = self.macro.formatter.text(text)
        return text

    def tooltip(self):
        """ Return tooltip safe for rendering """
        tooltip = self.args.get('tooltip', u'')
        if tooltip:
            tooltip = self.macro.formatter.text(tooltip)
        return tooltip
 
    def renderInText(self):
        """ Render macro in paragraph context

        The parser should decide what to do if this macro is placed in a
        page context.
        
        @rtype: unicode
        @return rendered output
        """
        _ = self.request.getText
       
        errors = self.errors()
        if errors:
            return errors

        text = self.text()
        tooltip = self.tooltip()

        out = u'<span style="cursor: help; border-bottom: 1px dashed;" title="%(tooltip)s">%(text)s</span>' % {
            'text': text,
            'tooltip': tooltip, 
            }        
        return out

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