diff --git a/CHANGES b/CHANGES index b83f1925f..3f2fbbf7e 100644 --- a/CHANGES +++ b/CHANGES @@ -200,6 +200,7 @@ Bugs fixed * #5114: sphinx-build: Handle errors on scanning documents * epub: spine has been broken when "self" is listed on toctree (refs: #4611) * #344: autosummary does not understand docstring of module level attributes +* #5002: graphviz: SVGs do not adapt to the column width Testing -------- diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index 11d9e54a0..3b33c7b79 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -27,6 +27,7 @@ from sphinx.errors import SphinxError from sphinx.locale import _, __ from sphinx.util import logging from sphinx.util.docutils import SphinxDirective +from sphinx.util.fileutil import copy_asset_file from sphinx.util.i18n import search_image_for_language from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL @@ -281,19 +282,23 @@ def render_dot_html(self, node, code, options, prefix='graphviz', logger.warning(__('dot code %r: %s'), code, text_type(exc)) raise nodes.SkipNode + if imgcls: + imgcls += " graphviz" + else: + imgcls = "graphviz" + if fname is None: self.body.append(self.encode(code)) else: if alt is None: alt = node.get('alt', self.encode(code).strip()) - imgcss = imgcls and 'class="%s"' % imgcls or '' if 'align' in node: self.body.append('
' % (node['align'], node['align'])) if format == 'svg': self.body.append('
') - self.body.append('\n' % - (fname, imgcss)) + self.body.append('\n' % + (fname, imgcls)) self.body.append('

%s

' % alt) self.body.append('
\n') else: @@ -302,15 +307,15 @@ def render_dot_html(self, node, code, options, prefix='graphviz', if imgmap.clickable: # has a map self.body.append('
') - self.body.append('%s' % - (fname, alt, imgmap.id, imgcss)) + self.body.append('%s' % + (fname, alt, imgmap.id, imgcls)) self.body.append('
\n') self.body.append(imgmap.generate_clickable_map()) else: # nothing in image map self.body.append('
') - self.body.append('%s' % - (fname, alt, imgcss)) + self.body.append('%s' % + (fname, alt, imgcls)) self.body.append('
\n') if 'align' in node: self.body.append('\n') @@ -396,6 +401,14 @@ def man_visit_graphviz(self, node): raise nodes.SkipNode +def on_build_finished(app, exc): + # type: (Sphinx, Exception) -> None + if exc is None: + src = path.join(sphinx.package_dir, 'templates', 'graphviz', 'graphviz.css') + dst = path.join(app.outdir, '_static') + copy_asset_file(src, dst) + + def setup(app): # type: (Sphinx) -> Dict[unicode, Any] app.add_node(graphviz, @@ -410,4 +423,6 @@ def setup(app): app.add_config_value('graphviz_dot', 'dot', 'html') app.add_config_value('graphviz_dot_args', [], 'html') app.add_config_value('graphviz_output_format', 'png', 'html') + app.add_css_file('graphviz.css') + app.connect('build-finished', on_build_finished) return {'version': sphinx.__display_version__, 'parallel_read_safe': True} diff --git a/sphinx/templates/graphviz/graphviz.css b/sphinx/templates/graphviz/graphviz.css new file mode 100644 index 000000000..1f246c8d9 --- /dev/null +++ b/sphinx/templates/graphviz/graphviz.css @@ -0,0 +1,19 @@ +/* + * graphviz.css + * ~~~~~~~~~~~~ + * + * Sphinx stylesheet -- graphviz extension. + * + * :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS. + * :license: BSD, see LICENSE for details. + * + */ + +img.graphviz { + border: 0; + max-width: 100%; +} + +object.graphviz { + max-width: 100%; +} diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index 9b53876db..d0358b407 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -30,7 +30,7 @@ def test_graphviz_png_html(app, status, warning): html = 'Hello
\n graphviz world' assert re.search(html, content, re.S) - html = 'digraph {\n  bar -> baz\n}' + html = 'digraph {\n  bar -> baz\n}' assert re.search(html, content, re.M) html = (r'
\s*' @@ -41,7 +41,7 @@ def test_graphviz_png_html(app, status, warning): html = (r'
' r'
\"digraph
\n
') + r'}\" class="graphviz" />
\n') assert re.search(html, content, re.S) @@ -117,7 +117,7 @@ def test_graphviz_i18n(app, status, warning): app.builder.build_all() content = (app.outdir / 'index.html').text() - html = 'digraph {\n  BAR -> BAZ\n}' + html = 'digraph {\n  BAR -> BAZ\n}' assert re.search(html, content, re.M) diff --git a/tests/test_ext_inheritance_diagram.py b/tests/test_ext_inheritance_diagram.py index 8456cda4a..711e0712c 100644 --- a/tests/test_ext_inheritance_diagram.py +++ b/tests/test_ext_inheritance_diagram.py @@ -27,8 +27,8 @@ def test_inheritance_diagram_png_html(app, status, warning): pattern = ('
\n' '
' 'Inheritance diagram of test.Foo
\n

' - 'Test Foo!

\n

' + 'Test Foo!\xb6

') assert re.search(pattern, content, re.M) @@ -44,7 +44,7 @@ def test_inheritance_diagram_svg_html(app, status, warning): pattern = ('
\n' '
' '\n' + 'type="image/svg\\+xml" class="inheritance graphviz">\n' '

Inheritance diagram of test.Foo

' '
\n

' 'Test Foo!\n' '

\n

' - 'Test Foo!

\n

' + 'Test Foo!\xb6

') assert re.search(pattern, content, re.M)