diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index a1565a1a7..2a83474ce 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -246,14 +246,14 @@ def render_dot_html(self, node, code, options, prefix='graphviz', 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': svgtag = '''

%s

\n''' % (fname, alt) self.body.append(svgtag) else: - if 'align' in node: - self.body.append('
' % - (node['align'], node['align'])) with open(outfn + '.map', 'rb') as mapfile: imgmap = mapfile.readlines() if len(imgmap) == 2: @@ -266,8 +266,8 @@ def render_dot_html(self, node, code, options, prefix='graphviz', self.body.append('%s\n' % (fname, alt, mapname, imgcss)) self.body.extend([item.decode('utf-8') for item in imgmap]) - if 'align' in node: - self.body.append('
\n') + if 'align' in node: + self.body.append('
\n') raise nodes.SkipNode @@ -286,24 +286,26 @@ def render_dot_latex(self, node, code, options, prefix='graphviz'): raise nodes.SkipNode is_inline = self.is_inline(node) - if is_inline: - para_separator = '' - else: - para_separator = '\n' - if fname is not None: - post = None # type: unicode - if not is_inline and 'align' in node: + if not is_inline: + pre = '' + post = '' + if 'align' in node: if node['align'] == 'left': - self.body.append('{') - post = '\\hspace*{\\fill}}' + pre = '{' + post = r'\hspace*{\fill}}' elif node['align'] == 'right': - self.body.append('{\\hspace*{\\fill}') + pre = r'{\hspace*{\fill}' post = '}' - self.body.append('%s\\includegraphics{%s}%s' % - (para_separator, fname, para_separator)) - if post: - self.body.append(post) + elif node['align'] == 'center': + pre = r'{\hfill' + post = r'\hspace*{\fill}}' + self.body.append('\n%s' % pre) + + self.body.append(r'\includegraphics{%s}' % fname) + + if not is_inline: + self.body.append('%s\n' % post) raise nodes.SkipNode diff --git a/tests/roots/test-ext-graphviz/index.rst b/tests/roots/test-ext-graphviz/index.rst index ab86e2a5a..930ec656d 100644 --- a/tests/roots/test-ext-graphviz/index.rst +++ b/tests/roots/test-ext-graphviz/index.rst @@ -25,3 +25,8 @@ Hello |graph| graphviz world :caption: on right foo -> bar + +.. digraph:: foo + :align: center + + centered diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index 6affda7ab..1d2a3ab2f 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -16,7 +16,7 @@ import pytest @pytest.mark.sphinx('html', testroot='ext-graphviz') @pytest.mark.usefixtures('if_graphviz_found') -def test_graphviz_html(app, status, warning): +def test_graphviz_png_html(app, status, warning): app.builder.build_all() content = (app.outdir / 'index.html').text() @@ -34,6 +34,51 @@ def test_graphviz_html(app, status, warning): r'on right.*

\s*') assert re.search(html, content, re.S) + html = (r'
' + r'\"digraph\n
') + assert re.search(html, content, re.S) + +@pytest.mark.sphinx('html', testroot='ext-graphviz', + confoverrides={'graphviz_output_format': 'svg'}) +@pytest.mark.usefixtures('if_graphviz_found') +def test_graphviz_svg_html(app, status, warning): + app.builder.build_all() + + content = (app.outdir / 'index.html').text() + + html = (r'
\n' + r'\n' + r'\s+

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

\n' + r'

' + r'caption of graph.*

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

graph

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

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

\n' + r'

' + r'on right.*

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

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

\n' + r'
') + assert re.search(html, content, re.S) @pytest.mark.sphinx('latex', testroot='ext-graphviz') @pytest.mark.usefixtures('if_graphviz_found') @@ -54,6 +99,11 @@ def test_graphviz_latex(app, status, warning): '\\\\caption{on right}\\\\label{.*}\\\\end{wrapfigure}') assert re.search(macro, content, re.S) + macro = (r'\{\\hfill' + r'\\includegraphics{graphviz-.*}' + r'\\hspace\*{\\fill}}') + assert re.search(macro, content, re.S) + @pytest.mark.sphinx('html', testroot='ext-graphviz', confoverrides={'language': 'xx'}) @pytest.mark.usefixtures('if_graphviz_found')