Back to index

moin  1.9.0~rc2
EmbedObject.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - EmbedObject Macro
00004 
00005     This macro is used to embed an object into a wiki page. Optionally, the
00006     size of the object can get adjusted. Further keywords are dependent on
00007     the kind of application, see HelpOnMacros/EmbedObject
00008 
00009     <<EmbedObject(attachment[,width=width][,height=height][,alt=alternate Text])>>
00010 
00011     @copyright: 2006-2008 MoinMoin:ReimarBauer,
00012                 2006 TomSi,
00013                 2007 OliverSiemoneit
00014 
00015     @license: GNU GPL, see COPYING for details.
00016 """
00017 
00018 from MoinMoin import wikiutil
00019 from MoinMoin.action import AttachFile
00020 
00021 extension_type, extension_name = __name__.split('.')[-2:]
00022 
00023 def _check_object_value(param, value):
00024     """ helps to omit useless lines of object values
00025 
00026     @param param: definition of object param
00027     @param value: value of param
00028     """
00029     if value:
00030         return '%(param)s="%(value)s"' % {"param": param, "value": wikiutil.escape(value, True)}
00031     else:
00032         return ""
00033 
00034 def _check_param_value(param, value, valuetype):
00035     """ helps to ommit useless lines of param values
00036 
00037     @param param: param name defintion
00038     @param value: the value
00039     @param valuetype: the type of the value
00040     """
00041     # Because plugins do have different defaults we have to write "False" too.
00042     if isinstance(value, bool):
00043         value = str(value)
00044 
00045     if value:
00046         return '''
00047 <param name="%(param)s" value="%(value)s" valuetype="%(valuetype)s">''' % {"param": param,
00048                                                                            "value": wikiutil.escape(value, True),
00049                                                                            "valuetype": valuetype}
00050     else:
00051         return ""
00052 
00053 def macro_EmbedObject(macro, target=wikiutil.required_arg(unicode), pagename=None,
00054                       width=wikiutil.UnitArgument(None, float, ['px', 'em', 'pt', 'in', 'mm', '%'], defaultunit='px'),
00055                       height=wikiutil.UnitArgument(None, float, ['px', 'em', 'pt', 'in', 'mm', '%'], defaultunit='px'),
00056                       alt=u'',
00057                       play=False, stop=True, loop=False, quality=(u'high', u'low', u'medium'),
00058                       op=True, repeat=False, autostart=False, align=(u'middle', u'top', u'bottom'), hidden=False,
00059                       menu=True, wmode=u'transparent', url_mimetype=None):
00060     """ This macro is used to embed an object into a wiki page """
00061     # Join unit arguments with their units
00062     if width:
00063         if width[1] == 'px':
00064             width = '%dpx' % int(width[0])
00065         else:
00066             width = '%g%s' % width
00067 
00068     if height:
00069         if height[1] == 'px':
00070             height = '%dpx' % int(height[0])
00071         else:
00072             height = '%g%s' % height
00073 
00074     request = macro.request
00075     _ = macro.request.getText
00076     fmt = macro.formatter
00077 
00078     # AttachFile calls always with pagename. Users can call the macro from a different page as the attachment is saved.
00079     if not pagename:
00080         pagename = fmt.page.page_name
00081 
00082     if not wikiutil.is_URL(target):
00083         pagename, fname = AttachFile.absoluteName(target, pagename)
00084 
00085         if not AttachFile.exists(request, pagename, fname):
00086             linktext = _('Upload new attachment "%(filename)s"') % {'filename': fname}
00087             target = AttachFile.getAttachUrl(pagename, fname, request, do='upload_form')
00088             return (fmt.url(1, target) +
00089                     fmt.text(linktext) +
00090                     fmt.url(0))
00091 
00092         url = AttachFile.getAttachUrl(pagename, fname, request)
00093         mt = wikiutil.MimeType(filename=fname)
00094     else:
00095         if not url_mimetype:
00096             return fmt.text(_('%(extension_name)s %(extension_type)s: Required argument %(argument_name)s missing.') % {
00097                 "extension_name": extension_name,
00098                 "extension_type": extension_type,
00099                 "argument_name": "url_mimetype",
00100             })
00101         else:
00102             url = target
00103             mt = wikiutil.MimeType() # initialize dict
00104             try:
00105                 mt.major, mt.minor = url_mimetype.split('/')
00106             except ValueError:
00107                 return fmt.text(_('%(extension_name)s %(extension_type)s: Invalid %(argument_name)s=%(argument_value)s!') % {
00108                    "extension_name": extension_name,
00109                    "extension_type": extension_type,
00110                    "argument_name": "url_mimetype",
00111                    "argument_value": str(url_mimetype),
00112                 })
00113 
00114     mime_type = "%s/%s" % (mt.major, mt.minor, )
00115     dangerous = mime_type in request.cfg.mimetypes_xss_protect
00116 
00117     if not mime_type in request.cfg.mimetypes_embed or dangerous:
00118         return "%s: %s%s%s" % (fmt.text(
00119                 _("Current configuration does not allow embedding of the file %(file)s because of its mimetype %(mimetype)s.") % {
00120                     "mimetype": mime_type,
00121                     "file": target}),
00122                 fmt.url(1, url),
00123                 fmt.text(target),
00124                 fmt.url(0))
00125 
00126     if not alt:
00127         alt = "%(text)s %(mime_type)s" % {'text': _("Embedded"), 'mime_type': mime_type}
00128 
00129     embed_src = ''
00130     if mt.major == 'video':
00131         if not width and not height:
00132             width = '400px'
00133             height = '400px'
00134 
00135         embed_src = '''
00136 <object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s %(ob_standby)s %(ob_stop)s>
00137 %(wmode)s%(movie)s%(play)s%(stop)s%(repeat)s%(autostart)s%(op)s%(menu)s
00138 <p>%(alt)s</p>
00139 </object>''' % {
00140     "ob_data": _check_object_value("data", url),
00141     "ob_type": _check_object_value("type", mime_type),
00142     "ob_width": _check_object_value("width", width),
00143     "ob_height": _check_object_value("height", height),
00144     "ob_align": _check_object_value("align", align),
00145     "ob_standby": _check_object_value("standby", alt),
00146     "ob_stop": _check_object_value("stop", stop),
00147     "wmode": _check_param_value("wmode", wmode, "data"),
00148     "movie": _check_param_value("movie", url, "data"),
00149     "play": _check_param_value("play", play, "data"),
00150     "stop": _check_param_value("stop", stop, "data"),
00151     "repeat": _check_param_value("repeat", repeat, "data"),
00152     "autostart": _check_param_value("autostart", autostart, "data"),
00153     "op": _check_param_value("op", op, "data"),
00154     "menu": _check_param_value("menu", menu, "data"),
00155     "alt": wikiutil.escape(alt),
00156 }
00157 
00158     elif mt.major in ['image', 'chemical', 'x-world']:
00159         embed_src = '''
00160 <object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
00161 %(name)s
00162 <p>%(alt)s</p>
00163 </object>''' % {
00164     "mime_type": mime_type,
00165     "ob_data": _check_object_value("data", url),
00166     "ob_width": _check_object_value("width", width),
00167     "ob_height": _check_object_value("height", height),
00168     "ob_type": _check_object_value("type", mime_type),
00169     "ob_align": _check_object_value("align", align),
00170     "name": _check_param_value("name", url, "data"),
00171     "alt": wikiutil.escape(alt),
00172 }
00173 
00174     elif mt.major == 'audio':
00175         if not width and not height:
00176             width = '400px'
00177             height = '100px'
00178         embed_src = '''
00179 <object %(ob_data)s %(ob_type)s  %(ob_width)s %(ob_height)s %(ob_align)s>
00180 %(audio)s%(repeat)s%(autostart)s%(op)s%(play)s%(stop)s%(hidden)s<p>%(alt)s</p>
00181 </object>''' % {
00182     "ob_data": _check_object_value("data", url),
00183     "ob_width": _check_object_value("width", width or "60"),
00184     "ob_height": _check_object_value("height", height or "20"),
00185     "ob_type": _check_object_value("type", mime_type),
00186     "ob_align": _check_object_value("align", align),
00187     "audio": _check_param_value("audio", url, "data"),
00188     "repeat": _check_param_value("repeat", repeat, "data"),
00189     "autostart": _check_param_value("autostart", autostart, "data"),
00190     "op": _check_param_value("op", op, "data"),
00191     "play": _check_param_value("play", play, "data"),
00192     "stop": _check_param_value("stop", stop, "data"),
00193     "hidden": _check_param_value("hidden", hidden, "data"),
00194     "alt": wikiutil.escape(alt),
00195 }
00196 
00197     elif mt.major == 'application':
00198         # workaround for the acroread browser plugin not knowing the size to embed
00199         # we use a width of 100% for the case that there is no width given.
00200         # A height of 100% gives a fullscreen pdf file view without embedding it into the wikicontent.
00201         if mt.minor == 'pdf':
00202             width = width or '100%'
00203             height = height or '800px'
00204             embed_src = '''
00205 <object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s>
00206 <p>%(alt)s</p>
00207 </object>''' % {
00208     "ob_data": _check_object_value("data", url),
00209     "ob_width": _check_object_value("width", width),
00210     "ob_height": _check_object_value("height", height),
00211     "ob_type": _check_object_value("type", mime_type),
00212     "ob_align": _check_object_value("align", align),
00213     "alt": wikiutil.escape(alt),
00214 }
00215         else:
00216             embed_src = '''
00217 <object %(ob_data)s %(ob_type)s %(ob_width)s %(ob_height)s %(ob_align)s>
00218 %(movie)s%(quality)s%(wmode)s%(autostart)s%(play)s%(loop)s%(menu)s<p>%(alt)s</p>
00219 </object>''' % {
00220     "ob_data": _check_object_value("data", url),
00221     "ob_width": _check_object_value("width", width),
00222     "ob_height": _check_object_value("height", height),
00223     "ob_type": _check_object_value("type", mime_type),
00224     "ob_align": _check_object_value("align", align),
00225     "movie": _check_param_value("movie", url, "data"),
00226     "quality": _check_param_value("quality", quality, "data"),
00227     "wmode": _check_param_value("wmode", wmode, "data"),
00228     "autostart": _check_param_value("autostart", autostart, "data"),
00229     "play": _check_param_value("play", play, "data"),
00230     "loop": _check_param_value("loop", loop, "data"),
00231     "menu": _check_param_value("menu", menu, "data"),
00232     "alt": wikiutil.escape(alt),
00233 }
00234 
00235     return fmt.rawHTML(embed_src)