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

    PURPOSE:
        This macro is used to set a link as WikiName for an attached image. Optional the size of the
	image could be adjusted.

    CALLING SEQUENCE:
        [[ImageLink(attachment,WikiName,[width,[height]])]]

    INPUTS:
        attachment:image name of attachment
	WikiName: the page to set the link to

    OPTIONAL INPUTS:
        width: width of the embedded image
	height: height of the embedded image

    EXAMPLE:
      [[ImageLink(plot.gif,FrontPage,20,20)]]
      [[ImageLink(plot.gif,FrontPage)]]

    PROCEDURE:
      This routine requires attachment enabled. If the attachment isn't downloaded at all
      the attachment line is shown.

      It must be in "MoinMoin/macro"


    @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
    @license: GNU GPL, see COPYING for details.
"""

#Dependencies = []

from MoinMoin.action import AttachFile
import string,os


def execute(macro, args):

   width=''
   height=''
   n_args=string.count(args,',')+1

   sargs=args.split(',')
   attname=string.join(sargs[0],'')
   link=string.join(sargs[1],'')


   pagename=macro.formatter.page.page_name
   url=AttachFile.getAttachUrl(pagename,attname,macro.request)

   attach_dir=AttachFile.getAttachDir(pagename,create=1)
   if not os.path.isfile(attach_dir+'/'+attname):
      return '<a href="/'+pagename+'?action=AttachFile&amp;rename='+attname+'">Upload new attachment "'+attname+'"</a>'


   substring=''
   if (n_args >= 3):
     width=string.join(sargs[2],'')
     substring=' width="'+width+'"'
   if (n_args == 4):
      height=string.join(sargs[3],'')
      substring=substring+' height="'+height+'"'


   return '<a title="'+link+'" href='+link+'> <img src="' + url+'"'+' '+substring+'> </a>'
