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