diff --git a/CHANGES b/CHANGES index c79f7c6fe..cbc92be00 100644 --- a/CHANGES +++ b/CHANGES @@ -135,6 +135,7 @@ Bugs fixed * #5637: inheritance_diagram: Incorrect handling of nested class names * #7139: ``code-block:: guess`` does not work * #7325: html: source_suffix containing dot leads to wrong source link +* #7357: html: Resizing SVG image fails with ValueError * #7278: html search: Fix use of ``html_file_suffix`` instead of ``html_link_suffix`` in search results * #7297: html theme: ``bizstyle`` does not support ``sidebarwidth`` diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index e74c0334f..85eeb4376 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -11,6 +11,7 @@ import copy import os import posixpath +import re import warnings from typing import Any, Iterable, Tuple from typing import cast @@ -38,6 +39,19 @@ logger = logging.getLogger(__name__) # http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html +def multiply_length(length: str, scale: int) -> str: + """Multiply *length* (width or height) by *scale*.""" + matched = re.match(r'^(\d*\.?\d*)\s*(\S*)$', length) + if not matched: + return length + elif scale == 100: + return length + else: + amount, unit = matched.groups() + result = float(amount) * scale / 100 + return "%s%s" % (int(result), unit) + + class HTMLWriter(Writer): # override embed-stylesheet default value to 0. @@ -597,11 +611,10 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator): 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 + atts['width'] = multiply_length(atts['width'], node['scale']) if 'height' in atts: - atts['height'] = int(atts['height']) * scale + atts['height'] = multiply_length(atts['height'], node['scale']) atts['alt'] = node.get('alt', uri) if 'align' in node: atts['class'] = 'align-%s' % node['align'] diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 0c00a1fa4..f94dd60a3 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -10,6 +10,7 @@ import os import posixpath +import re import warnings from typing import Any, Iterable, Tuple from typing import cast @@ -37,6 +38,19 @@ logger = logging.getLogger(__name__) # http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html +def multiply_length(length: str, scale: int) -> str: + """Multiply *length* (width or height) by *scale*.""" + matched = re.match(r'^(\d*\.?\d*)\s*(\S*)$', length) + if not matched: + return length + elif scale == 100: + return length + else: + amount, unit = matched.groups() + result = float(amount) * scale / 100 + return "%s%s" % (int(result), unit) + + class HTML5Translator(SphinxTranslator, BaseTranslator): """ Our custom HTML translator. @@ -538,11 +552,10 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): 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 + atts['width'] = multiply_length(atts['width'], node['scale']) if 'height' in atts: - atts['height'] = int(atts['height']) * scale + atts['height'] = multiply_length(atts['height'], node['scale']) atts['alt'] = node.get('alt', uri) if 'align' in node: atts['class'] = 'align-%s' % node['align']