# -*- 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"

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

	Marcin Zalewski:
	    Some things that were causing problems on my wiki are changed
            (wikiutil.link_tag and macro.formatter.pagelink implemented)

        Marcin Zalewski:
            Added title attribute to the created link. One could generalize that to
            add arbitrary attributes.

            One could also add class attributes to <a> and/or
            <img> elements. I do not see the need for such modifications. If however this is
            to be done in the future one would need to add 'html_class' key to the kw dictionary
            with a corresponding value to add class to <img> element. To add class to <a> element
            one needs to add 'css_class' key with a corresponding value to the dictionary passed to
            pagelink call.

"""

#Dependencies = []

from MoinMoin.action import AttachFile
from MoinMoin import wikiutil
import os,string


def execute(macro, text):

   kw ={} # create a dictionary for the formatter.image call

   if text:
      args=text.split(',')
   else:
      args=[]
   number_args=len(args)
   if number_args < 2:
      return macro.formatter.sysmsg('Not enough arguments to ImageLink macro')
   attname=wikiutil.taintfilename(macro.formatter.text(args[0]))
   linked_page=macro.formatter.text(args[1])
   current_pagename=macro.formatter.page.page_name
   kw['src']=AttachFile.getAttachUrl(current_pagename,attname,macro.request)

   # Check if attachment used for image link exists
   attachment_path = os.path.join(AttachFile.getAttachDir(current_pagename), attname)
   if not os.path.exists(attachment_path):
      import urllib
      linktext = macro.request.getText('Upload new attachment "%(filename)s"')
      return wikiutil.link_tag(macro.request,
                               '%s?action=AttachFile&amp;rename=%s' %
                               (wikiutil.quoteWikiname(current_pagename),
                                urllib.quote_plus(attname)),
                               linktext % {'filename': attname})



   if (number_args >= 3 and args[2] != ''):
      kw['width']=wikiutil.escape(string.join(args[2],''), quote=1)
   if (number_args >= 4 and args[3] != ''):
      kw['height']=wikiutil.escape(string.join(args[3],''), quote=1)
   image_link=macro.formatter.image(**kw)
   pagelink_attrs='title="'+wikiutil.escape(linked_page, quote=1)+'"'
   return macro.formatter.pagelink(linked_page, image_link, attrs=pagelink_attrs)
