comparison MoinMoin/macro/EmbedObject.py @ 3277:e79c742eaca7

EmbedObject macro: misc. fixes / cleanups
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 17 Mar 2008 00:00:28 +0100
parents 753f234085c2
children e342c92094f4
comparison
equal deleted inserted replaced
3274:6fe451cd6219 3277:e79c742eaca7
19 from MoinMoin.action import AttachFile 19 from MoinMoin.action import AttachFile
20 20
21 extension_type, extension_name = __name__.split('.')[-2:] 21 extension_type, extension_name = __name__.split('.')[-2:]
22 22
23 def _check_object_value(param, value): 23 def _check_object_value(param, value):
24 """ 24 """ helps to omit useless lines of object values
25 helps omit useless lines of object values
26 25
27 @param param: definition of object param 26 @param param: definition of object param
28 @param value: value of param 27 @param value: value of param
29 """ 28 """
30 if value: 29 if value:
31 return '%(param)s="%(value)s"' % {"param": param, "value": wikiutil.escape(value)} 30 return '%(param)s="%(value)s"' % {"param": param, "value": wikiutil.escape(value, True)}
32 else: 31 else:
33 return "" 32 return ""
34 33
35 def _check_param_value(param, value, valuetype): 34 def _check_param_value(param, value, valuetype):
36 """ helps to ommit useless lines of param values 35 """ helps to ommit useless lines of param values
36
37 @param param: param name defintion 37 @param param: param name defintion
38 @param value: the value 38 @param value: the value
39 @param valuetype: the type of the value 39 @param valuetype: the type of the value
40 """ 40 """
41 if value: 41 if value:
42 return ''' 42 return '''
43 <param name="%(param)s" value="%(value)s" valuetype="%(valuetype)s">''' % {"param": param, 43 <param name="%(param)s" value="%(value)s" valuetype="%(valuetype)s">''' % {"param": param,
44 "value": wikiutil.escape(value), 44 "value": wikiutil.escape(value, True),
45 "valuetype": valuetype} 45 "valuetype": valuetype}
46 else: 46 else:
47 return "" 47 return ""
48 48
49 def macro_EmbedObject(macro, target=None, pagename=None, width=wikiutil.UnitArgument(None, float, ['px', 'em', 'mm', '%']), 49 def macro_EmbedObject(macro, target=None, pagename=None,
50 height=wikiutil.UnitArgument(None, float, ['px', 'em', 'mm', '%']), alt=u'', 50 width=wikiutil.UnitArgument(None, float, ['px', 'em', 'mm', '%']),
51 height=wikiutil.UnitArgument(None, float, ['px', 'em', 'mm', '%']),
52 alt=u'',
51 play=False, stop=True, loop=False, quality=(u'high', u'low', u'medium'), 53 play=False, stop=True, loop=False, quality=(u'high', u'low', u'medium'),
52 op=True, repeat=False, autostart=False, align=(u'middle', u'top', u'bottom'), hidden=False, 54 op=True, repeat=False, autostart=False, align=(u'middle', u'top', u'bottom'), hidden=False,
53 menu=True, wmode=u'transparent', url_mimetype=None): 55 menu=True, wmode=u'transparent', url_mimetype=None):
54
55 """ This macro is used to embed an object into a wiki page """ 56 """ This macro is used to embed an object into a wiki page """
56 # Join unit arguments with their units 57 # Join unit arguments with their units
57 if width: 58 if width:
58 if width[1] == 'px': 59 if width[1] == 'px':
59 width = '%dpx' % int(width[0]) 60 width = '%dpx' % int(width[0])
73 # AttachFile calls always with pagename. Users can call the macro from a different page as the attachment is saved. 74 # AttachFile calls always with pagename. Users can call the macro from a different page as the attachment is saved.
74 if not pagename: 75 if not pagename:
75 pagename = fmt.page.page_name 76 pagename = fmt.page.page_name
76 77
77 if not target: 78 if not target:
78 msg = _('Not enough arguments given to EmbedObject macro! Try <<EmbedObject(attachment [,width=width] [,height=height] [,alt=alternate Text])>>') 79 return fmt.text(_('%(extension_name)s %(extension_type)s: Required argument %(argument_name)s missing.') % {
79 return "%s%s%s" % (fmt.sysmsg(1), fmt.text(msg), fmt.sysmsg(0)) 80 "extension_name": extension_name,
81 "extension_type": extension_type,
82 "argument_name": "target",
83 })
80 84
81 if not wikiutil.is_URL(target): 85 if not wikiutil.is_URL(target):
82 pagename, fname = AttachFile.absoluteName(target, pagename) 86 pagename, fname = AttachFile.absoluteName(target, pagename)
83 87
84 if not AttachFile.exists(request, pagename, fname): 88 if not AttachFile.exists(request, pagename, fname):
90 94
91 url = AttachFile.getAttachUrl(pagename, fname, request) 95 url = AttachFile.getAttachUrl(pagename, fname, request)
92 mt = wikiutil.MimeType(filename=fname) 96 mt = wikiutil.MimeType(filename=fname)
93 else: 97 else:
94 if not url_mimetype: 98 if not url_mimetype:
95 return _('Not enough arguments given to %(extension_name)s %(extension_type)s! Try <<EmbedObject(url, url_mimetype [,width=width] [,height=height] [,alt=alternate Text])>>') % { 99 return fmt.text(_('%(extension_name)s %(extension_type)s: Required argument %(argument_name)s missing.') % {
96 "extension_name": extension_name, 100 "extension_name": extension_name,
97 "extension_type": extension_type, 101 "extension_type": extension_type,
98 } 102 "argument_name": "url_mimetype",
103 })
99 else: 104 else:
105 url = target
100 mt = wikiutil.MimeType() # initialize dict 106 mt = wikiutil.MimeType() # initialize dict
101 mt.major, mt.minor = url_mimetype.split('/') 107 try:
102 url = target 108 mt.major, mt.minor = url_mimetype.split('/')
103 109 except ValueError:
104 # XXX Should better use formatter.embed if available? 110 return fmt.text(_('%(extension_name)s %(extension_type)s: Invalid %(argument_name)s=%(argument_value)s!') % {
105 if not mt: 111 "extension_name": extension_name,
106 return _("Unknown mimetype %(mimetype)s of the file %(file)s.") % {"mimetype": url_mimetype, 112 "extension_type": extension_type,
107 "file": target} 113 "argument_name": "url_mimetype",
108 114 "argument_value": str(url_mimetype),
115 })
116
109 mime_type = "%s/%s" % (mt.major, mt.minor, ) 117 mime_type = "%s/%s" % (mt.major, mt.minor, )
110 dangerous = mime_type in request.cfg.mimetypes_xss_protect 118 dangerous = mime_type in request.cfg.mimetypes_xss_protect
111 119
112 if not mime_type in request.cfg.mimetypes_embed or dangerous: 120 if not mime_type in request.cfg.mimetypes_embed or dangerous:
113 kw = {'src': url}
114 return "%s: %s%s%s" % (fmt.text( 121 return "%s: %s%s%s" % (fmt.text(
115 _("Current configuration doesn't allow mimetype %(mimetype)s of the file %(file)s.") % {"mimetype": mime_type, 122 _("Current configuration does not allow embedding of the file %(file)s because of its mimetype %(mimetype)s.") % {
116 "file": target}), 123 "mimetype": mime_type,
117 fmt.url(1, kw['src']), 124 "file": target}),
118 fmt.text(target), 125 fmt.url(1, url),
119 fmt.url(0)) 126 fmt.text(target),
127 fmt.url(0))
120 128
121 if not alt: 129 if not alt:
122 alt = "%(text)s %(mime_type)s" % { 130 alt = "%(text)s %(mime_type)s" % {'text': _("Embedded"), 'mime_type': mime_type}
123 'text': _("Embedded"), 131
124 'mime_type': mime_type,
125 }
126 embed_src = '' 132 embed_src = ''
127 if mt.major == 'video': 133 if mt.major == 'video':
128 if not width and not height: 134 if not width and not height:
129 width = '400px' 135 width = '400px'
130 height = '400px' 136 height = '400px'
212 "loop": _check_param_value("loop", loop, "data"), 218 "loop": _check_param_value("loop", loop, "data"),
213 "menu": _check_param_value("menu", menu, "data"), 219 "menu": _check_param_value("menu", menu, "data"),
214 "alt": wikiutil.escape(alt), 220 "alt": wikiutil.escape(alt),
215 } 221 }
216 222
217 return embed_src 223 return fmt.rawHTML(embed_src)
218