From 9273140ee257fd754ff036198cd506cd07fb4e4a Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 13 Feb 2016 01:18:39 +0900 Subject: [PATCH] Override figure directive to apply :name: option to the figure itself --- sphinx/directives/__init__.py | 1 + sphinx/directives/patches.py | 33 +++++++++++++++++++++++++++++++++ sphinx/environment.py | 7 +------ sphinx/transforms.py | 3 --- sphinx/util/__init__.py | 4 ---- sphinx/writers/latex.py | 2 ++ tests/test_build_latex.py | 2 +- tests/test_domain_std.py | 23 ----------------------- tests/test_ext_graphviz.py | 2 +- 9 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 sphinx/directives/patches.py diff --git a/sphinx/directives/__init__.py b/sphinx/directives/__init__.py index 36ca3d34d..d99a9bc1b 100644 --- a/sphinx/directives/__init__.py +++ b/sphinx/directives/__init__.py @@ -20,6 +20,7 @@ from sphinx.util.docfields import DocFieldTransformer # import and register directives from sphinx.directives.code import * # noqa from sphinx.directives.other import * # noqa +from sphinx.directives.patches import * # noqa # RE to strip backslash escapes diff --git a/sphinx/directives/patches.py b/sphinx/directives/patches.py new file mode 100644 index 000000000..7e00bc81c --- /dev/null +++ b/sphinx/directives/patches.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +""" + sphinx.directives.patches + ~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from docutils import nodes +from docutils.parsers.rst import directives +from docutils.parsers.rst.directives import images + + +class Figure(images.Figure): + """The figure directive which applies `:name:` option to the figure node + instead of the image node. + """ + + def run(self): + name = self.options.pop('name', None) + (figure_node,) = images.Figure.run(self) + if isinstance(figure_node, nodes.system_message): + return [figure_node] + + if name: + self.options['name'] = name + self.add_name(figure_node) + + return [figure_node] + + +directives.register_directive('figure', Figure) diff --git a/sphinx/environment.py b/sphinx/environment.py index 55d3139a3..931407fd1 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -1725,12 +1725,7 @@ class BuildEnvironment: fignumbers = self.toc_fignumbers[docname].setdefault(figtype, {}) figure_id = fignode['ids'][0] - if (isinstance(fignode, nodes.image) and - isinstance(fignode.parent, nodes.figure) and - fignode.parent['ids']): - fignumbers[figure_id] = fignumbers[fignode.parent['ids'][0]] - else: - fignumbers[figure_id] = get_next_fignumber(figtype, secnum) + fignumbers[figure_id] = get_next_fignumber(figtype, secnum) def _walk_doctree(docname, doctree, secnum): for subnode in doctree.children: diff --git a/sphinx/transforms.py b/sphinx/transforms.py index f8b0bf984..430ba3d94 100644 --- a/sphinx/transforms.py +++ b/sphinx/transforms.py @@ -120,9 +120,6 @@ class AutoNumbering(Transform): if isinstance(node, nodes.figure): if has_child(node, nodes.caption): self.document.note_implicit_target(node) - elif isinstance(node, nodes.image): - if node.parent and has_child(node.parent, nodes.caption): - self.document.note_implicit_target(node.parent) elif isinstance(node, nodes.table): if has_child(node, nodes.title): self.document.note_implicit_target(node) diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index 0d2e70c18..bb13f3fd8 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -522,10 +522,6 @@ def get_figtype(node): from docutils import nodes if isinstance(node, nodes.figure): return 'figure' - elif isinstance(node, nodes.image) and isinstance(node.parent, nodes.figure): - # bare image node is not supported because it doesn't have caption and - # no-caption-target isn't a numbered figure. - return 'figure' elif isinstance(node, nodes.table): return 'table' elif isinstance(node, nodes.container): diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 3510c6358..2b1cf24cb 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1372,6 +1372,8 @@ class LaTeXTranslator(nodes.NodeVisitor): ids = '' for id in self.next_figure_ids: ids += self.hypertarget(id, anchor=False) + if node['ids']: + ids += self.hypertarget(node['ids'][0], anchor=False) self.next_figure_ids.clear() self.restrict_footnote(node) if (len(node.children) and diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index d7d208c58..e73356688 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -425,7 +425,7 @@ def test_reference_in_caption(app, status, warning): '\\label{index:the-section-with-a-reference-to}' '\\footnotetext[4]{\nFootnote in section\n}' in result) assert ('\\caption{This is the figure caption with a footnote to ' - '\\protect\\footnotemark[6].}\end{figure}\n' + '\\protect\\footnotemark[6].}\label{index:id23}\end{figure}\n' '\\footnotetext[6]{\nFootnote in caption\n}')in result assert ('\\caption{footnote \\protect\\footnotemark[7] ' 'in caption of normal table}') in result diff --git a/tests/test_domain_std.py b/tests/test_domain_std.py index 6a6ff5315..2d31ada2e 100644 --- a/tests/test_domain_std.py +++ b/tests/test_domain_std.py @@ -36,29 +36,6 @@ def test_process_doc_handle_figure_caption(): 'testdoc', 'testid', 'caption text') -def test_process_doc_handle_image_parent_figure_caption(): - env = mock.Mock(domaindata={}) - img_node = nodes.image('', alt='image alt') - figure_node = nodes.figure( - '', - nodes.caption('caption text', 'caption text'), - img_node, - ) - document = mock.Mock( - nametypes={'testname': True}, - nameids={'testname': 'testid'}, - ids={'testid': img_node}, - ) - - domain = StandardDomain(env) - if 'testname' in domain.data['labels']: - del domain.data['labels']['testname'] - domain.process_doc(env, 'testdoc', document) - assert 'testname' in domain.data['labels'] - assert domain.data['labels']['testname'] == ( - 'testdoc', 'testid', 'caption text') - - def test_process_doc_handle_table_title(): env = mock.Mock(domaindata={}) table_node = nodes.table( diff --git a/tests/test_ext_graphviz.py b/tests/test_ext_graphviz.py index d39adccd1..95b7f5042 100644 --- a/tests/test_ext_graphviz.py +++ b/tests/test_ext_graphviz.py @@ -38,7 +38,7 @@ def test_graphviz_latex(app, status, warning): content = (app.outdir / 'SphinxTests.tex').text() macro = ('\\\\begin{figure}\[htbp\]\n\\\\centering\n\\\\capstart\n\n' '\\\\includegraphics{graphviz-\w+.pdf}\n' - '\\\\caption{caption of graph}\\\\end{figure}') + '\\\\caption{caption of graph}\\\\label{.*}\\\\end{figure}') assert re.search(macro, content, re.S) macro = 'Hello \\\\includegraphics{graphviz-\w+.pdf} graphviz world'