changeset 1876:e308a0e74069

EmbedObject: alt tag code from Oliver Siemoneit added and some optimisations
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 11 Mar 2007 18:08:12 +0100
parents 5afb427026ea
children 970334e27707
files MoinMoin/macro/EmbedObject.py
diffstat 1 files changed, 74 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/macro/EmbedObject.py	Sun Mar 11 13:09:40 2007 +0100
+++ b/MoinMoin/macro/EmbedObject.py	Sun Mar 11 18:08:12 2007 +0100
@@ -8,7 +8,7 @@
         the kind of application.
 
     CALLING SEQUENCE:
-        [[EmbedObject(attachment[,width=width][,height=height])]]
+        [[EmbedObject(attachment[,width=width][,height=height][,alt=Embedded mimetpye/xy])]]
 
     SUPPORTED MIMETYPES:  
          application/x-shockwave-flash
@@ -45,6 +45,7 @@
 
            width = ""
            height = ""
+           alt = "Embedded mimetpye/xy"
            type = mime_type
            play = false
            loop = false
@@ -55,22 +56,31 @@
            menu = true
       
 
-        All do use width, height, mime_type   
+        All do use width, height, mime_type, alt   
         
         in addition:
            'video' do use  repeat, autostart, menu, op
            'audio' do use   play, repeat, autostart, op, hidden
                    the default width is 60 and default height is 20
            'application' do use play, menu, autostart
-        
+
+        Note: Please do provide always a sensible alt text for the embedded object which
+        gives a short description of the visually or acoustically presented content so
+        that visually and acoustically impaired people can at least get a clue of what's
+        going on in this "black box". By default alt is set to "Embedded mimetpye/xy" for
+        people that forget to set an alt. However this default alt text is not a sensible
+        one since it does not describe the content really but only the type of content.
+        Compare these alt texts: "Embedded application/pdf" vs. "MoinMoin Tutorial embedded
+        as PDF file"
     
     EXAMPLE:
         [[EmbedObject]]
-        [[EmbedObject(example.swf)]]
-        [[EmbedObject(example.pdf]]
-        [[EmbedObject(example.svg]]
-        [[EmbedObject(example.mp3]]
-        [[EmbedObject(example.vss]]
+        [[EmbedObject(example.swf,alt=A flash movie showing the rotating moin logo)]]
+        [[EmbedObject(example.mid,alt=Background sound of wikipage: oceanwaves)]]
+        [[EmbedObject(example.pdf)]]
+        [[EmbedObject(example.svg)]]
+        [[EmbedObject(example.mp3)]]
+        [[EmbedObject(example.vss)]]
          
         [[EmbedObject(example.swf,width=637,height=392)]]
         [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
@@ -88,9 +98,10 @@
         I haven't added it by now.
 
         Please add needed mimetypes as objects.
-    
+
+           
     RESTRICTIONS:
-        some mimetypes do ignore all used keywords. May be they do use different names.        
+        Some mimetypes do ignore all used keywords. May be they do use different names.        
 
 
     MODIFICATION HISTORY:
@@ -105,6 +116,8 @@
         2006-10-01 RB code refactored
         2006-10-05 RB bug fixed closing " at height added
         2006-10-08 RB type is needed on some platforms, some more keywords added
+        2007-02-10 OliverSiemoneit: alt and noembed tags added for AccessibleMoin; fixed
+                   output abstraction violation.
 """
 import os, mimetypes
 
@@ -114,6 +127,7 @@
 class EmbedObject:
 
     def __init__(self, macro, args):
+        self._ = macro.request.getText
         self.macro = macro
         self.request = macro.request
         self.formatter = macro.formatter
@@ -121,6 +135,7 @@
 
         self.width = ""
         self.height = ""
+        self.alt = ""
         self.play = "false"
         self.loop = "false"
         self.quality = "high"
@@ -130,6 +145,7 @@
         self.align = "center"
         self.hidden = "false"
         self.menu = "true"
+        self.target = None
 
         if args:
             args = args.split(',')
@@ -139,27 +155,29 @@
 
         kw_count = 0
         argc = len(args)
-        for arg in self.args.split(','):
-            if '=' in arg:
-                kw_count += 1
-                key, value = arg.split('=')
-                setattr(self, key, wikiutil.escape(value.strip(), quote=1))
-
-        argc -= kw_count
-
-        if not argc:
-           msg = 'Not enough arguments to EmbedObject macro! Try [[EmbedObject(attachment [,width=width] [,height=heigt])]]'
-           return "%s%s%s" % (self.formatter.sysmsg(1), self.formatter.text(msg), self.formatter.sysmsg(0))
-        else:
+        if args:
+            for arg in self.args.split(','):
+                if '=' in arg:
+                    kw_count += 1
+                    key, value = arg.split('=')
+                    setattr(self, key, wikiutil.escape(value.strip(), quote=1))
+                    argc -= kw_count
             self.target = args[0]
 
     def embed(self, mime_type, file):
+        _ = self._
         mtype = mime_type.split('/')
 
+        if self.alt == "":
+            self.alt = "%(text)s %(mime_type)s" % {'text': _("Embedded"), 'mime_type': mime_type,}
+
         if mtype[0] == 'video':
             return '''
 <OBJECT>
 <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>
+<NOEMBED>
+<p>%(alt)s</p>
+</NOEMBED>
 </OBJECT>''' % {
     "width": self.width,
     "height": self.height,
@@ -169,17 +187,22 @@
     "op": self.op,
     "type": mime_type,
     "menu": self.menu,
+    "alt": self.alt,
 }
 
         if mtype[0] in ['image', 'chemical', 'x-world']:
             return '''
 <OBJECT>
 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" TYPE="%(type)s"></EMBED>
+<NOEMBED>
+<p>%(alt)s</p>
+</NOEMBED>
 </OBJECT>''' % {
     "width": self.width,
     "height": self.height,
     "file": file,
     "type": mime_type,
+    "alt": self.alt,
 }
 
         if mtype[0] == 'audio':
@@ -190,22 +213,29 @@
             return '''
 <OBJECT>
 <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>
+<NOEMBED>
+<p>%(alt)s</p>
+</NOEMBED>
 </OBJECT>''' % {
-   "width": self.width,
-   "height": self.height,
-   "file": file,
-   "play": self.play,
-   "repeat": self.repeat,
-   "autostart": self.autostart,
-   "op": self.op,
-   "hidden": self.hidden,
-   "type": mime_type,
+    "width": self.width,
+    "height": self.height,
+    "file": file,
+    "play": self.play,
+    "repeat": self.repeat,
+    "autostart": self.autostart,
+    "op": self.op,
+    "hidden": self.hidden,
+    "type": mime_type,
+    "alt": self.alt,
 }
 
         if mtype[0] == 'application':
             return '''
 <OBJECT>
 <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>
+<NOEMBED>
+<p>%(alt)s</p>
+</NOEMBED>
 </OBJECT>''' % {
     "width": self.width,
     "height": self.height,
@@ -215,10 +245,15 @@
     "loop": self.loop,
     "type": mime_type,
     "menu": self.menu,
+    "alt": self.alt,
 }
 
     def render(self):
-        _ = self.request.getText
+        _ = self._
+
+        if not self.target:
+            msg = 'Not enough arguments to EmbedObject macro! Try [[EmbedObject(attachment [,width=width] [,height=height] [,alt=Embedded mimetpye/xy])]]'
+            return "%s%s%s" % (self.formatter.sysmsg(1), self.formatter.text(msg), self.formatter.sysmsg(0))
 
         pagename, attname = AttachFile.absoluteName(self.target, self.formatter.page.page_name)
         attachment_fname = AttachFile.getFilename(self.request, pagename, attname)
@@ -259,8 +294,13 @@
 
                          "x-world/x-vrml",
                        ]:
-
-            return self.embed(mime_type, url)
+            # XXX Should better use formatter.embed if available?
+            try:
+                return self.macro.formatter.rawHTML(self.embed(mime_type, url))
+            except:
+                return "%s%s%s" % (self.macro.formatter.sysmsg(1),
+                                   self.macro.formatter.text('Embedding of object by choosen formatter not possible'),
+                                   self.macro.formatter.sysmsg(0))
 
         else:
             msg = 'Not supported mimetype %(mimetype)s ' % {"mimetype": mime_type}