Skip to content

Commit af7d34b

Browse files
committed
BF: plot_directive template to allow class+caption
1 parent 8e13d3b commit af7d34b

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

lib/matplotlib/sphinxext/plot_directive.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ def split_code_at_show(text):
344344
# Template
345345
# -----------------------------------------------------------------------------
346346

347-
348347
TEMPLATE = """
349348
{{ source_code }}
350349
@@ -379,9 +378,8 @@ def split_code_at_show(text):
379378
`{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
380379
{%- endfor -%}
381380
)
382-
{%- endif -%}
383-
384-
{{ caption }}
381+
{%- endif %}
382+
{{ caption }} {# appropriate leading whitespace added beforehand #}
385383
{% endfor %}
386384
387385
.. only:: not html
@@ -392,7 +390,7 @@ def split_code_at_show(text):
392390
{{ option }}
393391
{% endfor %}
394392
395-
{{ caption }}
393+
{{ caption }} {# appropriate leading whitespace added beforehand #}
396394
{% endfor %}
397395
398396
"""
@@ -632,9 +630,12 @@ def run(arguments, content, options, state_machine, state, lineno):
632630

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

lib/matplotlib/tests/test_sphinxext.py

+4
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ def plot_file(num):
5757
assert b'Plot 17 uses the caption option.' in html_contents
5858
# check if figure caption made it into html file
5959
assert b'This is the caption for plot 18.' in html_contents
60+
# check if the custom classes made it into the html file
61+
assert b'plot-directive my-class my-other-class' in html_contents
62+
# check that the multi-image caption is applied twice
63+
assert html_contents.count(b'This caption applies to both plots.') == 2

lib/matplotlib/tests/tinypages/some_plots.rst

+20
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,23 @@ using the :caption: option:
141141

142142
.. plot:: range4.py
143143
:caption: This is the caption for plot 18.
144+
145+
Plot 19 uses shows that the "plot-directive" class is still appended, even if
146+
we request other custom classes:
147+
148+
.. plot:: range4.py
149+
:class: my-class my-other-class
150+
151+
Should also have a caption.
152+
153+
Plot 20 shows that the default template correctly prints the multi-image
154+
scenario:
155+
156+
.. plot::
157+
:caption: This caption applies to both plots.
158+
159+
plt.figure()
160+
plt.plot(range(6))
161+
162+
plt.figure()
163+
plt.plot(range(4))

0 commit comments

Comments
 (0)