Add :shows-source-link: option to Sphinx plot directive by lpsinger · Pull Request #24215 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
the ``plot_include_source`` variable in :file:`conf.py` (which itself
defaults to False).

``:show-source-link:`` : bool
Whether to show a link to the source in HTML. The default can be
changed using the ``plot_html_show_source_link`` variable in
:file:`conf.py` (which itself defaults to True).

``:encoding:`` : str
If this source file is in a non-UTF8 or non-ASCII encoding, the
encoding must be specified using the ``:encoding:`` option. The
Expand Down Expand Up @@ -245,6 +250,7 @@ class PlotDirective(Directive):
'align': Image.align,
'class': directives.class_option,
'include-source': _option_boolean,
'show-source-link': _option_boolean,
'format': _option_format,
'context': _option_context,
'nofigs': directives.flag,
Expand Down Expand Up @@ -666,6 +672,7 @@ def run(arguments, content, options, state_machine, state, lineno):
default_fmt = formats[0][0]

options.setdefault('include-source', config.plot_include_source)
options.setdefault('show-source-link', config.plot_html_show_source_link)
if 'class' in options:
# classes are parsed into a list of string, and output by simply
# printing the list, abusing the fact that RST guarantees to strip
Expand Down Expand Up @@ -832,7 +839,7 @@ def run(arguments, content, options, state_machine, state, lineno):

# Not-None src_link signals the need for a source link in the generated
# html
if j == 0 and config.plot_html_show_source_link:
if j == 0 and options['show-source-link']:
src_link = source_link
else:
src_link = None
Expand Down Expand Up @@ -866,7 +873,7 @@ def run(arguments, content, options, state_machine, state, lineno):
shutil.copyfile(fn, destimg)

# copy script (if necessary)
if config.plot_html_show_source_link:
if options['show-source-link']:
Path(dest_dir, output_base + source_ext).write_text(
doctest.script_from_examples(code)
if source_file_name == rst_file and is_doctest
Expand Down
82 changes: 60 additions & 22 deletions lib/matplotlib/tests/test_sphinxext.py