Attachment 'EmbedObject_patch_20061001.txt'

Download

   1 # HG changeset patch
   2 # User ReimarBauer <R.Bauer@fz-juelich.de>
   3 # Node ID a3d0184c9eae1f3d900746baf05bcc0c980fbb98
   4 # Parent  53bd1632c92a121c0104813679b526b27bdfd1d3
   5 refactored into a class EmbedObject
   6 
   7 diff -r 53bd1632c92a -r a3d0184c9eae MoinMoin/macro/EmbedObject.py
   8 --- a/MoinMoin/macro/EmbedObject.py	Tue Sep 26 23:43:25 2006 +0200
   9 +++ b/MoinMoin/macro/EmbedObject.py	Sun Oct 01 20:10:20 2006 +0200
  10 @@ -10,25 +10,57 @@
  11      CALLING SEQUENCE:
  12          [[EmbedObject(attachment[,width=width][,height=height])]]
  13  
  14 -    SUPPORTED MIMETYPES:
  15 -         application/x-shockwave-flash 
  16 +    SUPPORTED MIMETYPES:  
  17 +         application/x-shockwave-flash
  18 +         application/x-dvi
  19 +         application/postscript
  20 +         application/pdf
  21 +         application/ogg
  22 +         application/vnd.visio
  23 +         
  24 +         image/x-ms-bmp
  25           image/svg+xml
  26 -         application/pdf
  27 +         image/tiff
  28 +         image/x-photoshop
  29 +
  30           audio/mpeg
  31 -         application/vnd.visio
  32 -
  33 +         audio/midi
  34 +         audio/x-wav
  35 +                         
  36 +         video/fli
  37 +         video/mpeg
  38 +         video/quicktime
  39 +         video/x-msvideo
  40 +                         
  41 +         chemical/x-pdb
  42 +
  43 +         x-world/x-vrml  
  44 +           
  45      INPUTS:
  46          attachment: name of attachment
  47  
  48      KEYWORD PARAMETERS:
  49 -        width: width of the embedded object, default is 100% of window
  50 -        height: height of the embedded object, default is 100% of window
  51 -
  52 -        application/x-shockwave-flash:
  53 -          play: true is default
  54 -          loop: true is default
  55 -          quality: high is default (medium,low)
  56 -
  57 +        
  58 +        Dependent on the mimetype class a different set of keywords is used from the defaults
  59 +
  60 +           width = ""
  61 +           height = ""
  62 +           play = false
  63 +           loop = false
  64 +           quality = high
  65 +           op = true
  66 +           repeat = false
  67 +           autostart = false
  68 +      
  69 +
  70 +        All do use width, height   
  71 +        
  72 +        in addition:
  73 +           'video' do use  repeat, autostart, op
  74 +           'audio' do use   play repeat, autostart, op
  75 +           'application' do use  play, autostart
  76 +        
  77 +    
  78      EXAMPLE:
  79          [[EmbedObject]]
  80          [[EmbedObject(example.swf)]]
  81 @@ -39,9 +71,9 @@
  82           
  83          [[EmbedObject(example.swf,width=637,height=392)]]
  84          [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
  85 -        [[EmbedObject(SlideShow/example.swf,width=637,height=392,play=false)]]
  86 -        [[EmbedObject(SlideShow/example.swf,width=637,height=392,play=false,loop=false)]]
  87 -        [[EmbedObject(SlideShow/example.swf,width=637,height=392,play=false,loop=low)]]
  88 +        [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
  89 +        [[EmbedObject(SlideShow/example.swf,width=637,height=392,play=true,loop=false)]]
  90 +        [[EmbedObject(SlideShow/example.swf,width=637,height=392,quality=low)]]
  91  
  92   
  93      PROCEDURE:
  94 @@ -53,6 +85,9 @@
  95          I haven't added it by now.
  96  
  97          Please add needed mimetypes as objects.
  98 +    
  99 +    RESTRICTIONS:
 100 +        some mimetypes do ignore all used keywords. May be they do use different names.        
 101  
 102  
 103      MODIFICATION HISTORY:
 104 @@ -64,175 +99,158 @@
 105          2006-05-09 RB code refactored, fixed a taintfilename bug
 106          2006-06-29 visio from OwenJones added but not tested,
 107                     RB code reviewed, taintfile removed
 108 +        2006-10-01 RB code refactored
 109  """
 110  import os, mimetypes
 111  
 112  from MoinMoin import wikiutil
 113  from MoinMoin.action import AttachFile
 114  
 115 +class EmbedObject:
 116 +
 117 +    def __init__(self, macro, args):
 118 +        self.macro = macro
 119 +        self.request = macro.request
 120 +        self.formatter = macro.formatter
 121 +        self.args = args
 122 +
 123 +        self.width = ""
 124 +        self.height = ""
 125 +        self.play = "false"
 126 +        self.loop = "false"
 127 +        self.quality = "high"
 128 +        self.op = "true"
 129 +        self.repeat = "false"
 130 +        self.autostart = "false"
 131 +        self.align = "center"
 132 +
 133 +        if args:
 134 +            args = args.split(',')
 135 +            args = [arg.strip() for arg in args]
 136 +        else:
 137 +            args = []
 138 +
 139 +        kw_count = 0
 140 +        argc = len(args)
 141 +        for arg in self.args.split(','):
 142 +            if arg.find('=') > -1:
 143 +                kw_count += 1
 144 +                key, value = arg.split('=')
 145 +                setattr(self, key, wikiutil.escape(value.strip(), quote=1))
 146 +
 147 +        argc -= kw_count
 148 +
 149 +        if not argc:
 150 +           msg = 'Not enough arguments to EmbedObject macro! Try [[EmbedObject(attachment [,width=width] [,height=heigt])]]'
 151 +           return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 152 +        else:
 153 +            self.target = args[0]
 154 +
 155 +    def embed(self, mime_type, file):
 156 +        mtype = mime_type.split('/')
 157 +
 158 +        if mtype[0] == 'video':
 159 +            return '''
 160 +<OBJECT>
 161 +<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s REPEAT="%(repeat)s" AUTOSTART="%(autostart)s" OP="%(op)s"></EMBED>
 162 +</OBJECT>''' % {
 163 +    "width": self.width,
 164 +    "height": self.height,
 165 +    "file": file,
 166 +    "repeat": self.repeat,
 167 +    "autostart": self.autostart,
 168 +    "op": self.op,
 169 +}
 170 +
 171 +        if mtype[0] in ['image', 'chemical', 'x-world']:
 172 +            return '''
 173 +<OBJECT>
 174 +<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"></EMBED>
 175 +</OBJECT>''' % {
 176 +    "width": self.width,
 177 +    "height": self.height,
 178 +    "file": file,
 179 +}
 180 +
 181 +        if mtype[0] == 'audio':
 182 +            return '''
 183 +<OBJECT>
 184 +<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s REPEAT="%(repeat)s" AUTOSTART="%(autostart)s" OP="%(op)s" PLAY="%(play)s"></EMBED>
 185 +</OBJECT>''' % {
 186 +   "width": self.width,
 187 +   "height": self.height,
 188 +   "file": file,
 189 +   "play": self.play,
 190 +   "repeat": self.repeat,
 191 +   "autostart": self.autostart,
 192 +   "op": self.op,
 193 +}
 194 +
 195 +        if mtype[0] == 'application':
 196 +            return '''
 197 +<OBJECT>
 198 +<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" AUTOSTART="%(autostart)s" PLAY="%(play)s" LOOP="%(loop)s"  </EMBED>
 199 +</OBJECT>''' % {
 200 +    "width": self.width,
 201 +    "height": self.height,
 202 +    "file": file,
 203 +    "autostart": self.autostart,
 204 +    "play": self.play,
 205 +    "loop": self.loop,
 206 +}
 207 +
 208 +    def render(self):
 209 +        _ = self.request.getText
 210 +
 211 +        pagename, attname = AttachFile.absoluteName(self.target, self.formatter.page.page_name)
 212 +        attachment_fname = AttachFile.getFilename(self.request, pagename, attname)
 213 +
 214 +        if not os.path.exists(attachment_fname):
 215 +            linktext = _('Upload new attachment "%(filename)s"')
 216 +            return wikiutil.link_tag(self.request,
 217 +                ('%s?action=AttachFile&rename=%s' % (
 218 +                wikiutil.quoteWikinameURL(pagename),
 219 +                wikiutil.url_quote_plus(attname))),
 220 +                linktext % {'filename': attname})
 221 +
 222 +        url = AttachFile.getAttachUrl(pagename, attname, self.request)
 223 +        mime_type, enc = mimetypes.guess_type(attname)
 224 +
 225 +        if mime_type in ["application/x-shockwave-flash",
 226 +                         "application/x-dvi",
 227 +                         "application/postscript",
 228 +                         "application/pdf",
 229 +                         "application/ogg",
 230 +                         "application/vnd.visio",
 231 +
 232 +                         "image/x-ms-bmp",
 233 +                         "image/svg+xml",
 234 +                         "image/tiff",
 235 +                         "image/x-photoshop",
 236 +
 237 +                         "audio/mpeg",
 238 +                         "audio/midi",
 239 +                         "audio/x-wav",
 240 +
 241 +                         "video/fli",
 242 +                         "video/mpeg",
 243 +                         "video/quicktime",
 244 +                         "video/x-msvideo",
 245 +
 246 +                         "chemical/x-pdb",
 247 +
 248 +                         "x-world/x-vrml",
 249 +                       ]:
 250 +
 251 +            return self.embed(mime_type, url)
 252 +
 253 +        else:
 254 +            msg = 'Not supported mimetype %(mimetype)s ' % {"mimetype": mime_type}
 255 +            return "%s%s%s" % (self.macro.formatter.sysmsg(1),
 256 +                       self.macro.formatter.text(msg),
 257 +                       self.macro.formatter.sysmsg(0))
 258 +
 259 +
 260  def execute(macro, args):
 261 -    request = macro.request
 262 -    _ = request.getText
 263 -    formatter = macro.formatter
 264 -    if args:
 265 -        args = args.split(',')
 266 -        args = [arg.strip() for arg in args]
 267 -    else:
 268 -        args = []
 269 -
 270 -    argc = len(args)
 271 -    kw_count = 0
 272 -    kw = {}
 273 -    kw["width"] = "100%"
 274 -    kw["height"] = "100%"
 275 -    kw["play"] = "true"
 276 -    kw["loop"] = "true"
 277 -    kw["quality"] = "high"
 278 -
 279 -    for arg in args:
 280 -        if '=' in arg:
 281 -            kw_count += 1
 282 -            key, value = arg.split('=', 1)
 283 -            kw[str(key)] = wikiutil.escape(value, quote=1)
 284 -    argc -= kw_count
 285 -
 286 -    if not argc:
 287 -       msg = 'Not enough arguments to EmbedObject macro! Try [[EmbedObject(attachment [,width=width] [,height=heigt])]]'
 288 -       return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 289 -    else:
 290 -        target = args[0]
 291 -
 292 -    #target = wikiutil.taintfilename(target)
 293 -    pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
 294 -    attachment_fname = AttachFile.getFilename(request, pagename, attname)
 295 -
 296 -    if not os.path.exists(attachment_fname):
 297 -        linktext = _('Upload new attachment "%(filename)s"')
 298 -        return wikiutil.link_tag(request,
 299 -            ('%s?action=AttachFile&rename=%s' % (
 300 -            wikiutil.quoteWikinameURL(pagename),
 301 -            wikiutil.url_quote_plus(attname))),
 302 -            linktext % {'filename': attname})
 303 -
 304 -    url = AttachFile.getAttachUrl(pagename, attname, request)
 305 -    mime_type, enc = mimetypes.guess_type(attname)
 306 -    if mime_type == "application/x-shockwave-flash":
 307 -        return '''
 308 -<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
 309 -WIDTH="%(width)s"
 310 -HEIGHT="%(height)s"
 311 -CODEBASE="http://active.macromedia.com/flash5/cabs/swflash.cab#version=6,0,23,0">
 312 -<PARAM NAME="MOVIE" VALUE="%(file)s">
 313 -<PARAM NAME="PLAY" VALUE="%(play)s">
 314 -<PARAM NAME="LOOP" VALUE="%(loop)s">
 315 -<PARAM NAME="QUALITY" VALUE="%(quality)s">
 316 -<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"
 317 -PLAY="%(play)s" ALIGN="" LOOP="%(loop)s" QUALITY="%(quality)s"
 318 -TYPE="application/x-shockwave-flash"
 319 -PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
 320 -</EMBED>
 321 -</OBJECT>''' % {
 322 -    "width": kw["width"],
 323 -    "height": kw["height"],
 324 -    "play": kw["play"],
 325 -    "loop": kw["loop"],
 326 -    "quality": kw["quality"],
 327 -    "file": url,
 328 -}
 329 -    elif mime_type == "image/svg+xml":
 330 -        return '''
 331 -<OBJECT CLASSID="" 
 332 -WIDTH="%(width)s"
 333 -HEIGHT="%(height)s"
 334 -CODEBASE="http://purl.org/dc/dcmitype/StillImage">
 335 -<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"
 336 -TYPE="image/svg+xml">
 337 -</EMBED>
 338 -</OBJECT>''' % {
 339 -    "width": kw["width"],
 340 -    "height": kw["height"],
 341 -    "file": url,
 342 -}
 343 -    elif mime_type == "application/pdf":
 344 -        return '''
 345 -<OBJECT CLASSID=""
 346 -WIDTH="%(width)s"
 347 -HEIGHT="%(height)s"
 348 -CODEBASE="http://www.adobe.com">
 349 -<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"
 350 -TYPE="application/pdf">
 351 -</EMBED>
 352 -</OBJECT>''' % {
 353 -    "width": kw["width"],
 354 -    "height": kw["height"],
 355 -    "file": url,
 356 -}
 357 -    elif mime_type == "audio/mpeg":
 358 -        return '''
 359 -<OBJECT CLASSID=""
 360 -WIDTH="%(width)s"
 361 -HEIGHT="%(height)s"
 362 -<EMBED SRC="%(file)s" HEIGHT="0" REPEAT="TRUE" AUTOSTART="TRUE" WIDTH="0" OP="TRUE"
 363 -TYPE="audio/mpeg">
 364 -</EMBED>
 365 -</OBJECT>''' % {
 366 -    "width": kw["width"],
 367 -    "height": kw["height"],
 368 -    "file": url,
 369 -}
 370 -    elif mime_type == "application/vnd.visio":
 371 -        return  '''
 372 -<OBJECT CLASSID="CLSID:279D6C9A-652E-4833-BEFC-312CA8887857" 
 373 -CODEBASE="http://www.microsoft.com/technet/prodtechnol/office/visio2003/depvisvw.mspx"
 374 -ID="viewer1" WIDTH="%(width)s" HEIGHT="%(height)s"> <PARAM NAME="CurrentPageIndex" VALUE="0"> 
 375 -<PARAM NAME="Zoom" VALUE="-1"> <PARAM NAME = "SRC" 
 376 -VALUE = "%(file)s">Your browser cannot display Visio</OBJECT>''' % {
 377 -    "width": kw['width'],
 378 -    "height": kw['height'],
 379 -    "file": url,
 380 -}
 381 -    elif mime_type == "audio/midi":
 382 -        return '''
 383 -<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
 384 -WIDTH="%(width)s"
 385 -HEIGHT="%(height)s"
 386 -<EMBED SRC="%(file)s" HEIGHT="0" REPEAT="TRUE" AUTOSTART="TRUE" WIDTH="0" OP="TRUE"
 387 -TYPE="audio/midi">
 388 -</EMBED>
 389 -</OBJECT>''' % {
 390 -    "width": kw["width"],
 391 -    "height": kw["height"],
 392 -    "file": url,
 393 -}
 394 -    elif mime_type == "video/mpeg":
 395 -        return '''
 396 -<OBJECT CLASSID="CLSID:05589FA1-C356-11CE-BF01-00AA0055595A"
 397 -WIDTH="%(width)s"
 398 -HEIGHT="%(height)s"
 399 -<EMBED SRC="%(file)s" HEIGHT="0" REPEAT="TRUE" AUTOSTART="TRUE" WIDTH="0" OP="TRUE"
 400 -TYPE="application/x-mplayer2">
 401 -</EMBED>
 402 -</OBJECT>''' % {
 403 -   "width": kw["width"],
 404 -   "height": kw["height"],
 405 -   "file": url,
 406 -}
 407 -    elif mime_type == "video/quicktime":
 408 -        return '''
 409 -<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
 410 -WIDTH="%(width)s"
 411 -HEIGHT="%(height)s"
 412 -<EMBED SRC="%(file)s" HEIGHT="0" REPEAT="TRUE" AUTOSTART="TRUE" WIDTH="0" OP="TRUE"
 413 -TYPE="video/quicktime">
 414 -</EMBED>
 415 -</OBJECT>''' % {
 416 -   "width": kw["width"],
 417 -   "height": kw["height"],
 418 -   "file": url,
 419 -}
 420 -    else:
 421 -        msg = 'Not supported mimetype %(mimetype)s ' % {"mimetype": mime_type}
 422 -        return "%s%s%s" % (macro.formatter.sysmsg(1),
 423 -                   macro.formatter.text(msg),
 424 -                   macro.formatter.sysmsg(0))
 425 -
 426 +    return EmbedObject(macro, args).render()
 427 +

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] (2006-10-08 08:22:31, 8.5 KB) [[attachment:EmbedObject.py]]
  • [get | view] (2006-10-08 08:22:14, 14.0 KB) [[attachment:EmbedObject_20061008_patch.txt]]
  • [get | view] (2006-10-01 18:09:12, 13.3 KB) [[attachment:EmbedObject_patch_20061001.txt]]
  • [get | view] (2006-10-05 07:50:34, 13.3 KB) [[attachment:EmbedObject_patch_20061005.txt]]
 All files | Selected Files: delete move to page copy to page

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