156156import matplotlib
157157from matplotlib .backend_bases import FigureManagerBase
158158import matplotlib .pyplot as plt
159- from matplotlib import _api , _pylab_helpers , cbook
159+ from matplotlib import _api , _pylab_helpers , cbook , sphinxext
160160
161161matplotlib .use ("agg" )
162162align = _api .deprecated (
@@ -254,6 +254,13 @@ def run(self):
254254 raise self .error (str (e ))
255255
256256
257+ def _copy_css_file (app , exc ):
258+ if exc is None and app .builder .format == 'html' :
259+ src = sphinxext ._static_path / Path ('plot_directive.css' )
260+ dst = app .outdir / Path ('_static' )
261+ shutil .copy (src , dst )
262+
263+
257264def setup (app ):
258265 setup .app = app
259266 setup .config = app .config
@@ -269,9 +276,9 @@ def setup(app):
269276 app .add_config_value ('plot_apply_rcparams' , False , True )
270277 app .add_config_value ('plot_working_directory' , None , True )
271278 app .add_config_value ('plot_template' , None , True )
272-
273279 app .connect ('doctree-read' , mark_plot_labels )
274-
280+ app .add_css_file ('plot_directive.css' )
281+ app .connect ('build-finished' , _copy_css_file )
275282 metadata = {'parallel_read_safe' : True , 'parallel_write_safe' : True ,
276283 'version' : matplotlib .__version__ }
277284 return metadata
@@ -337,7 +344,6 @@ def split_code_at_show(text):
337344# Template
338345# -----------------------------------------------------------------------------
339346
340-
341347TEMPLATE = """
342348{{ source_code }}
343349
@@ -374,7 +380,7 @@ def split_code_at_show(text):
374380 )
375381 {%- endif -%}
376382
377- {{ caption }}
383+ {{ caption }} {# appropriate leading whitespace added beforehand #}
378384 {% endfor %}
379385
380386.. only:: not html
@@ -383,9 +389,9 @@ def split_code_at_show(text):
383389 .. figure:: {{ build_dir }}/{{ img.basename }}.*
384390 {% for option in options -%}
385391 {{ option }}
386- {% endfor %}
392+ {% endfor - %}
387393
388- {{ caption }}
394+ {{ caption }} {# appropriate leading whitespace added beforehand #}
389395 {% endfor %}
390396
391397"""
@@ -521,7 +527,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
521527 """
522528 formats = get_plot_formats (config )
523529
524- # -- Try to determine if all images already exist
530+ # Try to determine if all images already exist
525531
526532 code_pieces = split_code_at_show (code )
527533
@@ -624,6 +630,13 @@ def run(arguments, content, options, state_machine, state, lineno):
624630 default_fmt = formats [0 ][0 ]
625631
626632 options .setdefault ('include-source' , config .plot_include_source )
633+ if 'class' in options :
634+ # classes are parsed into a list of string, and output by simply
635+ # printing the list, abusing the fact that RST guarantees to strip
636+ # non-conforming characters
637+ options ['class' ] = ['plot-directive' ] + options ['class' ]
638+ else :
639+ options .setdefault ('class' , ['plot-directive' ])
627640 keep_context = 'context' in options
628641 context_opt = None if not keep_context else options ['context' ]
629642
@@ -743,8 +756,8 @@ def run(arguments, content, options, state_machine, state, lineno):
743756 errors = [sm ]
744757
745758 # Properly indent the caption
746- caption = '\n ' .join (' ' + line .strip ()
747- for line in caption .split ('\n ' ))
759+ caption = '\n ' + ' \n ' .join (' ' + line .strip ()
760+ for line in caption .split ('\n ' ))
748761
749762 # generate output restructuredtext
750763 total_lines = []
0 commit comments