Skip to content

Commit

Permalink
Close sphinx-doc#6966: graphviz: Support :class: option
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Dec 27, 2019
1 parent 402d011 commit d85d972
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -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
----------
Expand Down
21 changes: 21 additions & 0 deletions doc/usage/extensions/graphviz.rst
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down
12 changes: 8 additions & 4 deletions sphinx/ext/graphviz.py
Expand Up @@ -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]:
Expand Down Expand Up @@ -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)
Expand All @@ -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]:
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion tests/roots/test-ext-graphviz/index.rst
Expand Up @@ -14,8 +14,9 @@ Hello |graph| graphviz world

.. digraph:: foo
:graphviz_dot: neato
:class: neato_graph

bar -> baz
baz -> qux


.. graphviz:: graph.dot
Expand Down
5 changes: 3 additions & 2 deletions tests/test_ext_graphviz.py
Expand Up @@ -29,8 +29,9 @@ def test_graphviz_png_html(app, status, warning):
html = 'Hello <div class="graphviz"><img .*?/></div>\n graphviz world'
assert re.search(html, content, re.S)

html = '<img src=".*?" alt="digraph {\n bar -&gt; baz\n}" class="graphviz" />'
assert re.search(html, content, re.M)
html = ('<img src=".*?" alt="digraph foo {\nbaz -&gt; qux\n}" '
'class="graphviz neato_graph" />')
assert re.search(html, content, re.S)

html = (r'<div class="figure align-right" .*?>\s*'
r'<div class="graphviz"><img .*?/></div>\s*<p class="caption">'
Expand Down

0 comments on commit d85d972

Please sign in to comment.