156
156
import matplotlib
157
157
from matplotlib .backend_bases import FigureManagerBase
158
158
import matplotlib .pyplot as plt
159
- from matplotlib import _api , _pylab_helpers , cbook
159
+ from matplotlib import _api , _pylab_helpers , cbook , sphinxext
160
160
161
161
matplotlib .use ("agg" )
162
162
align = _api .deprecated (
@@ -254,6 +254,13 @@ def run(self):
254
254
raise self .error (str (e ))
255
255
256
256
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
+
257
264
def setup (app ):
258
265
setup .app = app
259
266
setup .config = app .config
@@ -269,9 +276,9 @@ def setup(app):
269
276
app .add_config_value ('plot_apply_rcparams' , False , True )
270
277
app .add_config_value ('plot_working_directory' , None , True )
271
278
app .add_config_value ('plot_template' , None , True )
272
-
273
279
app .connect ('doctree-read' , mark_plot_labels )
274
-
280
+ app .add_css_file ('plot_directive.css' )
281
+ app .connect ('build-finished' , _copy_css_file )
275
282
metadata = {'parallel_read_safe' : True , 'parallel_write_safe' : True ,
276
283
'version' : matplotlib .__version__ }
277
284
return metadata
@@ -337,7 +344,6 @@ def split_code_at_show(text):
337
344
# Template
338
345
# -----------------------------------------------------------------------------
339
346
340
-
341
347
TEMPLATE = """
342
348
{{ source_code }}
343
349
@@ -374,7 +380,7 @@ def split_code_at_show(text):
374
380
)
375
381
{%- endif -%}
376
382
377
- {{ caption }}
383
+ {{ caption }} {# appropriate leading whitespace added beforehand #}
378
384
{% endfor %}
379
385
380
386
.. only:: not html
@@ -383,9 +389,9 @@ def split_code_at_show(text):
383
389
.. figure:: {{ build_dir }}/{{ img.basename }}.*
384
390
{% for option in options -%}
385
391
{{ option }}
386
- {% endfor %}
392
+ {% endfor - %}
387
393
388
- {{ caption }}
394
+ {{ caption }} {# appropriate leading whitespace added beforehand #}
389
395
{% endfor %}
390
396
391
397
"""
@@ -521,7 +527,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
521
527
"""
522
528
formats = get_plot_formats (config )
523
529
524
- # -- Try to determine if all images already exist
530
+ # Try to determine if all images already exist
525
531
526
532
code_pieces = split_code_at_show (code )
527
533
@@ -624,6 +630,13 @@ def run(arguments, content, options, state_machine, state, lineno):
624
630
default_fmt = formats [0 ][0 ]
625
631
626
632
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' ])
627
640
keep_context = 'context' in options
628
641
context_opt = None if not keep_context else options ['context' ]
629
642
@@ -743,8 +756,8 @@ def run(arguments, content, options, state_machine, state, lineno):
743
756
errors = [sm ]
744
757
745
758
# 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 ' ))
748
761
749
762
# generate output restructuredtext
750
763
total_lines = []
0 commit comments