# -*- 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. If no WikiName is given the attachment is linked to the image itselfs.
CALLING SEQUENCE:
[[ImageLink(attachment,[WikiName],[width=width,[height=heigt]])]]
INPUTS:
attachment:image name of attachment
WikiName: the page to set the link to
KEYWORD PARAMETERS:
width: width of the embedded image
height: height of the embedded image
EXAMPLE:
[[ImageLink(plot.gif,FrontPage,width=20,height=20)]]
[[ImageLink(plot.gif,FrontPage,height=20)]]
[[ImageLink(plot.gif,FrontPage)]]
[[ImageLink(plot.gif,width=20,height=20)]]
[[ImageLink(plot.gif,width=20)]]
PROCEDURE:
This routine requires attachment enabled. If the attachment isn't downloaded at all
the attachment line is shown.
If you give only one image size argument e.g. width only the other one is calculated
It must be in "MoinMoin/macro"
Please remove the version number from the file name!
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 and/or
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
element. To add class to element
one needs to add 'css_class' key with a corresponding value to the dictionary passed to
pagelink call.
Reimar Bauer:
2004-12-23 Adopted to MoinMoin Version 1.3.1-1
2004-12-23 SYNTAX CHANGE Version 1.3.1-2
width and height and probably other keywords must be called as keywords (e.g. height=20)
2004-12-31 Version 1.3.1-3 code clean up
"""
from MoinMoin.action import AttachFile
from MoinMoin import wikiutil, config
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)
count=0
for a in args :
if (a.find('=') > -1):
count=count+1
key=a.split('=')
kw[str(key[0])]=wikiutil.escape(string.join(key[1],''), quote=1)
number_args=number_args-count
if (number_args < 1):
return macro.formatter.sysmsg('Not enough arguments to ImageLink macro')
attname=wikiutil.taintfilename(macro.formatter.text(args[0]))
wikiname=args[1]
current_pagename=macro.formatter.page.page_name
kw['src']=AttachFile.getAttachUrl(current_pagename,attname,macro.request)
attachment_path = os.path.join(AttachFile.getAttachDir(macro.request,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&rename=%s' %
(wikiutil.quoteWikinameFS(current_pagename),
urllib.quote_plus(attname)),
linktext % {'filename': attname})
if (number_args == 1):
kw['title']=attname
kw['alt']=attname
image_link=macro.formatter.image(**kw)
return macro.formatter.url(1,kw['src'] )+image_link +macro.formatter.url(0)
if (number_args == 2 ):
kw['title']=wikiname
kw['alt']=wikiname
image_link=macro.formatter.image(**kw)
return macro.formatter.pagelink(1,wikiname)+image_link+macro.formatter.url(0)