From 5011783d38c414ef7b142aae2ba73bd63b6144bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 11 Sep 2017 17:15:54 +0200 Subject: [PATCH] m.htmlsanity: patch image node writing to not include useless alt attrib. --- pelican-plugins/m/htmlsanity.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pelican-plugins/m/htmlsanity.py b/pelican-plugins/m/htmlsanity.py index a74aa1fc..ed34c941 100644 --- a/pelican-plugins/m/htmlsanity.py +++ b/pelican-plugins/m/htmlsanity.py @@ -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 .... + 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) + '' + suffix) + else: + self.body.append(self.emptytag(node, 'img', suffix, **atts)) + + def depart_image(self, node): + pass + # Use HTML5
tag for sections (instead of
) def visit_section(self, node): self.section_level += 1 -- 2.30.2