148148import traceback
149149import warnings
150150
151- from docutils .parsers .rst import directives
151+ from docutils .parsers .rst import directives , Directive
152152from docutils .parsers .rst .directives .images import Image
153- align = Image .align
154153import jinja2 # Sphinx dependency.
155154
156155import matplotlib
165164else :
166165 import matplotlib .pyplot as plt
167166from matplotlib import _pylab_helpers , cbook
167+ align = Image .align
168168
169169__version__ = 2
170170
174174# -----------------------------------------------------------------------------
175175
176176
177+ @cbook .deprecated ("3.1" , alternative = "PlotDirective" )
177178def plot_directive (name , arguments , options , content , lineno ,
178179 content_offset , block_text , state , state_machine ):
179180 """Implementation of the ``.. plot::`` directive.
@@ -241,25 +242,42 @@ def mark_plot_labels(app, document):
241242 break
242243
243244
245+ class PlotDirective (Directive ):
246+ """Implementation of the ``.. plot::`` directive.
247+
248+ See the module docstring for details.
249+ """
250+
251+ has_content = True
252+ required_arguments = 0
253+ optional_arguments = 2
254+ final_argument_whitespace = False
255+ option_spec = {
256+ 'alt' : directives .unchanged ,
257+ 'height' : directives .length_or_unitless ,
258+ 'width' : directives .length_or_percentage_or_unitless ,
259+ 'scale' : directives .nonnegative_int ,
260+ 'align' : _option_align ,
261+ 'class' : directives .class_option ,
262+ 'include-source' : _option_boolean ,
263+ 'format' : _option_format ,
264+ 'context' : _option_context ,
265+ 'nofigs' : directives .flag ,
266+ 'encoding' : directives .encoding ,
267+ }
268+
269+ def run (self ):
270+ """Run the plot directive."""
271+ return run (self .arguments , self .content , self .options ,
272+ self .state_machine , self .state , self .lineno )
273+
274+
244275def setup (app ):
276+ import matplotlib
245277 setup .app = app
246278 setup .config = app .config
247279 setup .confdir = app .confdir
248-
249- options = {'alt' : directives .unchanged ,
250- 'height' : directives .length_or_unitless ,
251- 'width' : directives .length_or_percentage_or_unitless ,
252- 'scale' : directives .nonnegative_int ,
253- 'align' : _option_align ,
254- 'class' : directives .class_option ,
255- 'include-source' : _option_boolean ,
256- 'format' : _option_format ,
257- 'context' : _option_context ,
258- 'nofigs' : directives .flag ,
259- 'encoding' : directives .encoding
260- }
261-
262- app .add_directive ('plot' , plot_directive , True , (0 , 2 , False ), ** options )
280+ app .add_directive ('plot' , PlotDirective )
263281 app .add_config_value ('plot_pre_code' , None , True )
264282 app .add_config_value ('plot_include_source' , False , True )
265283 app .add_config_value ('plot_html_show_source_link' , True , True )
@@ -273,7 +291,8 @@ def setup(app):
273291
274292 app .connect ('doctree-read' , mark_plot_labels )
275293
276- metadata = {'parallel_read_safe' : True , 'parallel_write_safe' : True }
294+ metadata = {'parallel_read_safe' : True , 'parallel_write_safe' : True ,
295+ 'version' : matplotlib .__version__ }
277296 return metadata
278297
279298
0 commit comments