# -*- coding: utf-8 -*-
"""
    MoinMoin - macro to specify columns

    As far as the author is concerned, this code is released to the public
    domain, but some restrictions in the MoinMoin COPYING file may apply. Ask a
    lawyer.
    ***********
    This code has not been tested exhaustively. Use at your own risk.
    ***********

    This macro generates containers, that can be used with CSS to create
    columns.

    Usage:
        <<Columns(mode,name,keywordargs)>>

        mode            start, next or end.

        name            Name of the div id. <div id="name">

        keywordargs     Zero or more key="value" arguments for the HTML div
                        tag.

    Examples:

        <<Columns(start, leftcolumn)>>
                This text is in the left column.
        <<Columns(next, rightcolumn)>>
                This text is in the right column.
        <<Columns(end)>>

    requires class definitions in CSS for the used column names, eg

    #leftcolumn {
        position: relative;
        background: transparent;
        float: left;
        width: 45%;
        border-right: 1px solid gray;
        padding-left: 5%;
        padding-right: 1%;
        margin: 0px;
    }

    #rightcolumn {
        position: relative;
        background: transparent;
        float: right;
        width: 42%;
        padding-right: 1%;
        margin: 0px;
    }

    Demo. Try pasting the above examples into a MoinMoin sandbox page and the
    current CSS.

    @copyright: Antti Kuntsi <antti@kuntsi.com>
    @license: GNU GPL, see COPYING for details.

    changes:
        12.2007 - conversion to new syntax by Bolesław Kulbabiński
"""

from MoinMoin import wikiutil, config

DEFAULT_KEYWORD_ARGS = {}

def macro_Columns(macro, mode=('start', 'next', 'end'), name=unicode, _kwargs=None):
    # handle the Columns Macro. return the generated display HTML

    if mode != 'end':
        classname = name.strip()
    else:
        classname = ''

    # update keyword arguments
    kwargs = DEFAULT_KEYWORD_ARGS
    if _kwargs is not None:
        kwargs.update(_kwargs)

    # generate a string containing the keyword arguments
    kwstring = ''
    for key in kwargs.keys():
        kwstring = '%s %s="%s"' % (kwstring, key, kwargs[key])

    # generate a string specifying the location of the image
    divtag = ''
    if mode in ('end', 'next'):
            divtag += '</div>'
            if mode == 'end':
                divtag += '<br clear="both">'
    if mode in ('start', 'next'):
            divtag += '<div id="%s"%s>' % (classname, kwstring)

    # combine target location, image location, and keyword arguments into HTML IMG tag
    result = [macro.formatter.paragraph(0),
              macro.formatter.rawHTML(divtag),
              macro.formatter.paragraph(1)]

    return "".join(result)


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

