Attachment 'EmbedObject_20061008_patch.txt'

Download

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

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.