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
84
2006-09-22 bug fix of image linked to attachment and inline
85
2006-10-08 patch of DavidLinke added and keys now only lowercase used
87
@copyright: 2001 by Jeff Kunce,
88
2004 by Marcin Zalewski,
89
2004-2006 by Reimar Bauer (R.Bauer@fz-juelich.de),
90
2006 by Thomas Waldmann
91
@license: GNU GPL, see COPYING for details.
95
from MoinMoin import wikiutil, config
96
from MoinMoin.action import AttachFile
98
kwAllowed = ['width', 'height', 'alt']
101
""" Answer true if text is an URL.
102
The method used here is pretty dumb. Improvements are welcome.
106
def execute(macro, args):
107
request = macro.request
109
formatter = macro.formatter
111
args = args.split(',')
112
args = [arg.strip() for arg in args]
118
kw = {} # create a dictionary for the formatter.image call
121
key, value = arg.split('=', 1)
122
# avoid that urls with "=" are interpreted as keyword
123
if key.lower() not in kwAllowed: continue
125
kw[str(key.lower())] = wikiutil.escape(value, quote=1)
128
if not argc or argc and not args[0]:
129
msg = 'Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png, WikiName, width=200)]].'
130
return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
133
if argc >= 2 and args[1]:
135
if target.startswith('attachment:') or target.startswith('inline:'):
136
if target.startswith('attachment:'):
137
target = (target.split('attachment:'))[1]
138
pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
139
target = AttachFile.getAttachUrl(pagename, target, request)
140
elif target.startswith('inline:'):
141
target = (target.split('inline:'))[1]
142
pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
143
target = AttachFile.getAttachUrl(pagename, target, request, do='view')
145
attachment_fname = AttachFile.getFilename(request, pagename, attname)
146
if not os.path.exists(attachment_fname):
147
linktext = _('Upload new attachment "%(filename)s"')
148
return wikiutil.link_tag(request,
149
('%s?action=AttachFile&rename=%s' % (
150
wikiutil.quoteWikinameURL(pagename),
151
wikiutil.url_quote_plus(attname))),
152
linktext % {'filename': attname})
154
kw['src'] = AttachFile.getAttachUrl(pagename, image, request)
157
pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
158
target = AttachFile.getAttachUrl(pagename, image, request)
165
pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
166
kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
167
attachment_fname = AttachFile.getFilename(request, pagename, attname)
168
if not os.path.exists(attachment_fname):
169
linktext = _('Upload new attachment "%(filename)s"')
170
return wikiutil.link_tag(request,
171
('%s?action=AttachFile&rename=%s' % (
172
wikiutil.quoteWikinameURL(pagename),
173
wikiutil.url_quote_plus(attname))),
174
linktext % {'filename': attname})
176
if not kw.has_key('alt'):
177
if target is None or _is_URL(target):
179
# Get image name http://here.com/dir/image.png -> image.png
180
kw['alt'] = wikiutil.taintfilename(formatter.text(image.split('/')[-1])) # XXX
190
return "%s%s%s" % (formatter.url(1, kw['src']),
191
formatter.image(**kw),
194
if _is_URL(target) or 'action=AttachFile&do=get&target=' in target or 'action=AttachFile&do=view&target=' in target:
195
return "%s%s%s" % (formatter.url(1, target),
196
formatter.image(**kw),
199
return "%s%s%s" % (formatter.pagelink(1, target),
200
formatter.image(**kw),
201
formatter.pagelink(0))