summaryrefslogtreecommitdiff
path: root/sphinx/ext/graphviz.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/ext/graphviz.py')
-rw-r--r--sphinx/ext/graphviz.py39
1 files changed, 23 insertions, 16 deletions
diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py
index 08707b733..11d9e54a0 100644
--- a/sphinx/ext/graphviz.py
+++ b/sphinx/ext/graphviz.py
@@ -18,7 +18,7 @@ from os import path
from subprocess import Popen, PIPE
from docutils import nodes
-from docutils.parsers.rst import Directive, directives
+from docutils.parsers.rst import directives
from docutils.statemachine import ViewList
from six import text_type
@@ -26,11 +26,13 @@ import sphinx
from sphinx.errors import SphinxError
from sphinx.locale import _, __
from sphinx.util import logging
+from sphinx.util.docutils import SphinxDirective
from sphinx.util.i18n import search_image_for_language
from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL
if False:
# For type annotation
+ from docutils.parsers.rst import Directive # NOQA
from typing import Any, Dict, List, Tuple # NOQA
from sphinx.application import Sphinx # NOQA
@@ -111,7 +113,7 @@ def align_spec(argument):
return directives.choice(argument, ('left', 'center', 'right'))
-class Graphviz(Directive):
+class Graphviz(SphinxDirective):
"""
Directive to insert arbitrary dot markup.
"""
@@ -135,12 +137,11 @@ class Graphviz(Directive):
return [document.reporter.warning(
__('Graphviz directive cannot have both content and '
'a filename argument'), line=self.lineno)]
- env = self.state.document.settings.env
- argument = search_image_for_language(self.arguments[0], env)
- rel_filename, filename = env.relfn2path(argument)
- env.note_dependency(rel_filename)
+ argument = search_image_for_language(self.arguments[0], self.env)
+ rel_filename, filename = self.env.relfn2path(argument)
+ self.env.note_dependency(rel_filename)
try:
- with codecs.open(filename, 'r', 'utf-8') as fp:
+ with codecs.open(filename, 'r', 'utf-8') as fp: # type: ignore
dotcode = fp.read()
except (IOError, OSError):
return [document.reporter.warning(
@@ -170,7 +171,7 @@ class Graphviz(Directive):
return [node]
-class GraphvizSimple(Directive):
+class GraphvizSimple(SphinxDirective):
"""
Directive to insert arbitrary dot markup.
"""
@@ -277,7 +278,7 @@ def render_dot_html(self, node, code, options, prefix='graphviz',
"'svg', but is %r") % format)
fname, outfn = render_dot(self, code, options, format, prefix)
except GraphvizError as exc:
- logger.warning('dot code %r: ' % code + str(exc))
+ logger.warning(__('dot code %r: %s'), code, text_type(exc))
raise nodes.SkipNode
if fname is None:
@@ -290,21 +291,27 @@ def render_dot_html(self, node, code, options, prefix='graphviz',
self.body.append('<div align="%s" class="align-%s">' %
(node['align'], node['align']))
if format == 'svg':
- svgtag = '''<object data="%s" type="image/svg+xml">
- <p class="warning">%s</p></object>\n''' % (fname, alt)
- self.body.append(svgtag)
+ self.body.append('<div class="graphviz">')
+ self.body.append('<object data="%s" type="image/svg+xml" %s>\n' %
+ (fname, imgcss))
+ self.body.append('<p class="warning">%s</p>' % alt)
+ self.body.append('</object></div>\n')
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('<img src="%s" alt="%s" usemap="#%s" %s/>\n' %
+ self.body.append('<div class="graphviz">')
+ self.body.append('<img src="%s" alt="%s" usemap="#%s" %s/>' %
(fname, alt, imgmap.id, imgcss))
+ self.body.append('</div>\n')
self.body.append(imgmap.generate_clickable_map())
else:
# nothing in image map
- self.body.append('<img src="%s" alt="%s" %s/>\n' %
+ self.body.append('<div class="graphviz">')
+ self.body.append('<img src="%s" alt="%s" %s/>' %
(fname, alt, imgcss))
+ self.body.append('</div>\n')
if 'align' in node:
self.body.append('</div>\n')
@@ -321,7 +328,7 @@ def render_dot_latex(self, node, code, options, prefix='graphviz'):
try:
fname, outfn = render_dot(self, code, options, 'pdf', prefix)
except GraphvizError as exc:
- logger.warning('dot code %r: ' % code + str(exc))
+ logger.warning(__('dot code %r: %s'), code, text_type(exc))
raise nodes.SkipNode
is_inline = self.is_inline(node)
@@ -359,7 +366,7 @@ def render_dot_texinfo(self, node, code, options, prefix='graphviz'):
try:
fname, outfn = render_dot(self, code, options, 'png', prefix)
except GraphvizError as exc:
- logger.warning('dot code %r: ' % code + str(exc))
+ logger.warning(__('dot code %r: %s'), code, text_type(exc))
raise nodes.SkipNode
if fname is not None:
self.body.append('@image{%s,,,[graphviz],png}\n' % fname[:-4])