diff options
Diffstat (limited to 'sphinx/writers/html5.py')
-rw-r--r-- | sphinx/writers/html5.py | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 4d6089718..b8adb7ea3 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -519,6 +519,21 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): node['uri'] = posixpath.join(self.builder.imgpath, self.builder.images[olduri]) + if 'scale' in node: + # Try to figure out image height and width. Docutils does that too, + # but it tries the final file name, which does not necessarily exist + # yet at the time the HTML file is written. + if not ('width' in node and 'height' in node): + size = get_image_size(os.path.join(self.builder.srcdir, olduri)) + if size is None: + logger.warning(__('Could not obtain image size. :scale: option is ignored.'), # NOQA + location=node) + else: + if 'width' not in node: + node['width'] = str(size[0]) + if 'height' not in node: + node['height'] = str(size[1]) + uri = node['uri'] if uri.lower().endswith(('svg', 'svgz')): atts = {'src': uri} @@ -526,6 +541,12 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): atts['width'] = node['width'] if 'height' in node: atts['height'] = node['height'] + if 'scale' in node: + scale = node['scale'] / 100.0 + if 'width' in atts: + atts['width'] = int(atts['width']) * scale + if 'height' in atts: + atts['height'] = int(atts['height']) * scale atts['alt'] = node.get('alt', uri) if 'align' in node: self.body.append('<div align="%s" class="align-%s">' % @@ -536,20 +557,6 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): self.body.append(self.emptytag(node, 'img', '', **atts)) return - if 'scale' in node: - # Try to figure out image height and width. Docutils does that too, - # but it tries the final file name, which does not necessarily exist - # yet at the time the HTML file is written. - if not ('width' in node and 'height' in node): - size = get_image_size(os.path.join(self.builder.srcdir, olduri)) - if size is None: - logger.warning(__('Could not obtain image size. :scale: option is ignored.'), # NOQA - location=node) - else: - if 'width' not in node: - node['width'] = str(size[0]) - if 'height' not in node: - node['height'] = str(size[1]) super().visit_image(node) # overwritten |