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