# -*- coding: iso-8859-1 -*-
"""
MoinMoin - IMG Macro

Creates an image with optional link.

Usage:

    [[IMG(src)]] - create an image using src url

    [[IMG(src, href)]] - create an image using src, linkinng to href

This macro is useful when your image url does not use the common format
like png, gif or jpg, or created dynamically by another script, like
src="http://example.com/image.php?name=123"

@copyright: 2005 by Josselin Mouette, Nir Soffer
@license: GNU GPL, see COPYING for details.
"""

Dependencies = []

def execute(macro, args):
    # Check arguments, return raw markup for bad call
    if args == None:
        return macro.formatter.text('[[IMG]]')
    arguments = [item.strip() for item in args.split(',')]
    # It does not make sense to call with empty argument for this script
    if len(arguments) > 2 or '' in arguments:
        return macro.formatter.text('[[IMG(%s)]]' % args)

    # Render a link with an image or just an image
    f = macro.formatter
    try:
        src, href = arguments
        return f.url(1, href) + f.image(src=src) + f.url(0)
    except ValueError:
        src = arguments[0]
        return f.image(src=src)
