diff --git a/CHANGES b/CHANGES index cc3047f48..d537cd232 100644 --- a/CHANGES +++ b/CHANGES @@ -12,6 +12,9 @@ Dependencies Incompatible changes -------------------- +* #5156: the :py:mod:`sphinx.ext.graphviz: extension runs `dot` in the + directory of the document being built instead of in the root directory of + the documentation. * #4460: extensions which stores any data to environment should return the version of its env data structure as metadata. In detail, please see :ref:`ext-metadata`. diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index 11d9e54a0..3c8b3ac50 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -155,7 +155,10 @@ class Graphviz(SphinxDirective): line=self.lineno)] node = graphviz() node['code'] = dotcode - node['options'] = {} + node['options'] = { + 'docname': path.splitext(self.state.document.current_source)[0], + } + if 'graphviz_dot' in self.options: node['options']['graphviz_dot'] = self.options['graphviz_dot'] if 'alt' in self.options: @@ -192,7 +195,9 @@ class GraphvizSimple(SphinxDirective): node = graphviz() node['code'] = '%s %s {\n%s\n}\n' % \ (self.name, self.arguments[0], '\n'.join(self.content)) - node['options'] = {} + node['options'] = { + 'docname': path.splitext(self.state.document.current_source)[0], + } if 'graphviz_dot' in self.options: node['options']['graphviz_dot'] = self.options['graphviz_dot'] if 'alt' in self.options: @@ -235,10 +240,14 @@ def render_dot(self, code, options, format, prefix='graphviz'): dot_args = [graphviz_dot] dot_args.extend(self.builder.config.graphviz_dot_args) dot_args.extend(['-T' + format, '-o' + outfn]) + + docname = options.get('docname', 'index') + cwd = path.dirname(path.join(self.builder.srcdir, docname)) + if format == 'png': dot_args.extend(['-Tcmapx', '-o%s.map' % outfn]) try: - p = Popen(dot_args, stdout=PIPE, stdin=PIPE, stderr=PIPE) + p = Popen(dot_args, stdout=PIPE, stdin=PIPE, stderr=PIPE, cwd=cwd) except OSError as err: if err.errno != ENOENT: # No such file or directory raise