Skip to content

Commit 64619e5

Browse files
authored
Merge pull request matplotlib#20814 from harupy/save-script-if-necessary
FIX: Avoid copying source script when `plot_html_show_source_link` is False in plot directive
2 parents b071f39 + 515f441 commit 64619e5

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/matplotlib/sphinxext/plot_directive.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -866,10 +866,11 @@ def run(arguments, content, options, state_machine, state, lineno):
866866
shutil.copyfile(fn, destimg)
867867

868868
# copy script (if necessary)
869-
Path(dest_dir, output_base + source_ext).write_text(
870-
doctest.script_from_examples(code)
871-
if source_file_name == rst_file and is_doctest
872-
else code,
873-
encoding='utf-8')
869+
if config.plot_html_show_source_link:
870+
Path(dest_dir, output_base + source_ext).write_text(
871+
doctest.script_from_examples(code)
872+
if source_file_name == rst_file and is_doctest
873+
else code,
874+
encoding='utf-8')
874875

875876
return errors

lib/matplotlib/tests/test_sphinxext.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,35 @@ def plot_directive_file(num):
9797
assert filecmp.cmp(range_6, plot_file(5))
9898

9999

100-
def build_sphinx_html(source_dir, doctree_dir, html_dir):
100+
def test_plot_html_show_source_link(tmpdir):
101+
source_dir = Path(tmpdir) / 'src'
102+
source_dir.mkdir()
103+
parent = Path(__file__).parent
104+
shutil.copyfile(parent / 'tinypages/conf.py', source_dir / 'conf.py')
105+
shutil.copytree(parent / 'tinypages/_static', source_dir / '_static')
106+
doctree_dir = source_dir / 'doctrees'
107+
(source_dir / 'index.rst').write_text("""
108+
.. plot::
109+
110+
plt.plot(range(2))
111+
""")
112+
# Make sure source scripts are created by default
113+
html_dir1 = source_dir / '_build' / 'html1'
114+
build_sphinx_html(source_dir, doctree_dir, html_dir1)
115+
assert "index-1.py" in [p.name for p in html_dir1.iterdir()]
116+
# Make sure source scripts are NOT created when
117+
# plot_html_show_source_link` is False
118+
html_dir2 = source_dir / '_build' / 'html2'
119+
build_sphinx_html(source_dir, doctree_dir, html_dir2,
120+
extra_args=['-D', 'plot_html_show_source_link=0'])
121+
assert "index-1.py" not in [p.name for p in html_dir2.iterdir()]
122+
123+
124+
def build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=None):
101125
# Build the pages with warnings turned into errors
126+
extra_args = [] if extra_args is None else extra_args
102127
cmd = [sys.executable, '-msphinx', '-W', '-b', 'html',
103-
'-d', str(doctree_dir), str(source_dir), str(html_dir)]
128+
'-d', str(doctree_dir), str(source_dir), str(html_dir), *extra_args]
104129
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True,
105130
env={**os.environ, "MPLBACKEND": ""})
106131
out, err = proc.communicate()

0 commit comments

Comments
 (0)