Attachment 'ImageLink-1.2.3-3.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.
8
9 CALLING SEQUENCE:
10 [[ImageLink(attachment,WikiName,[width,[height]])]]
11
12 INPUTS:
13 attachment:image name of attachment
14 WikiName: the page to set the link to
15
16 OPTIONAL INPUTS:
17 width: width of the embedded image
18 height: height of the embedded image
19
20 EXAMPLE:
21 [[ImageLink(plot.gif,FrontPage,20,20)]]
22 [[ImageLink(plot.gif,FrontPage)]]
23
24 PROCEDURE:
25 This routine requires attachment enabled. If the attachment isn't downloaded at all
26 the attachment line is shown.
27
28 It must be in "MoinMoin/macro"
29
30 MODIFICATION HISTORY:
31 @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
32 @license: GNU GPL, see COPYING for details.
33
34 Marcin Zalewski:
35 Some things that were causing problems on my wiki are changed
36 (wikiutil.link_tag and macro.formatter.pagelink implemented)
37
38 Marcin Zalewski:
39 Added title attribute to the created link. One could generalize that to
40 add arbitrary attributes.
41
42 One could also add class attributes to <a> and/or
43 <img> elements. I do not see the need for such modifications. If however this is
44 to be done in the future one would need to add 'html_class' key to the kw dictionary
45 with a corresponding value to add class to <img> element. To add class to <a> element
46 one needs to add 'css_class' key with a corresponding value to the dictionary passed to
47 pagelink call.
48
49 """
50
51 #Dependencies = []
52
53 from MoinMoin.action import AttachFile
54 from MoinMoin import wikiutil
55 import os,string
56
57
58 def execute(macro, text):
59
60 kw ={} # create a dictionary for the formatter.image call
61
62 if text:
63 args=text.split(',')
64 else:
65 args=[]
66 number_args=len(args)
67 if number_args < 2:
68 return macro.formatter.sysmsg('Not enough arguments to ImageLink macro')
69 attname=wikiutil.taintfilename(macro.formatter.text(args[0]))
70 linked_page=macro.formatter.text(args[1])
71 current_pagename=macro.formatter.page.page_name
72 kw['src']=AttachFile.getAttachUrl(current_pagename,attname,macro.request)
73
74 # Check if attachment used for image link exists
75 attachment_path = os.path.join(AttachFile.getAttachDir(current_pagename), attname)
76 if not os.path.exists(attachment_path):
77 import urllib
78 linktext = macro.request.getText('Upload new attachment "%(filename)s"')
79 return wikiutil.link_tag(macro.request,
80 '%s?action=AttachFile&rename=%s' %
81 (wikiutil.quoteWikiname(current_pagename),
82 urllib.quote_plus(attname)),
83 linktext % {'filename': attname})
84
85
86
87 if (number_args >= 3 and args[2] != ''):
88 kw['width']=wikiutil.escape(string.join(args[2],''), quote=1)
89 if (number_args >= 4 and args[3] != ''):
90 kw['height']=wikiutil.escape(string.join(args[3],''), quote=1)
91 image_link=macro.formatter.image(**kw)
92 pagelink_attrs='title="'+wikiutil.escape(linked_page, quote=1)+'"'
93 return macro.formatter.pagelink(linked_page, image_link, attrs=pagelink_attrs)
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.