From b689fb1c12252984a3fa0ed90e7b0276919cf22b Mon Sep 17 00:00:00 2001 From: Michael Goerz Date: Fri, 20 Apr 2018 02:10:30 -0400 Subject: [PATCH] Wrap graphviz diagrams in
The graphviz extension is modified to wrap the `` and `` tags it creates for diagrams in the png and svg formats in an additional `
...
` tag. This allows to apply additional CSS styling to the the diagrams that cannot be applied to `` and `` directly. Most notably, div.graphviz { overflow: auto; } would be a useful style that the standard themes might consider. In any case, it would allow users to add custom CSS code to their project that gets around the problems with inheritance-diagrams discussed in https://github.com/sphinx-doc/sphinx/issues/4865 The tests for the `grahviz` and `inheritance_diagram` have been modified to test for the presence of the additional `
` tag. Closes #4865 --- sphinx/ext/graphviz.py | 12 ++++++++---- tests/test_ext_graphviz.py | 28 ++++++++++++++------------- tests/test_ext_inheritance_diagram.py | 6 ++++-- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index bc348f4e1..f0b321378 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -290,21 +290,25 @@ def render_dot_html(self, node, code, options, prefix='graphviz', self.body.append('
' % (node['align'], node['align'])) if format == 'svg': - svgtag = ''' -

%s

\n''' % (fname, alt) + svgtag = '''
+

%s

\n''' % (fname, alt) self.body.append(svgtag) else: with codecs.open(outfn + '.map', 'r', encoding='utf-8') as mapfile: # type: ignore imgmap = ClickableMapDefinition(outfn + '.map', mapfile.read(), dot=code) if imgmap.clickable: # has a map - self.body.append('%s\n' % + self.body.append('
') + self.body.append('%s' % (fname, alt, imgmap.id, imgcss)) + self.body.append('
\n') self.body.append(imgmap.generate_clickable_map()) else: # nothing in image map - self.body.append('%s\n' % + self.body.append('
') + self.body.append('%s' % (fname, alt, imgcss)) + self.body.append('
\n') if 'align' in node: self.body.append('
\n') diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index 3efab6b56..df096ce62 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -22,24 +22,26 @@ def test_graphviz_png_html(app, status, warning): app.builder.build_all() content = (app.outdir / 'index.html').text() - html = (r'
\s*\s*

' + html = (r'

\s*' + r'
\s*

' r'caption of graph.*

\s*
') assert re.search(html, content, re.S) - html = 'Hello \n graphviz world' + html = 'Hello
\n graphviz world' assert re.search(html, content, re.S) html = 'digraph {\n  bar -> baz\n}' assert re.search(html, content, re.M) - html = (r'
\s*\s*

' + html = (r'

\s*' + r'
\s*

' r'on right.*

\s*
') assert re.search(html, content, re.S) html = (r'
' - r'\"digraph\"digraph\n
') + r'}\" />
\n
') assert re.search(html, content, re.S) @@ -52,34 +54,34 @@ def test_graphviz_svg_html(app, status, warning): content = (app.outdir / 'index.html').text() html = (r'
\n' - r'\n' + r'
\n' r'\s+

digraph foo {\n' r'bar -> baz\n' - r'}

\n' + r'}

\n' r'

' r'caption of graph.*

\n
') assert re.search(html, content, re.S) - html = (r'Hello \n' - r'\s+

graph

\n' + html = (r'Hello
\n' + r'\s+

graph

\n' r' graphviz world') assert re.search(html, content, re.S) html = (r'
\n' - r'\n' + r'
\n' r'\s+

digraph bar {\n' r'foo -> bar\n' - r'}

\n' + r'}

\n' r'

' r'on right.*

\n' r'') assert re.search(html, content, re.S) html = (r'
' - r'\n' + r'
\n' r'\s+

digraph foo {\n' r'centered\n' - r'}

\n' + r'}

\n' r'') assert re.search(html, content, re.S) diff --git a/tests/test_ext_inheritance_diagram.py b/tests/test_ext_inheritance_diagram.py index ad106e6c6..dfee015e0 100644 --- a/tests/test_ext_inheritance_diagram.py +++ b/tests/test_ext_inheritance_diagram.py @@ -25,8 +25,9 @@ def test_inheritance_diagram_html(app, status, warning): content = (app.outdir / 'index.html').text() pattern = ('
\n' + '
' 'Inheritance diagram of test.Foo\n

' + 'class="inheritance"/>

\n

' 'Test Foo!\xb6

') assert re.search(pattern, content, re.M) @@ -62,8 +63,9 @@ def test_inheritance_diagram_latex_alias(app, status, warning): content = (app.outdir / 'index.html').text() pattern = ('
\n' + '
' 'Inheritance diagram of test.Foo\n

' + 'class="inheritance"/>

\n

' 'Test Foo!\xb6

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