8000 Fix the source link when using a custom basename to include the .py e… · matplotlib/matplotlib@550e382 · GitHub
[go: up one dir, main page]

Skip to content

Commit 550e382

Browse files
committed
Fix the source link when using a custom basename to include the .py extension
1 parent 7f56c94 commit 550e382

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ def run(arguments, content, options, state_machine, state, lineno):
910910

911911
# save script (if necessary)
912912
if options['show-source-link']:
913-
Path(build_dir, output_base + source_ext).write_text(
913+
Path(build_dir, output_base + (source_ext or '.py')).write_text(
914914
doctest.script_from_examples(code)
915915
if source_file_name == rst_file and is_doctest
916916
else code,
@@ -970,7 +970,7 @@ def run(arguments, content, options, state_machine, state, lineno):
970970
# Not-None src_name signals the need for a source download in the
971971
# generated html
972972
if j == 0 and options['show-source-link']:
973-
src_name = output_base + source_ext
973+
src_name = output_base + (source_ext or '.py')
974974
else:
975975
src_name = None
976976
if config.plot_srcset:

lib/matplotlib/tests/test_sphinxext.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,28 @@ def test_show_source_link_false(tmp_path, plot_html_show_source_link):
181181
assert len(list(html_dir.glob("**/index-1.py"))) == 0
182182

183183

184+
def test_plot_html_show_source_link_custom_basename(tmp_path):
185+
# Test that source link filename includes .py extension when using custom basename
186+
shutil.copyfile(tinypages / 'conf.py', tmp_path / 'conf.py')
187+
shutil.copytree(tinypages / '_static', tmp_path / '_static')
188+
doctree_dir = tmp_path / 'doctrees'
189+
(tmp_path / 'index.rst').write_text("""
190+
.. plot::
191+
:image-basename: custom-name
192+
193+
plt.plot(range(2))
194+
""")
195+
html_dir = tmp_path / '_build' / 'html'
196+
build_sphinx_html(tmp_path, doctree_dir, html_dir)
197+
198+
# Check that source file with .py extension is generated
199+
assert len(list(html_dir.glob("**/custom-name.py"))) == 1
200+
201+
# Check that the HTML contains the correct link with .py extension
202+
html_content = (html_dir / 'index.html').read_text()
203+
assert 'custom-name.py' in html_content
204+
205+
184206
def test_srcset_version(tmp_path):
185207
html_dir = tmp_path / '_build' / 'html'
186208
img_dir = html_dir / '_images'

0 commit comments

Comments
 (0)
0