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

Skip to content

Commit 39933e4

Browse files
committed
[2906157] Let sphinx reference matplotlib-created plots
svn path=/trunk/matplotlib/; revision=7997
1 parent 0c725cd commit 39933e4

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
308308
# treated as relative to the root of the documentation tree. We
309309
# need to support both methods here.
310310
tmpdir = os.path.join('build', outdir)
311+
tmpdir = os.path.abspath(tmpdir)
311312
if sphinx_version < (0, 6):
312-
tmpdir = os.path.abspath(tmpdir)
313313
prefix = ''
314314
else:
315315
prefix = '/'
@@ -421,6 +421,36 @@ def plot_directive(name, arguments, options, content, lineno,
421421
return _plot_directive(plot_path, 'inline', None, plot_code, '', options,
422422
state_machine)
423423

424+
def mark_plot_labels(app, document):
425+
"""
426+
To make plots referenceable, we need to move the reference from
427+
the "htmlonly" (or "latexonly") node to the actual figure node
428+
itself.
429+
"""
430+
for name, explicit in document.nametypes.iteritems():
431+
if not explicit:
432+
continue
433+
labelid = document.nameids[name]
434+
if labelid is None:
435+
continue
436+
node = document.ids[labelid]
437+
if node.tagname in ('html_only', 'latex_only'):
438+
for n in node:
439+
if n.tagname == 'figure':
440+
sectname = name
441+
for c in n:
442+
if c.tagname == 'caption':
443+
sectname = c.astext()
444+
break
445+
446+
node['ids'].remove(labelid)
447+
node['names'].remove(name)
448+
n['ids'].append(labelid)
449+
n['names'].append(name)
450+
document.settings.env.labels[name] = \
451+
document.settings.env.docname, labelid, sectname
452+
break
453+
424454
def setup(app):
425455
setup.app = app
426456
setup.config = app.config
@@ -441,3 +471,4 @@ def setup(app):
441471
[('png', 80), ('hires.png', 200), ('pdf', 50)],
442472
True)
443473

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

0 commit comments

Comments
 (0)