#443: Allow referencing external graphviz files.

This commit is contained in:
Georg Brandl
2010-08-23 13:07:28 +00:00
parent 398fb3f00c
commit 9c2747da91
3 changed files with 45 additions and 6 deletions

View File

@@ -3,6 +3,13 @@ Release 1.1 (in development)
* Added Python 3.x support.
* Added i18n support for content, a ``gettext`` builder and
related utilities.
* Added the ``websupport`` library.
* #443: Allow referencing external graphviz files.
Release 1.0.2 (Aug 14, 2010)
============================

View File

@@ -29,6 +29,17 @@ It adds these directives:
:confval:`graphviz_output_format`). In LaTeX output, the code will be
rendered to an embeddable PDF file.
You can also embed external dot files, by giving the file name as an
argument to :rst:dir:`graphviz` and no additional content::
.. graphviz:: external.dot
As for all file references in Sphinx, if the filename is absolute, it is
taken as relative to the source directory.
.. versionchanged:: 1.1
Added support for external files.
.. rst:directive:: graph

View File

@@ -11,6 +11,7 @@
"""
import re
import codecs
import posixpath
from os import path
from math import ceil
@@ -46,18 +47,38 @@ class Graphviz(Directive):
"""
has_content = True
required_arguments = 0
optional_arguments = 0
optional_arguments = 1
final_argument_whitespace = False
option_spec = {
'alt': directives.unchanged,
}
def run(self):
dotcode = '\n'.join(self.content)
if not dotcode.strip():
return [self.state_machine.reporter.warning(
'Ignoring "graphviz" directive without content.',
line=self.lineno)]
if self.arguments:
document = self.state.document
if self.content:
return [document.reporter.warning(
'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])
env.note_dependency(rel_filename)
try:
fp = codecs.open(filename, 'r', 'utf-8')
try:
dotcode = fp.read()
finally:
fp.close()
except (IOError, OSError):
return [document.reporter.warning(
'External Graphviz file %r not found or reading '
'it failed' % filename, line=self.lineno)]
else:
dotcode = '\n'.join(self.content)
if not dotcode.strip():
return [self.state_machine.reporter.warning(
'Ignoring "graphviz" directive without content.',
line=self.lineno)]
node = graphviz()
node['code'] = dotcode
node['options'] = []