Skip to content

Commit

Permalink
BF: plot_directive template to allow class+caption
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobeltran committed Dec 4, 2020
1 parent 8e13d3b commit a801758
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/matplotlib/sphinxext/plot_directive.py
Expand Up @@ -344,7 +344,6 @@ def split_code_at_show(text):
# Template
# -----------------------------------------------------------------------------


TEMPLATE = """
{{ source_code }}
Expand Down Expand Up @@ -379,9 +378,8 @@ def split_code_at_show(text):
`{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
{%- endfor -%}
)
{%- endif -%}
{{ caption }}
{%- endif %}
{{ caption }}
{% endfor %}
.. only:: not html
Expand All @@ -392,7 +390,7 @@ def split_code_at_show(text):
{{ option }}
{% endfor %}
{{ caption }}
{{ caption }}
{% endfor %}
"""
Expand Down Expand Up @@ -632,9 +630,12 @@ def run(arguments, content, options, state_machine, state, lineno):

options.setdefault('include-source', config.plot_include_source)
if 'class' in options:
options['class'] = options['class'] + ' plot-directive'
# classes are parsed into a list of string, and output by simply
# printing the list, abusing the fact that RST guarantees to strip
# non-conforming characters
options['class'] = ['plot-directive'] + options['class']
else:
options.setdefault('class', 'plot-directive')
options.setdefault('class', ['plot-directive'])
keep_context = 'context' in options
context_opt = None if not keep_context else options['context']

Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/tests/test_sphinxext.py
Expand Up @@ -11,7 +11,8 @@
pytest.importorskip('sphinx')


def test_tinypages(tmpdir):
def test_tinypages():
tmpdir = '/tmp/bb'
tmp_path = Path(tmpdir)
html_dir = tmp_path / 'html'
doctree_dir = tmp_path / 'doctrees'
Expand Down Expand Up @@ -57,3 +58,7 @@ def plot_file(num):
assert b'Plot 17 uses the caption option.' in html_contents
# check if figure caption made it into html file
assert b'This is the caption for plot 18.' in html_contents
# check if the custom classes made it into the html file
assert b'plot-directive my-class my-other-class' in html_contents
# check that the multi-image caption is applied twice
assert html_contents.count(b'This caption applies to both plots.') == 2
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/tinypages/some_plots.rst
Expand Up @@ -141,3 +141,23 @@ using the :caption: option:

.. plot:: range4.py
:caption: This is the caption for plot 18.

Plot 19 uses shows that the "plot-directive" class is still appended, even if
we request other custom classes:

.. plot:: range4.py
:class: my-class my-other-class

Should also have a caption.

Plot 20 shows that the default template correctly prints the multi-image
scenario:

.. plot::
:caption: This caption applies to both plots.

plt.figure()
plt.plot(range(6))

plt.figure()
plt.plot(range(4))

0 comments on commit a801758

Please sign in to comment.