From 1d17475a08c70289d4ca575090d0ee3854a53c7b Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 26 May 2016 01:50:21 +0900 Subject: [PATCH] #2575: Now ``sphinx.ext.graphviz`` allows ``:align:`` option --- CHANGES | 1 + doc/ext/graphviz.rst | 4 ++++ sphinx/ext/graphviz.py | 28 +++++++++++++++++++++++++ tests/roots/test-ext-graphviz/index.rst | 6 ++++++ tests/test_ext_graphviz.py | 9 ++++++++ 5 files changed, 48 insertions(+) diff --git a/CHANGES b/CHANGES index 8ae975a50..d3aa3e4a1 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,7 @@ Features added * Convert linkcheck builder to requests for better encoding handling * #2463, #2516: Add keywords of "meta" directive to search index * ``:maxdepth:`` option of toctree affects ``secnumdepth`` (ref: #2547) +* #2575: Now ``sphinx.ext.graphviz`` allows ``:align:`` option Bugs fixed ---------- diff --git a/doc/ext/graphviz.rst b/doc/ext/graphviz.rst index ca34fa281..0994c932a 100644 --- a/doc/ext/graphviz.rst +++ b/doc/ext/graphviz.rst @@ -96,6 +96,10 @@ It adds these directives: All three directives support a ``graphviz_dot`` option that can be switch the ``dot`` command within the directive. +.. versionadded:: 1.5 + All three directives support a ``align`` option to align the graph horizontal. + The values "left", "center", "right" are allowed. + There are also these new config values: .. confval:: graphviz_dot diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index 527cb3704..5e76eb8ba 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -43,6 +43,8 @@ class graphviz(nodes.General, nodes.Inline, nodes.Element): def figure_wrapper(directive, node, caption): figure_node = nodes.figure('', node) + if 'align' in node: + figure_node['align'] = node.attributes.pop('align') parsed = nodes.Element() directive.state.nested_parse(ViewList([caption], source=''), @@ -55,6 +57,10 @@ def figure_wrapper(directive, node, caption): return figure_node +def align_spec(argument): + return directives.choice(argument, ('left', 'center', 'right')) + + class Graphviz(Directive): """ Directive to insert arbitrary dot markup. @@ -65,6 +71,7 @@ class Graphviz(Directive): final_argument_whitespace = False option_spec = { 'alt': directives.unchanged, + 'align': align_spec, 'inline': directives.flag, 'caption': directives.unchanged, 'graphviz_dot': directives.unchanged, @@ -101,6 +108,8 @@ class Graphviz(Directive): node['options']['graphviz_dot'] = self.options['graphviz_dot'] if 'alt' in self.options: node['alt'] = self.options['alt'] + if 'align' in self.options: + node['align'] = self.options['align'] if 'inline' in self.options: node['inline'] = True @@ -121,6 +130,7 @@ class GraphvizSimple(Directive): final_argument_whitespace = False option_spec = { 'alt': directives.unchanged, + 'align': align_spec, 'inline': directives.flag, 'caption': directives.unchanged, 'graphviz_dot': directives.unchanged, @@ -135,6 +145,8 @@ class GraphvizSimple(Directive): node['options']['graphviz_dot'] = self.options['graphviz_dot'] if 'alt' in self.options: node['alt'] = self.options['alt'] + if 'align' in self.options: + node['align'] = self.options['align'] if 'inline' in self.options: node['inline'] = True @@ -236,6 +248,9 @@ def render_dot_html(self, node, code, options, prefix='graphviz',

%s

\n''' % (fname, alt) self.body.append(svgtag) else: + if 'align' in node: + self.body.append('
' % + (node['align'], node['align'])) with open(outfn + '.map', 'rb') as mapfile: imgmap = mapfile.readlines() if len(imgmap) == 2: @@ -248,6 +263,8 @@ def render_dot_html(self, node, code, options, prefix='graphviz', self.body.append('%s\n' % (fname, alt, mapname, imgcss)) self.body.extend([item.decode('utf-8') for item in imgmap]) + if 'align' in node: + self.body.append('
\n') raise nodes.SkipNode @@ -271,8 +288,19 @@ def render_dot_latex(self, node, code, options, prefix='graphviz'): para_separator = '\n' if fname is not None: + post = None + if not is_inline and 'align' in node: + if node['align'] == 'left': + self.body.append('{') + post = '\\hspace*{\\fill}}' + elif node['align'] == 'right': + self.body.append('{\\hspace*{\\fill}') + post = '}' self.body.append('%s\\includegraphics{%s}%s' % (para_separator, fname, para_separator)) + if post: + self.body.append(post) + raise nodes.SkipNode diff --git a/tests/roots/test-ext-graphviz/index.rst b/tests/roots/test-ext-graphviz/index.rst index b778307e3..ab86e2a5a 100644 --- a/tests/roots/test-ext-graphviz/index.rst +++ b/tests/roots/test-ext-graphviz/index.rst @@ -19,3 +19,9 @@ Hello |graph| graphviz world .. graphviz:: graph.dot + +.. digraph:: bar + :align: right + :caption: on right + + foo -> bar diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index dbab33761..aa97f4135 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -55,6 +55,10 @@ def test_graphviz_html(app, status, warning): html = 'digraph {\n  bar -> baz\n}' assert re.search(html, content, re.M) + html = ('
\s*\s*

' + 'on right.*

\s*
') + assert re.search(html, content, re.S) + @with_app('latex', testroot='ext-graphviz') @skip_if_graphviz_not_found @@ -70,6 +74,11 @@ def test_graphviz_latex(app, status, warning): macro = 'Hello \\\\includegraphics{graphviz-\w+.pdf} graphviz world' assert re.search(macro, content, re.S) + macro = ('\\\\begin{wrapfigure}{r}{0pt}\n\\\\centering\n' + '\\\\includegraphics{graphviz-\w+.pdf}\n' + '\\\\caption{on right}\\\\label{.*}\\\\end{wrapfigure}') + assert re.search(macro, content, re.S) + @with_app('html', testroot='ext-graphviz', confoverrides={'language': 'xx'}) @skip_if_graphviz_not_found