From: Vladimír Vondruš Date: Mon, 11 Sep 2017 15:15:54 +0000 (+0200) Subject: m.htmlsanity: patch image node writing to not include useless alt attrib. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=5011783d38c414ef7b142aae2ba73bd63b6144bc;p=blog.git m.htmlsanity: patch image node writing to not include useless alt attrib. --- 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