Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d12f935

Browse files
committed
[2906157] Let sphinx reference matplotlib-created plots (backport)
svn path=/branches/v0_99_maint/; revision=7998
1 parent 015b530 commit d12f935

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def plot_directive(name, arguments, options, content, lineno,
276276
# treated as relative to the root of the documentation tree. We need
277277
# to support both methods here.
278278
tmpdir = os.path.join('build', outdir)
279+
tmpdir = os.path.abspath(tmpdir)
279280
if sphinx_version < (0, 6):
280-
tmpdir = os.path.abspath(tmpdir)
281281
prefix = ''
282282
else:
283283
prefix = '/'
@@ -343,6 +343,36 @@ def plot_directive(name, arguments, options, content, lineno,
343343

344344
return []
345345

346+
def mark_plot_labels(app, document):
347+
"""
348+
To make plots referenceable, we need to move the reference from
349+
the "htmlonly" (or "latexonly") node to the actual figure node
350+
itself.
351+
"""
352+
for name, explicit in document.nametypes.iteritems():
353+
if not explicit:
354+
continue
355+
labelid = document.nameids[name]
356+
if labelid is None:
357+
continue
358+
node = document.ids[labelid]
359+
if node.tagname in ('html_only', 'latex_only'):
360+
for n in node:
361+
if n.tagname == 'figure':
362+
sectname = name
363+
for c in n:
364+
if c.tagname == 'caption':
365+
sectname = c.astext()
366+
break
367+
368+
node['ids'].remove(labelid)
369+
node['names'].remove(name)
370+
n['ids'].append(labelid)
371+
n['names'].append(name)
372+
document.settings.env.labels[name] = \
373+
document.settings.env.docname, labelid, sectname
374+
break
375+
346376
def setup(app):
347377
setup.app = app
348378
setup.config = app.config
@@ -354,3 +384,4 @@ def setup(app):
354384
['png', 'hires.png', 'pdf'],
355385
True)
356386

387+
app.connect('doctree-read', mark_plot_labels)

0 commit comments

Comments
 (0)