Attachment 'EmbedObject.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - EmbedObject Macro
   4 
   5     PURPOSE:
   6         This macro is used to embed an object into a wiki page. Optionally, the
   7         size of the object can get adjusted. Further keywords are dependent on
   8         the kind of application.
   9 
  10     CALLING SEQUENCE:
  11         [[EmbedObject(attachment[,width=width][,height=height][,alt=Embedded mimetpye/xy])]]
  12 
  13     SUPPORTED MIMETYPES:  
  14          application/x-shockwave-flash
  15          application/x-dvi
  16          application/postscript
  17          application/pdf
  18          application/ogg
  19          application/vnd.visio
  20          
  21          image/x-ms-bmp
  22          image/svg+xml
  23          image/tiff
  24          image/x-photoshop
  25 
  26          audio/mpeg
  27          audio/midi
  28          audio/x-wav
  29                          
  30          video/fli
  31          video/mpeg
  32          video/quicktime
  33          video/x-msvideo
  34                          
  35          chemical/x-pdb
  36 
  37          x-world/x-vrml  
  38            
  39     INPUTS:
  40         attachment: name of attachment
  41 
  42     KEYWORD PARAMETERS:
  43         
  44         Dependent on the mimetype class a different set of keywords is used from the defaults
  45 
  46            width = ""
  47            height = ""
  48            alt = "Embedded mimetpye/xy"
  49            type = mime_type
  50            play = false
  51            loop = false
  52            quality = high
  53            op = true
  54            repeat = false
  55            autostart = false
  56            menu = true
  57       
  58 
  59         All do use width, height, mime_type, alt   
  60         
  61         in addition:
  62            'video' do use  repeat, autostart, menu, op
  63            'audio' do use   play, repeat, autostart, op, hidden
  64                    the default width is 60 and default height is 20
  65            'application' do use play, menu, autostart
  66 
  67         Note: Please do provide always a sensible alt text for the embedded object which
  68         gives a short description of the visually or acoustically presented content so
  69         that visually and acoustically impaired people can at least get a clue of what's
  70         going on in this "black box". By default alt is set to "Embedded mimetpye/xy" for
  71         people that forget to set an alt. However this default alt text is not a sensible
  72         one since it does not describe the content really but only the type of content.
  73         Compare these alt texts: "Embedded application/pdf" vs. "MoinMoin Tutorial embedded
  74         as PDF file"
  75     
  76     EXAMPLE:
  77         [[EmbedObject]]
  78         [[EmbedObject(example.swf,alt=A flash movie showing the rotating moin logo)]]
  79         [[EmbedObject(example.mid,alt=Background sound of wikipage: oceanwaves)]]
  80         [[EmbedObject(example.pdf)]]
  81         [[EmbedObject(example.svg)]]
  82         [[EmbedObject(example.mp3)]]
  83         [[EmbedObject(example.vss)]]
  84          
  85         [[EmbedObject(example.swf,width=637,height=392)]]
  86         [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
  87         [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
  88         [[EmbedObject(SlideShow/example.swf,width=637,height=392,play=true,loop=false)]]
  89         [[EmbedObject(SlideShow/example.swf,width=637,height=392,quality=low)]]
  90 
  91  
  92     PROCEDURE:
  93         If the attachment file isn't uploaded yet the attachment line will be shown.
  94         If you give only one size argument, e.g. width only, the other one will be calculated.
  95 
  96         By the swftools it is possible to get the swf size returned. I don't know if it is 
  97         possible to get sizes for svg, pdf and others detected too, that's the reason why 
  98         I haven't added it by now.
  99 
 100         Please add needed mimetypes as objects.
 101 
 102            
 103     RESTRICTIONS:
 104         Some mimetypes do ignore all used keywords. May be they do use different names.        
 105 
 106 
 107     MODIFICATION HISTORY:
 108         @copyright: 2006 by Reimar Bauer (R.Bauer@fz-juelich.de)      
 109         @license: GNU GPL, see COPYING for details.
 110         initial version: 1.5.0-1
 111         svg was added by AndrewArmstrong
 112         2006-05-04 TomSi: added mp3 support
 113         2006-05-09 RB code refactored, fixed a taintfilename bug
 114         2006-06-29 visio from OwenJones added but not tested,
 115                    RB code reviewed, taintfile removed
 116         2006-10-01 RB code refactored
 117         2006-10-05 RB bug fixed closing " at height added
 118         2006-10-08 RB type is needed on some platforms, some more keywords added
 119         2007-02-10 OliverSiemoneit: alt and noembed tags added for AccessibleMoin; fixed
 120                    output abstraction violation.
 121 """
 122 import os, mimetypes
 123 
 124 from MoinMoin import wikiutil
 125 from MoinMoin.action import AttachFile
 126 
 127 class EmbedObject:
 128 
 129     def __init__(self, macro, args):
 130         self._ = macro.request.getText
 131         self.macro = macro
 132         self.request = macro.request
 133         self.formatter = macro.formatter
 134         self.args = args
 135 
 136         self.width = ""
 137         self.height = ""
 138         self.alt = ""
 139         self.play = "false"
 140         self.loop = "false"
 141         self.quality = "high"
 142         self.op = "true"
 143         self.repeat = "false"
 144         self.autostart = "false"
 145         self.align = "center"
 146         self.hidden = "false"
 147         self.menu = "true"
 148         
 149         if args:
 150             args = args.split(',')
 151             args = [arg.strip() for arg in args]
 152         else:
 153             args = []
 154 
 155         kw_count = 0
 156         argc = len(args)
 157         for arg in self.args.split(','):
 158             if arg.find('=') > -1:
 159                 kw_count += 1
 160                 key, value = arg.split('=')
 161                 setattr(self, key, wikiutil.escape(value.strip(), quote=1))
 162 
 163         argc -= kw_count
 164 
 165         if not argc:
 166            msg = 'Not enough arguments to EmbedObject macro! Try [[EmbedObject(attachment [,width=width] [,height=height] [,alt=Embedded mimetpye/xy])]]'
 167            return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 168         else:
 169             self.target = args[0]
 170 
 171     def embed(self, mime_type, file):
 172         _ = self._
 173         mtype = mime_type.split('/')
 174 
 175         if self.alt == "":
 176             self.alt = "%(text)s %(mime_type)s" % { 'text': _("Embedded"), 'mime_type': mime_type, }
 177 
 178         if mtype[0] == 'video':
 179             return '''
 180 <OBJECT>
 181 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" REPEAT="%(repeat)s" AUTOSTART="%(autostart)s" OP="%(op)s" MENU="%(menu)s" TYPE="%(type)s"></EMBED>
 182 <NOEMBED>
 183 <p>%(alt)s</p>
 184 </NOEMBED>
 185 </OBJECT>''' % {
 186     "width": self.width,
 187     "height": self.height,
 188     "file": file,
 189     "repeat": self.repeat,
 190     "autostart": self.autostart,
 191     "op": self.op,
 192     "type": mime_type,
 193     "menu": self.menu,
 194     "alt": self.alt,
 195 }
 196 
 197         if mtype[0] in ['image', 'chemical', 'x-world']:
 198             return '''
 199 <OBJECT>
 200 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" TYPE="%(type)s"></EMBED>
 201 <NOEMBED>
 202 <p>%(alt)s</p>
 203 </NOEMBED>
 204 </OBJECT>''' % {
 205     "width": self.width,
 206     "height": self.height,
 207     "file": file,
 208     "type": mime_type,
 209     "alt": self.alt,
 210 }
 211 
 212         if mtype[0] == 'audio':
 213             if self.width == "":
 214                 self.width = "60"
 215             if self.height == "":
 216                 self.height = "20"
 217             return '''
 218 <OBJECT>
 219 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" REPEAT="%(repeat)s" AUTOSTART="%(autostart)s" OP="%(op)s" PLAY="%(play)s" HIDDEN="%(hidden)s" TYPE="%(type)s"></EMBED>
 220 <NOEMBED>
 221 <p>%(alt)s</p>
 222 </NOEMBED>
 223 </OBJECT>''' % {
 224     "width": self.width,
 225     "height": self.height,
 226     "file": file,
 227     "play": self.play,
 228     "repeat": self.repeat,
 229     "autostart": self.autostart,
 230     "op": self.op,
 231     "hidden": self.hidden,
 232     "type": mime_type,
 233     "alt": self.alt,
 234 }
 235 
 236         if mtype[0] == 'application':
 237             return '''
 238 <OBJECT>
 239 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" AUTOSTART="%(autostart)s" PLAY="%(play)s" LOOP="%(loop)s" MENU="%(menu)s" TYPE="%(type)s"> </EMBED>
 240 <NOEMBED>
 241 <p>%(alt)s</p>
 242 </NOEMBED>
 243 </OBJECT>''' % {
 244     "width": self.width,
 245     "height": self.height,
 246     "file": file,
 247     "autostart": self.autostart,
 248     "play": self.play,
 249     "loop": self.loop,
 250     "type": mime_type,
 251     "menu": self.menu,
 252     "alt": self.alt,
 253 }
 254 
 255     def render(self):
 256         _ = self._
 257 
 258         pagename, attname = AttachFile.absoluteName(self.target, self.formatter.page.page_name)
 259         attachment_fname = AttachFile.getFilename(self.request, pagename, attname)
 260 
 261         if not os.path.exists(attachment_fname):
 262             linktext = _('Upload new attachment "%(filename)s"')
 263             return wikiutil.link_tag(self.request,
 264                 ('%s?action=AttachFile&rename=%s' % (
 265                 wikiutil.quoteWikinameURL(pagename),
 266                 wikiutil.url_quote_plus(attname))),
 267                 linktext % {'filename': attname})
 268 
 269         url = AttachFile.getAttachUrl(pagename, attname, self.request)
 270         mime_type, enc = mimetypes.guess_type(attname)
 271 
 272         if mime_type in ["application/x-shockwave-flash",
 273                          "application/x-dvi",
 274                          "application/postscript",
 275                          "application/pdf",
 276                          "application/ogg",
 277                          "application/vnd.visio",
 278 
 279                          "image/x-ms-bmp",
 280                          "image/svg+xml",
 281                          "image/tiff",
 282                          "image/x-photoshop",
 283 
 284                          "audio/mpeg",
 285                          "audio/midi",
 286                          "audio/x-wav",
 287 
 288                          "video/fli",
 289                          "video/mpeg",
 290                          "video/quicktime",
 291                          "video/x-msvideo",
 292 
 293                          "chemical/x-pdb",
 294 
 295                          "x-world/x-vrml",
 296                        ]:
 297             # XXX Should better use formatter.embed if available?
 298             try:
 299                 return self.macro.formatter.rawHTML(self.embed(mime_type, url))
 300             except:
 301                 return "%s%s%s" % (self.macro.formatter.sysmsg(1),
 302                                    self.macro.formatter.text('Embedding of object by choosen formatter not possible'),
 303                                    self.macro.formatter.sysmsg(0))  
 304 
 305         else:
 306             msg = 'Not supported mimetype %(mimetype)s ' % {"mimetype": mime_type}
 307             return "%s%s%s" % (self.macro.formatter.sysmsg(1),
 308                        self.macro.formatter.text(msg),
 309                        self.macro.formatter.sysmsg(0))
 310 
 311 
 312 def execute(macro, args):
 313     return EmbedObject(macro, args).render()

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.
  • [get | view] (2007-02-12 18:15:39, 10.3 KB) [[attachment:EmbedObject.py]]
  • [get | view] (2007-02-12 18:15:57, 7.6 KB) [[attachment:embedobject.diff]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.