Override figure directive to apply :name: option to the figure itself

This commit is contained in:
Takeshi KOMIYA 2016-02-13 01:18:39 +09:00
parent b07f4ccf10
commit 9273140ee2
9 changed files with 39 additions and 38 deletions

View File

@ -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

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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'