diff --git a/CHANGES b/CHANGES index ca1aef06e42..0312363f722 100644 --- a/CHANGES +++ b/CHANGES @@ -23,6 +23,7 @@ Features added * #6446: duration: Add ``sphinx.ext.durations`` to inspect which documents slow down the build * #6837: LaTeX: Support a nested table +* #6966: graphviz: Support ``:class:`` option Bugs fixed ---------- diff --git a/doc/usage/extensions/graphviz.rst b/doc/usage/extensions/graphviz.rst index dd21b821467..32cfcce306b 100644 --- a/doc/usage/extensions/graphviz.rst +++ b/doc/usage/extensions/graphviz.rst @@ -82,6 +82,13 @@ It adds these directives: .. versionadded:: 1.6 + .. rst:directive:option:: class: class names + :type: a list of class names separeted by spaces + + The class name of the graph. + + .. versionadded:: 2.4 + .. rst:directive:: graph @@ -131,6 +138,13 @@ It adds these directives: .. versionadded:: 1.6 + .. rst:directive:option:: class: class names + :type: a list of class names separeted by spaces + + The class name of the graph. + + .. versionadded:: 2.4 + .. rst:directive:: digraph @@ -176,6 +190,13 @@ It adds these directives: .. versionadded:: 1.6 + .. rst:directive:option:: class: class names + :type: a list of class names separeted by spaces + + The class name of the graph. + + .. versionadded:: 2.4 + There are also these config values: diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index c16952bc072..6ff1193d001 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -121,6 +121,7 @@ class Graphviz(SphinxDirective): 'layout': directives.unchanged, 'graphviz_dot': directives.unchanged, # an old alias of `layout` option 'name': directives.unchanged, + 'class': directives.class_option, } def run(self) -> List[Node]: @@ -158,6 +159,8 @@ def run(self) -> List[Node]: node['alt'] = self.options['alt'] if 'align' in self.options: node['align'] = self.options['align'] + if 'class' in self.options: + node['classes'] = self.options['class'] if 'caption' not in self.options: self.add_name(node) @@ -182,6 +185,7 @@ class GraphvizSimple(SphinxDirective): 'caption': directives.unchanged, 'graphviz_dot': directives.unchanged, 'name': directives.unchanged, + 'class': directives.class_option, } def run(self) -> List[Node]: @@ -195,6 +199,8 @@ def run(self) -> List[Node]: node['alt'] = self.options['alt'] if 'align' in self.options: node['align'] = self.options['align'] + if 'class' in self.options: + node['classes'] = self.options['class'] if 'caption' not in self.options: self.add_name(node) @@ -267,10 +273,8 @@ def render_dot_html(self: HTMLTranslator, node: graphviz, code: str, options: Di logger.warning(__('dot code %r: %s'), code, exc) raise nodes.SkipNode - if imgcls: - imgcls += " graphviz" - else: - imgcls = "graphviz" + classes = [imgcls, 'graphviz'] + node.get('classes', []) + imgcls = ' '.join(filter(None, classes)) if fname is None: self.body.append(self.encode(code)) diff --git a/tests/roots/test-ext-graphviz/index.rst b/tests/roots/test-ext-graphviz/index.rst index e67d1d0822e..e6db9b220ae 100644 --- a/tests/roots/test-ext-graphviz/index.rst +++ b/tests/roots/test-ext-graphviz/index.rst @@ -14,8 +14,9 @@ Hello |graph| graphviz world .. digraph:: foo :graphviz_dot: neato + :class: neato_graph - bar -> baz + baz -> qux .. graphviz:: graph.dot diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index ec905aa5fd4..f1ae8d17e37 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -29,8 +29,9 @@ 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}' - assert re.search(html, content, re.M) + html = ('digraph foo {\nbaz -> qux\n}') + assert re.search(html, content, re.S) html = (r'
\s*' r'
\s*

'