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