chiark / gitweb /
m.htmlsanity: patch image node writing to not include useless alt attrib.
authorVladimír Vondruš <mosra@centrum.cz>
Mon, 11 Sep 2017 15:15:54 +0000 (17:15 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Thu, 14 Sep 2017 22:11:11 +0000 (00:11 +0200)
pelican-plugins/m/htmlsanity.py

index a74aa1fc33e84c49411dcaa82842e4913a0f4626..ed34c94172dba2feb443061d492c1a5eb131e78b 100644 (file)
@@ -1,3 +1,4 @@
+import os.path
 import re
 
 from docutils.writers.html5_polyglot import HTMLTranslator
@@ -191,6 +192,35 @@ class SaneHtmlTranslator(HTMLTranslator):
         self.docinfo = self.body[start:]
         self.body = []
 
+    # Remove useless cruft from images, such as width, height, scale; don't put
+    # URI in alt text.
+    def visit_image(self, node):
+        atts = {}
+        uri = node['uri']
+        ext = os.path.splitext(uri)[1].lower()
+        if ext in self.object_image_types:
+            atts['data'] = uri
+            atts['type'] = self.object_image_types[ext]
+        else:
+            atts['src'] = uri
+            if 'alt' in node: atts['alt'] = node['alt']
+        if (isinstance(node.parent, nodes.TextElement) or
+            (isinstance(node.parent, nodes.reference) and
+             not isinstance(node.parent.parent, nodes.TextElement))):
+            # Inline context or surrounded by <a>...</a>.
+            suffix = ''
+        else:
+            suffix = '\n'
+        if ext in self.object_image_types:
+            # do NOT use an empty tag: incorrect rendering in browsers
+            self.body.append(self.starttag(node, 'object', suffix, **atts) +
+                             node.get('alt', uri) + '</object>' + suffix)
+        else:
+            self.body.append(self.emptytag(node, 'img', suffix, **atts))
+
+    def depart_image(self, node):
+        pass
+
     # Use HTML5 <section> tag for sections (instead of <div class="section">)
     def visit_section(self, node):
         self.section_level += 1