diff --git a/CHANGES b/CHANGES index 8aa32fac4..5d682ddb8 100644 --- a/CHANGES +++ b/CHANGES @@ -14,7 +14,7 @@ Incompatible changes conf.py that will be provided on sphinx-quickstart. * #2027, #2208: The ``html_title`` accepts string values only. And The None value cannot be accepted. - +* ``sphinx.ext.graphviz``: show graph image in inline by default Features added -------------- diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index 3701d40fe..f795976e7 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -36,7 +36,7 @@ class GraphvizError(SphinxError): category = 'Graphviz error' -class graphviz(nodes.General, nodes.Element): +class graphviz(nodes.General, nodes.Inline, nodes.Element): pass @@ -102,7 +102,7 @@ class Graphviz(Directive): node['inline'] = 'inline' in self.options caption = self.options.get('caption') - if caption and not node['inline']: + if caption: node = figure_wrapper(self, node, caption) return [node] @@ -132,7 +132,7 @@ class GraphvizSimple(Directive): node['inline'] = 'inline' in self.options caption = self.options.get('caption') - if caption and not node['inline']: + if caption: node = figure_wrapper(self, node, caption) return [node] @@ -197,6 +197,15 @@ def render_dot(self, code, options, format, prefix='graphviz'): return relfn, outfn +def warn_for_deprecated_option(self, node): + if hasattr(self.builder, '_graphviz_warned_inline'): + return + + if 'inline' in node: + self.builder.warn(':inline: option for graphviz is deprecated since version 1.4.0.') + self.builder._graphviz_warned_inline = True + + def render_dot_html(self, node, code, options, prefix='graphviz', imgcls=None, alt=None): format = self.builder.config.graphviz_output_format @@ -209,13 +218,6 @@ def render_dot_html(self, node, code, options, prefix='graphviz', self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode - inline = node.get('inline', False) - if inline: - wrapper = 'span' - else: - wrapper = 'p' - - self.body.append(self.starttag(node, wrapper, CLASS='graphviz')) if fname is None: self.body.append(self.encode(code)) else: @@ -243,11 +245,11 @@ def render_dot_html(self, node, code, options, prefix='graphviz', (fname, alt, mapname, imgcss)) self.body.extend([item.decode('utf-8') for item in imgmap]) - self.body.append('\n' % wrapper) raise nodes.SkipNode def html_visit_graphviz(self, node): + warn_for_deprecated_option(self, node) render_dot_html(self, node, node['code'], node['options']) @@ -258,8 +260,8 @@ def render_dot_latex(self, node, code, options, prefix='graphviz'): self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode - inline = node.get('inline', False) - if inline: + is_inline = self.is_inline(node) + if is_inline: para_separator = '' else: para_separator = '\n' @@ -271,6 +273,7 @@ def render_dot_latex(self, node, code, options, prefix='graphviz'): def latex_visit_graphviz(self, node): + warn_for_deprecated_option(self, node) render_dot_latex(self, node, node['code'], node['options']) @@ -286,10 +289,12 @@ def render_dot_texinfo(self, node, code, options, prefix='graphviz'): def texinfo_visit_graphviz(self, node): + warn_for_deprecated_option(self, node) render_dot_texinfo(self, node, node['code'], node['options']) def text_visit_graphviz(self, node): + warn_for_deprecated_option(self, node) if 'alt' in node.attributes: self.add_text(_('[graph: %s]') % node['alt']) else: @@ -298,6 +303,7 @@ def text_visit_graphviz(self, node): def man_visit_graphviz(self, node): + warn_for_deprecated_option(self, node) if 'alt' in node.attributes: self.body.append(_('[graph: %s]') % node['alt']) else: diff --git a/tests/roots/test-ext-graphviz/conf.py b/tests/roots/test-ext-graphviz/conf.py index 6dce9d0c6..6754fbab6 100644 --- a/tests/roots/test-ext-graphviz/conf.py +++ b/tests/roots/test-ext-graphviz/conf.py @@ -2,4 +2,12 @@ extensions = ['sphinx.ext.graphviz'] master_doc = 'index' +<<<<<<< 492f60c37185dccee1e42c01fb5d87ed42f45608 exclude_patterns = ['_build'] +======= + +latex_documents = [ + (master_doc, 'SphinxTests.tex', 'Sphinx Tests Documentation', + 'Georg Brandl', 'manual'), +] +>>>>>>> ``sphinx.ext.graphviz``: show graph image in inline by default diff --git a/tests/roots/test-ext-graphviz/index.rst b/tests/roots/test-ext-graphviz/index.rst index 39c755e9d..62eca8790 100644 --- a/tests/roots/test-ext-graphviz/index.rst +++ b/tests/roots/test-ext-graphviz/index.rst @@ -5,3 +5,9 @@ graphviz :caption: caption of graph bar -> baz + +.. |graph| digraph:: bar + + bar -> baz + +Hello |graph| graphviz world diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index d1e86937c..d39adccd1 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -15,12 +15,31 @@ from util import with_app, SkipTest @with_app('html', testroot='ext-graphviz') -def test_graphviz(app, status, warning): +def test_graphviz_html(app, status, warning): app.builder.build_all() if "dot command 'dot' cannot be run" in warning.getvalue(): raise SkipTest('graphviz "dot" is not available') content = (app.outdir / 'index.html').text() - html = ('

\s*\s*

\s*' - '

caption of graph') + html = ('

\s*\s*

' + 'caption of graph.*

\s*
') assert re.search(html, content, re.S) + + html = 'Hello \n graphviz world' + assert re.search(html, content, re.S) + + +@with_app('latex', testroot='ext-graphviz') +def test_graphviz_latex(app, status, warning): + app.builder.build_all() + if "dot command 'dot' cannot be run" in warning.getvalue(): + raise SkipTest('graphviz "dot" is not available') + + content = (app.outdir / 'SphinxTests.tex').text() + macro = ('\\\\begin{figure}\[htbp\]\n\\\\centering\n\\\\capstart\n\n' + '\\\\includegraphics{graphviz-\w+.pdf}\n' + '\\\\caption{caption of graph}\\\\end{figure}') + assert re.search(macro, content, re.S) + + macro = 'Hello \\\\includegraphics{graphviz-\w+.pdf} graphviz world' + assert re.search(macro, content, re.S)