Updated images renderers to add alternative text
authorThierry Florac <tflorac@ulthar.net>
Mon, 07 Sep 2020 16:07:08 +0200
changeset 209 b146beab5367
parent 208 44d15c8590e9
child 210 c3e90a2d36a0
Updated images renderers to add alternative text
src/pyams_file/image.py
src/pyams_file/templates/svg-render.pt
src/pyams_file/thumbnail.py
--- a/src/pyams_file/image.py	Mon Jul 20 15:21:00 2020 +0200
+++ b/src/pyams_file/image.py	Mon Sep 07 16:07:08 2020 +0200
@@ -16,6 +16,7 @@
 from io import BytesIO
 
 from PIL import Image, ImageFilter
+from pyquery import PyQuery
 from pyramid.renderers import render
 from zope.dublincore.interfaces import IZopeDublinCore
 from zope.interface import implementer
@@ -282,9 +283,9 @@
 # SVG utilities
 #
 
-def render_svg(image, width=None, height=None, request=None, css_class='', img_class=''):
+def render_svg(image, width=None, height=None, request=None, css_class='', img_class='', alt=''):
     """Render SVG file"""
-    options = {'svg': image}
+    options = {}
     if width or height:
         options['style'] = 'width: {0}{1}; height: {2}{3};'.format(
             width, 'px' if isinstance(width, int) else '',
@@ -292,14 +293,23 @@
     else:
         options['style'] = ''
     options['css_class'] = css_class
-    result = render('templates/svg-render.pt', options, request)
+    svg = image.data
+    if alt:
+        svg = PyQuery(svg)
+        g = PyQuery('<g></g>')
+        g.append(PyQuery('<title />').text(alt))
+        for child in svg.children():
+            g.append(child)
+        svg.empty().append(g)
+        svg = svg.outer_html()
     if img_class:
-        result = result.replace('<svg ', '<svg class="{0}" '.format(img_class))
-    return result
+        svg.attr('class', img_class)
+    options['svg'] = svg
+    return render('templates/svg-render.pt', options, request)
 
 
 def render_img(image, width=None, height=None, request=None,
-               css_class='', img_class='', timestamp=False):
+               css_class='', img_class='', timestamp=False, alt=''):
     """Render image thumbnail"""
     thumbnail = None
     thumbnails = IThumbnails(image, None)
@@ -322,16 +332,16 @@
         else:
             timestamp = dc.modified.timestamp()
         url += '?_={0}'.format(timestamp)
-    result = '<img src="{0}" class="{1}" />'.format(url, img_class)
+    result = '<img src="{0}" class="{1}" alt="{2}" />'.format(url, img_class, alt)
     if css_class:
         result = '<div class="{0}">{1}</div>'.format(css_class, result)
     return result
 
 
 def render_image(image, width=None, height=None, request=None,
-                 css_class='', img_class='', timestamp=False):
+                 css_class='', img_class='', timestamp=False, alt=''):
     """Render image"""
     if ISVGImage.providedBy(image):
-        return render_svg(image, width, height, request, css_class, img_class)
+        return render_svg(image, width, height, request, css_class, img_class, alt)
     else:
-        return render_img(image, width, height, request, css_class, img_class, timestamp)
+        return render_img(image, width, height, request, css_class, img_class, timestamp, alt)
--- a/src/pyams_file/templates/svg-render.pt	Mon Jul 20 15:21:00 2020 +0200
+++ b/src/pyams_file/templates/svg-render.pt	Mon Sep 07 16:07:08 2020 +0200
@@ -1,4 +1,4 @@
 <div class="display-inline align-middle svg-container"
 	 tal:attributes="class css_class + ' ' + default; style style;">
-	<svg tal:replace="structure svg.data" />
-</div>
+	<svg tal:replace="structure svg" />
+</div>
\ No newline at end of file
--- a/src/pyams_file/thumbnail.py	Mon Jul 20 15:21:00 2020 +0200
+++ b/src/pyams_file/thumbnail.py	Mon Sep 07 16:07:08 2020 +0200
@@ -291,7 +291,7 @@
     If image in an SVG image, a "div" is returned containing whole SVG data of given image.
     """
 
-    def render(self, context=None, width=None, height=None, css_class='', img_class=''):
+    def render(self, context=None, width=None, height=None, css_class='', img_class='', alt=''):
         if context is None:
             context = self.context
-        return render_image(context, width, height, self.request, css_class, img_class, True)
+        return render_image(context, width, height, self.request, css_class, img_class, True, alt)