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.py75
1 files changed, 46 insertions, 29 deletions
diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py
index 3970edc07..4e06677ca 100644
--- a/sphinx/ext/graphviz.py
+++ b/sphinx/ext/graphviz.py
@@ -6,7 +6,7 @@
Allow graphviz-formatted graphs to be included in Sphinx-generated
documents inline.
- :copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
+ :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
@@ -25,6 +25,7 @@ from docutils.statemachine import ViewList
import sphinx
from sphinx.errors import SphinxError
from sphinx.locale import _
+from sphinx.util.i18n import search_image_for_language
from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL
from sphinx.util.compat import Directive
@@ -36,7 +37,7 @@ class GraphvizError(SphinxError):
category = 'Graphviz error'
-class graphviz(nodes.General, nodes.Element):
+class graphviz(nodes.General, nodes.Inline, nodes.Element):
pass
@@ -66,6 +67,7 @@ class Graphviz(Directive):
'alt': directives.unchanged,
'inline': directives.flag,
'caption': directives.unchanged,
+ 'graphviz_dot': directives.unchanged,
}
def run(self):
@@ -76,7 +78,8 @@ class Graphviz(Directive):
'Graphviz directive cannot have both content and '
'a filename argument', line=self.lineno)]
env = self.state.document.settings.env
- rel_filename, filename = env.relfn2path(self.arguments[0])
+ argument = search_image_for_language(self.arguments[0], env)
+ rel_filename, filename = env.relfn2path(argument)
env.note_dependency(rel_filename)
try:
fp = codecs.open(filename, 'r', 'utf-8')
@@ -96,13 +99,16 @@ class Graphviz(Directive):
line=self.lineno)]
node = graphviz()
node['code'] = dotcode
- node['options'] = []
+ node['options'] = {}
+ if 'graphviz_dot' in self.options:
+ node['options']['graphviz_dot'] = self.options['graphviz_dot']
if 'alt' in self.options:
node['alt'] = self.options['alt']
- node['inline'] = 'inline' in self.options
+ if 'inline' in self.options:
+ node['inline'] = True
caption = self.options.get('caption')
- if caption and not node['inline']:
+ if caption:
node = figure_wrapper(self, node, caption)
return [node]
@@ -120,19 +126,23 @@ class GraphvizSimple(Directive):
'alt': directives.unchanged,
'inline': directives.flag,
'caption': directives.unchanged,
+ 'graphviz_dot': directives.unchanged,
}
def run(self):
node = graphviz()
node['code'] = '%s %s {\n%s\n}\n' % \
(self.name, self.arguments[0], '\n'.join(self.content))
- node['options'] = []
+ node['options'] = {}
+ if 'graphviz_dot' in self.options:
+ node['options']['graphviz_dot'] = self.options['graphviz_dot']
if 'alt' in self.options:
node['alt'] = self.options['alt']
- node['inline'] = 'inline' in self.options
+ if 'inline' in self.options:
+ node['inline'] = True
caption = self.options.get('caption')
- if caption and not node['inline']:
+ if caption:
node = figure_wrapper(self, node, caption)
return [node]
@@ -140,8 +150,8 @@ class GraphvizSimple(Directive):
def render_dot(self, code, options, format, prefix='graphviz'):
"""Render graphviz code into a PNG or PDF output file."""
- hashkey = (code + str(options) +
- str(self.builder.config.graphviz_dot) +
+ graphviz_dot = options.get('graphviz_dot', self.builder.config.graphviz_dot)
+ hashkey = (code + str(options) + str(graphviz_dot) +
str(self.builder.config.graphviz_dot_args)).encode('utf-8')
fname = '%s-%s.%s' % (prefix, sha1(hashkey).hexdigest(), format)
@@ -151,8 +161,8 @@ def render_dot(self, code, options, format, prefix='graphviz'):
if path.isfile(outfn):
return relfn, outfn
- if hasattr(self.builder, '_graphviz_warned_dot') or \
- hasattr(self.builder, '_graphviz_warned_ps2pdf'):
+ if (hasattr(self.builder, '_graphviz_warned_dot') and
+ self.builder._graphviz_warned_dot.get(graphviz_dot)):
return None, None
ensuredir(path.dirname(outfn))
@@ -161,9 +171,8 @@ def render_dot(self, code, options, format, prefix='graphviz'):
if isinstance(code, text_type):
code = code.encode('utf-8')
- dot_args = [self.builder.config.graphviz_dot]
+ dot_args = [graphviz_dot]
dot_args.extend(self.builder.config.graphviz_dot_args)
- dot_args.extend(options)
dot_args.extend(['-T' + format, '-o' + outfn])
if format == 'png':
dot_args.extend(['-Tcmapx', '-o%s.map' % outfn])
@@ -173,9 +182,10 @@ def render_dot(self, code, options, format, prefix='graphviz'):
if err.errno != ENOENT: # No such file or directory
raise
self.builder.warn('dot command %r cannot be run (needed for graphviz '
- 'output), check the graphviz_dot setting' %
- self.builder.config.graphviz_dot)
- self.builder._graphviz_warned_dot = True
+ 'output), check the graphviz_dot setting' % graphviz_dot)
+ if not hasattr(self.builder, '_graphviz_warned_dot'):
+ self.builder._graphviz_warned_dot = {}
+ self.builder._graphviz_warned_dot[graphviz_dot] = True
return None, None
try:
# Graphviz may close standard input when an error occurs,
@@ -197,6 +207,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 +228,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:
@@ -223,7 +235,8 @@ def render_dot_html(self, node, code, options, prefix='graphviz',
alt = node.get('alt', self.encode(code).strip())
imgcss = imgcls and 'class="%s"' % imgcls or ''
if format == 'svg':
- svgtag = '<img src="%s" alt="%s" %s/>\n' % (fname, alt, imgcss)
+ svgtag = '''<object data="%s" type="image/svg+xml">
+ <p class="warning">%s</p></object>\n''' % (fname, alt)
self.body.append(svgtag)
else:
mapfile = open(outfn + '.map', 'rb')
@@ -242,11 +255,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('</%s>\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'])
@@ -257,8 +270,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'
@@ -270,6 +283,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'])
@@ -285,10 +299,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:
@@ -297,6 +313,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: