Skip to content

Commit 4c1ca7d

Browse files
Delegate file handling to Sphinx using :download:
1 parent 3feaa5d commit 4c1ca7d

1 file changed

Lines changed: 19 additions & 38 deletions

File tree

‎lib/matplotlib/sphinxext/plot_directive.py‎

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,16 @@ def _split_code_at_show(text):
361361
362362
.. only:: html
363363
364-
{% if source_link or (html_show_formats and not multi_image) %}
364+
{% if src_name or (html_show_formats and not multi_image) %}
365365
(
366-
{%- if source_link -%}
367-
`Source code <{{ source_link }}>`__
366+
{%- if src_name -%}
367+
:download:`Source code <{{ build_dir }}/{{ src_name }}>`
368368
{%- endif -%}
369369
{%- if html_show_formats and not multi_image -%}
370370
{%- for img in images -%}
371371
{%- for fmt in img.formats -%}
372-
{%- if source_link or not loop.first -%}, {% endif -%}
373-
`{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
372+
{%- if src_name or not loop.first -%}, {% endif -%}
373+
:download:`{{ fmt }} <{{ build_dir }}/{{ img.basename }}.{{ fmt }}>`
374374
{%- endfor -%}
375375
{%- endfor -%}
376376
{%- endif -%}
@@ -387,7 +387,7 @@ def _split_code_at_show(text):
387387
(
388388
{%- for fmt in img.formats -%}
389389
{%- if not loop.first -%}, {% endif -%}
390-
`{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
390+
:download:`{{ fmt }} <{{ build_dir }}/{{ img.basename }}.{{ fmt }}>`
391391
{%- endfor -%}
392392
)
393393
{%- endif -%}
@@ -747,21 +747,13 @@ def run(arguments, content, options, state_machine, state, lineno):
747747
build_dir = os.path.normpath(build_dir)
748748
os.makedirs(build_dir, exist_ok=True)
749749

750-
# output_dir: final location in the builder's directory
751-
dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir,
752-
source_rel_dir))
753-
os.makedirs(dest_dir, exist_ok=True)
754-
755750
# how to link to files from the RST file
756-
dest_dir_link = os.path.join(relpath(setup.confdir, rst_dir),
757-
source_rel_dir).replace(os.path.sep, '/')
758751
try:
759752
build_dir_link = relpath(build_dir, rst_dir).replace(os.path.sep, '/')
760753
except ValueError:
761754
# on Windows, relpath raises ValueError when path and start are on
762755
# different mounts/drives
763756
build_dir_link = build_dir
764-
source_link = dest_dir_link + '/' + output_base + source_ext
765757

766758
# get list of included rst files so that the output is updated when any
767759
# plots in the included files change. These attributes are modified by the
@@ -782,6 +774,14 @@ def run(arguments, content, options, state_machine, state, lineno):
782774
except ValueError:
783775
pass
784776

777+
# save script (if necessary)
778+
if config.plot_html_show_source_link:
779+
Path(build_dir, output_base + source_ext).write_text(
780+
doctest.script_from_examples(code)
781+
if source_file_name == rst_file and is_doctest
782+
else code,
783+
encoding='utf-8')
784+
785785
# make figures
786786
try:
787787
results = render_figures(code,
@@ -828,18 +828,17 @@ def run(arguments, content, options, state_machine, state, lineno):
828828
':%s: %s' % (key, val) for key, val in options.items()
829829
if key in ('alt', 'height', 'width', 'scale', 'align', 'class')]
830830

831-
# Not-None src_link signals the need for a source link in the generated
832-
# html
831+
# Not-None src_name signals the need for a source download in the
832+
# generated html
833833
if j == 0 and config.plot_html_show_source_link:
834-
src_link = source_link
834+
src_name = output_base + source_ext
835835
else:
836-
src_link = None
836+
src_name = None
837837

838838
result = jinja2.Template(config.plot_template or TEMPLATE).render(
839839
default_fmt=default_fmt,
840-
dest_dir=dest_dir_link,
841840
build_dir=build_dir_link,
842-
source_link=src_link,
841+
src_name=src_name,
843842
multi_image=len(images) > 1,
844843
options=opts,
845844
images=images,
@@ -853,22 +852,4 @@ def run(arguments, content, options, state_machine, state, lineno):
853852
if total_lines:
854853
state_machine.insert_input(total_lines, source=source_file_name)
855854

856-
# copy image files to builder's output directory, if necessary
857-
Path(dest_dir).mkdir(parents=True, exist_ok=True)
858-
859-
for code_piece, images in results:
860-
for img in images:
861-
for fn in img.filenames():
862-
destimg = os.path.join(dest_dir, os.path.basename(fn))
863-
if fn != destimg:
864-
shutil.copyfile(fn, destimg)
865-
866-
# copy script (if necessary)
867-
if config.plot_html_show_source_link:
868-
Path(dest_dir, output_base + source_ext).write_text(
869-
doctest.script_from_examples(code)
870-
if source_file_name == rst_file and is_doctest
871-
else code,
872-
encoding='utf-8')
873-
874855
return errors

0 commit comments

Comments
 (0)