1
# -*- coding: iso-8859-1 -*-
3
MoinMoin - ImageLink Macro
5
This macro is used to make a link that displays an image (can be given
6
as either attachment or URL) and links to either an URL or a wiki page.
7
Optionally the size of the image can be adjusted.
8
If no target is given the link will point to the image itself.
11
[[ImageLink(image, [target,] [width=width, [height=height]])]]
14
image: image attachment file name or the URL of an image
15
target: link target wiki page or URL (optional)
18
width: rendered image width (optional)
19
height: rendered image heigth (optional)
20
alt: text for img tag "alt" attribute
23
[[ImageLink(M�nchen.png,M�nchen,height=100)]]
24
[[ImageLink(Images/M�nchen.png,M�nchen,height=100)]]
25
[[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,M�nchen Marienplatz)]]
26
[[ImageLink(plot.png,width=200)]]
27
[[ImageLink(plot.png,height=200)]]
28
[[ImageLink(plot.png)]]
29
[[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,http://www.muenchen.de,width=150)]]
30
[[ImageLink(m�nchen.png,http://www.muenchen.de,width=50)]]
31
[[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg)]]
32
[[ImageLink(example.png,alt=whateveryouwant(���))]]
36
wrote the first published version of this macro in 2001.
39
2004 intitial new version for MoinMoin 1.2
42
Implemented wikiutil.link_tag and macro.formatter.pagelink.
43
Added title attribute to the created link. One could generalize that to
44
add arbitrary attributes.
46
One could also add class attributes to <a> and/or <img> elements.
47
I do not see the need for such modifications. If however this is
48
to be done in the future one would need to add 'html_class' key to the kw dictionary
49
with a corresponding value to add class to <img> element. To add class to <a> element
50
one needs to add 'css_class' key with a corresponding value to the dictionary passed to
54
2004-12-23 Adapted to MoinMoin Version 1.3.1-1
55
2004-12-23 Syntax change Version 1.3.1-2
56
width and height and probably other keywords must be given as keywords (e.g. height=20)
57
2004-12-31 Version 1.3.1-3 code clean up
58
2005-01-16 Bug fixed in the errorhandler - found and patched by Malte Helmert
59
2005-03-05 Version 1.3.3-5 Bug fixed found by cypress ("If I put [[ImageLink(moinmoin.png)]] it bombs")
60
2005-03-28 Version 1.3.3-6 feature request added by CDPark:
61
"Can we use an external image? And an external target?"
62
2005-04-16 Version 1.3.3-7 no default alt tag definition as requested by CDPark and AlexanderSchremmer
65
2005-04-17 Version 1.3.3-8 code refactored
66
IMG with no alt tag is not recommended in the HTML standard.
67
It keeps a user specified alt tag. If there is none, it tries to make on using the WikiName
68
or image name instead.
71
2005-04-21 Version 1.3.3-9 bug fixed
72
When the image does not exist yet, it gives you a "Upload Image" link, this link does not
73
work. I suspect that this only is a problem on sub-pages, caused by incorrect escaping of
76
2005-12-19 Versiom 1.5.0-10 feature added to link to images on different wiki pages
77
2006-02-14 Version 1.5.2-11 bug fixed for filename of attached image is Chinese (encode added)
78
2006-02-22 Version 1.5.2-12 code refactored
81
2006-03-10 code refactored
83
@copyright: 2001 by Jeff Kunce,
84
2004 by Marcin Zalewski,
85
2004-2006 by Reimar Bauer (R.Bauer@fz-juelich.de),
86
2006 by Thomas Waldmann
87
@license: GNU GPL, see COPYING for details.
91
from MoinMoin import wikiutil, config
92
from MoinMoin.action import AttachFile
95
""" Answer true if text is an URL.
96
The method used here is pretty dumb. Improvements are welcome.
100
def execute(macro, args):
101
request = macro.request
103
formatter = macro.formatter
105
args = args.split(',')
106
args = [arg.strip() for arg in args]
112
kw = {} # create a dictionary for the formatter.image call
116
key, value = arg.split('=', 1)
117
kw[str(key)] = wikiutil.escape(value, quote=1)
120
if not argc or argc and not args[0]:
121
msg = 'Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png, WikiName, width=200)]].'
122
return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
125
if argc >= 2 and args[1]:
133
pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
134
kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
135
attachment_fname = AttachFile.getFilename(request, pagename, attname)
136
if not os.path.exists(attachment_fname):
137
linktext = _('Upload new attachment "%(filename)s"')
138
return wikiutil.link_tag(request,
139
('%s?action=AttachFile&rename=%s' % (
140
wikiutil.quoteWikinameURL(pagename),
141
wikiutil.url_quote_plus(attname))),
142
linktext % {'filename': attname})
144
if not kw.has_key('alt'):
145
if target is None or _is_URL(target):
147
# Get image name http://here.com/dir/image.png -> image.png
148
kw['alt'] = wikiutil.taintfilename(formatter.text(image.split('/')[-1])) # XXX
158
return "%s%s%s" % (formatter.url(1, target),
159
formatter.image(**kw),
162
return "%s%s%s" % (formatter.pagelink(1, target),
163
formatter.image(**kw),
164
formatter.pagelink(0))