Attachment 'ImageLink-1.5.0-10.py'
Download 1 # -*- coding: iso-8859-1 -*-
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
91 """
92
93 from MoinMoin.action import AttachFile
94 from MoinMoin import wikiutil, config
95 import os,string
96
97 def _is_URL(aString):
98 '''answer true if aString is a URL.
99 The method used here is pretty dumb. Improvements are welcome.
100 jjk 03/28/01'''
101 return string.find(aString, '://')>0
102
103
104 def execute(macro, text):
105
106 kw = {} # create a dictionary for the formatter.image call
107
108 if text:
109 args = text.split(',')
110 else:
111 args = []
112
113 number_args = len(args)
114 count = 0
115 for a in args :
116 if a.find('=') > -1 :
117 count += 1
118 key = a.split('=')
119 kw[str(key[0])] = wikiutil.escape(string.join(key[1],''), quote=1)
120
121 number_args = number_args - count
122
123 if number_args < 1:
124 msg='Not enough arguments to ImageLink macro! Try [[ImageLink(attachment,[WikiName],[width=width,[height=heigt]])]]'
125 return macro.formatter.sysmsg(1) + macro.formatter.text(msg) + macro.formatter.sysmsg(0)
126
127
128 attname = args[0]
129 if number_args >= 2 :
130 wikiname = args[1]
131
132 if attname.find('/') > -1 and attname.find(':') == -1:
133 current_pagename,attname = string.split(attname,'/')
134 else:
135 current_pagename = macro.formatter.page.page_name
136
137
138
139 if _is_URL(args[0]):
140 kw['src'] = args[0]
141 else:
142 kw['src'] = AttachFile.getAttachUrl(current_pagename,attname,macro.request)
143 attachment_path = os.path.join(AttachFile.getAttachDir(macro.request,current_pagename), attname)
144
145 if not os.path.exists(attachment_path):
146
147 import urllib
148 linktext = macro.request.getText('Upload new attachment "%(filename)s"'%{
149 "filename":attname})
150
151 return wikiutil.link_tag(macro.request,
152 "%(pagename)s?action=AttachFile&rename=%(newname)s" % {
153 "pagename":current_pagename,
154 "newname": attname},linktext)
155
156
157
158 if not kw.has_key('alt'):
159 if number_args == 1 or _is_URL(args[1]):
160 if _is_URL(args[0]):
161 # Get image name http://here.com/dir/image.gif -> image.gif
162 kw['alt'] = wikiutil.taintfilename(macro.formatter.text(args[0].split('/')[-1]))
163 kw['alt'] = args[0].split('/')[-1]
164 else:
165 kw['alt'] = attname
166 else:
167 kw['alt'] = wikiname
168
169 if number_args == 1:
170 image_link=macro.formatter.image(**kw)
171 return macro.formatter.url(1,kw['src'] ) + image_link + macro.formatter.url(0)
172
173 if number_args == 2:
174 image_link=macro.formatter.image(**kw)
175 if _is_URL(args[1]):
176 return macro.formatter.url(1,args[1] ) + image_link + macro.formatter.url(0)
177 else:
178 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.