@@ -369,16 +369,16 @@ def _split_code_at_show(text):
369
369
370
370
.. only:: html
371
371
372
- {% if source_link or (html_show_formats and not multi_image) %}
372
+ {% if src_name or (html_show_formats and not multi_image) %}
373
373
(
374
- {%- if source_link -%}
375
- `Source code <{{ source_link }}>`__
374
+ {%- if src_name -%}
375
+ :download: `Source code <{{ build_dir }}/{{ src_name }}>`
376
376
{%- endif -%}
377
377
{%- if html_show_formats and not multi_image -%}
378
378
{%- for img in images -%}
379
379
{%- for fmt in img.formats -%}
380
- {%- if source_link or not loop.first -%}, {% endif -%}
381
- `{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
380
+ {%- if src_name or not loop.first -%}, {% endif -%}
381
+ :download: `{{ fmt }} <{{ build_dir }}/{{ img.basename }}.{{ fmt }}>`
382
382
{%- endfor -%}
383
383
{%- endfor -%}
384
384
{%- endif -%}
@@ -395,7 +395,7 @@ def _split_code_at_show(text):
395
395
(
396
396
{%- for fmt in img.formats -%}
397
397
{%- if not loop.first -%}, {% endif -%}
398
- `{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
398
+ :download: `{{ fmt }} <{{ build_dir }}/{{ img.basename }}.{{ fmt }}>`
399
399
{%- endfor -%}
400
400
)
401
401
{%- endif -%}
@@ -756,21 +756,13 @@ def run(arguments, content, options, state_machine, state, lineno):
756
756
build_dir = os .path .normpath (build_dir )
757
757
os .makedirs (build_dir , exist_ok = True )
758
758
759
- # output_dir: final location in the builder's directory
760
- dest_dir = os .path .abspath (os .path .join (setup .app .builder .outdir ,
761
- source_rel_dir ))
762
- os .makedirs (dest_dir , exist_ok = True )
763
-
764
759
# how to link to files from the RST file
765
- dest_dir_link = os .path .join (relpath (setup .confdir , rst_dir ),
766
- source_rel_dir ).replace (os .path .sep , '/' )
767
760
try :
768
761
build_dir_link = relpath (build_dir , rst_dir ).replace (os .path .sep , '/' )
769
762
except ValueError :
770
763
# on Windows, relpath raises ValueError when path and start are on
771
764
# different mounts/drives
772
765
build_dir_link = build_dir
773
- source_link = dest_dir_link + '/' + output_base + source_ext
774
766
775
767
# get list of included rst files so that the output is updated when any
776
768
# plots in the included files change. These attributes are modified by the
@@ -791,6 +783,14 @@ def run(arguments, content, options, state_machine, state, lineno):
791
783
except ValueError :
792
784
pass
793
785
786
+ # save script (if necessary)
787
+ if options ['show-source-link' ]:
788
+ Path (build_dir , output_base + source_ext ).write_text (
789
+ doctest .script_from_examples (code )
790
+ if source_file_name == rst_file and is_doctest
791
+ else code ,
792
+ encoding = 'utf-8' )
793
+
794
794
# make figures
795
795
try :
796
796
results = render_figures (code ,
@@ -837,18 +837,17 @@ def run(arguments, content, options, state_machine, state, lineno):
837
837
':%s: %s' % (key , val ) for key , val in options .items ()
838
838
if key in ('alt' , 'height' , 'width' , 'scale' , 'align' , 'class' )]
839
839
840
- # Not-None src_link signals the need for a source link in the generated
841
- # html
840
+ # Not-None src_name signals the need for a source download in the
841
+ # generated html
842
842
if j == 0 and options ['show-source-link' ]:
843
- src_link = source_link
843
+ src_name = output_base + source_ext
844
844
else :
845
- src_link = None
845
+ src_name = None
846
846
847
847
result = jinja2 .Template (config .plot_template or TEMPLATE ).render (
848
848
default_fmt = default_fmt ,
849
- dest_dir = dest_dir_link ,
850
849
build_dir = build_dir_link ,
851
- source_link = src_link ,
850
+ src_name = src_name ,
852
851
multi_image = len (images ) > 1 ,
853
852
options = opts ,
854
853
images = images ,
@@ -862,22 +861,4 @@ def run(arguments, content, options, state_machine, state, lineno):
862
861
if total_lines :
863
862
state_machine .insert_input (total_lines , source = source_file_name )
864
863
865
- # copy image files to builder's output directory, if necessary
866
- Path (dest_dir ).mkdir (parents = True , exist_ok = True )
867
-
868
- for code_piece , images in results :
869
- for img in images :
870
- for fn in img .filenames ():
871
- destimg = os .path .join (dest_dir , os .path .basename (fn ))
872
- if fn != destimg :
873
- shutil .copyfile (fn , destimg )
874
-
875
- # copy script (if necessary)
876
- if options ['show-source-link' ]:
877
- Path (dest_dir , output_base + source_ext ).write_text (
878
- doctest .script_from_examples (code )
879
- if source_file_name == rst_file and is_doctest
880
- else code ,
881
- encoding = 'utf-8' )
882
-
883
864
return errors
0 commit comments