Attachment 'ImageLink-1.5.2-12.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - ImageLink Macro
4
5 PURPOSE:
6
7 This macro is used to make a link that displays an image (can be given as either attachment or URL) and links to either an URL
8 or a wiki page. Optionally the size of the image can be adjusted. If no target is given the link will point to the image
9 itself.
10
11 CALLING SEQUENCE:
12 [[ImageLink(attachment|URL,[WikiName|URL],[width=width,[height=heigt]])]]
13
14 INPUTS:
15 attachment:image name of attachment or the URL of an image
16
17 OPTIONAL INPUTS:
18 WikiName: the page to set the link to or the URL to link to
19
20
21 KEYWORD PARAMETERS:
22 width: width of the embedded image
23 height: height of the embedded image
24
25 EXAMPLE:
26 [[ImageLink(München.png,München,height=100)]]
27 [[ImageLink(Images/München.png,München,height=100)]]
28 [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,München Marienplatz)]]
29 [[ImageLink(plot.png,width=200)]]
30 [[ImageLink(plot.png,height=200)]]
31 [[ImageLink(plot.png)]]
32 [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,http://www.muenchen.de,width=150)]]
33 [[ImageLink(münchen.png,http://www.muenchen.de,width=50)]]
34 [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg)]]
35 [[ImageLink(example.png,alt=whateveryouwant(üöä))]]
36
37 PROCEDURE:
38 From JeffKunce ImageLink.py I have copied _is_URL to this routine. I have no better idea too.
39
40 HISTORY:
41 The first published version on MoinMoin I know of ImageLink was written by JeffKunce in 2001.
42
43 MODIFICATION HISTORY:
44 @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
45 @license: GNU GPL, see COPYING for details.
46
47 Marcin Zalewski:
48 Some things that were causing problems on my wiki are changed
49 (wikiutil.link_tag and macro.formatter.pagelink implemented)
50
51 Marcin Zalewski:
52 Added title attribute to the created link. One could generalize that to
53 add arbitrary attributes.
54
55 One could also add class attributes to <a> and/or
56 <img> elements. I do not see the need for such modifications. If however this is
57 to be done in the future one would need to add 'html_class' key to the kw dictionary
58 with a corresponding value to add class to <img> element. To add class to <a> element
59 one needs to add 'css_class' key with a corresponding value to the dictionary passed to
60 pagelink call.
61 Reimar Bauer:
62 2004-12-23 Adopted to MoinMoin Version 1.3.1-1
63 2004-12-23 SYNTAX CHANGE Version 1.3.1-2
64 width and height and probably other keywords must be called as keywords (e.g. height=20)
65 2004-12-31 Version 1.3.1-3 code clean up
66 2005-01-16 Bug fixed in the errorhandler found and patch code from Malte Helmert
67 2005-03-05 Version 1.3.3-5 Bug fixed found by cypress
68 ''If I put [[ImageLink(moinmoin.png)]] it bombs''
69 2005-03-28 Version 1.3.3-6 feature request added by CDPark:
70 ''Can we use an external image? And an external target? ''
71 2005-04-16 Version 1.3.3-7 no default alt tag definition requested by CDPark and AlexanderSchremmer
72 Chong-Dae Park:
73 2005-04-17 Version 1.3.3-8 code refactored
74 IMG with no alt tag is not recommended in the HTML standard.
75 It keeps user specified alt tag. If it is not, it tries to use WikiName or image name instead.
76 Reimar Bauer:
77 2005-04-21 Version 1.3.3-9 bug fixed
78 When the image does not exist yet, it gives you a "Upload Image" link, this link does not
79 work. I suspect that this only is a problem on sub-pages, caused by incorrect escaping of
80 "/". -- CraigJohnson
81
82 2005-12-19 Versiom 1.5.0-10 feature added to link to images on different wiki pages
83 2006-02-14 Version 1.5.2-11 bug fixed for filename of attached image is Chinese (encode added)
84 2006-02-22 Version 1.5.2-12 code refactored
85
86 """
87 import os
88 from MoinMoin.action import AttachFile
89 from MoinMoin import wikiutil, config
90
91
92 def _is_URL(text):
93 '''answer true if text is a URL.
94 The method used here is pretty dumb. Improvements are welcome.
95 jjk 03/28/01'''
96 if '://' in text:
97 return True
98
99 def execute(macro, text):
100 kw = {} # create a dictionary for the formatter.image call
101 if text:
102 arguments = text.split(',')
103 else:
104 arguments = []
105
106 number_args = len(arguments)
107 count = 0
108 for arg in arguments:
109 if '=' in arg:
110 count += 1
111 key = arg.split('=')
112 kw[str(key[0])] = ''.join(key[1])
113
114 number_args = number_args - count
115
116 if number_args < 1:
117 msg='Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png,WikiName,width=200)]].'
118 return "%s%s%s" % (macro.formatter.sysmsg(1),
119 macro.formatter.text(msg),
120 macro.formatter.sysmsg(0))
121
122 attname = arguments[0]
123
124 if number_args >= 2:
125 wikiname = arguments[1]
126
127 if attname.find('/') > -1 and attname.find(':') == -1:
128 current_pagename, attname = attname.split('/')
129 else:
130 current_pagename = macro.formatter.page.page_name
131
132 if _is_URL(arguments[0]):
133 kw['src'] = arguments[0]
134 else:
135 kw['src'] = AttachFile.getAttachUrl(current_pagename,attname,macro.request)
136 attachment_path = os.path.join(AttachFile.getAttachDir(macro.request,current_pagename), attname).encode(config.charset)
137
138 if not os.path.exists(attachment_path):
139 import urllib
140 linktext = macro.request.getText('Upload new attachment "%(filename)s"' % {
141 "filename": attname})
142 return wikiutil.link_tag(macro.request,
143 "%(pagename)s?action=AttachFile&rename=%(newname)s" % {
144 "pagename": current_pagename,
145 "newname": attname}, linktext)
146
147 if not kw.has_key('alt'):
148 if number_args == 1 or _is_URL(arguments[1]):
149 if _is_URL(arguments[0]):
150 # Get image name http://here.com/dir/image.png -> image.png
151 kw['alt'] = wikiutil.taintfilename(macro.formatter.text(arguments[0].split('/')[-1]))
152 kw['alt'] = arguments[0].split('/')[-1]
153 else:
154 kw['alt'] = attname
155 else:
156 kw['alt'] = wikiname
157
158 if number_args == 1:
159 return "%s%s%s" % (macro.formatter.url(1,kw['src']),
160 macro.formatter.image(**kw),
161 macro.formatter.url(0))
162
163 if number_args == 2:
164 if _is_URL(arguments[1]):
165 return "%s%s%s" % (macro.formatter.url(1,arguments[1]),
166 macro.formatter.image(**kw),
167 macro.formatter.url(0))
168 else:
169 return "%s%s%s" % (macro.formatter.pagelink(1,wikiname),
170 macro.formatter.image(**kw),
171 macro.formatter.url(0))
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.