Skip to content

Commit 8466da0

Browse files
authored
Merge pull request #13138 from larsoner/plot_directive
API: Use class-based directive in sphinxext
2 parents b794828 + 348531d commit 8466da0

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

.flake8

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ per-file-ignores =
4242
matplotlib/projections/geo.py: E203, E221, E502
4343
matplotlib/pylab.py: E501
4444
matplotlib/rcsetup.py: E501
45-
matplotlib/sphinxext/plot_directive.py: E402
4645
matplotlib/tests/test_mathtext.py: E501
4746
matplotlib/transforms.py: E201, E202, E203, E501
4847
matplotlib/tri/triinterpolate.py: E201, E221
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Deprecations
2+
````````````
3+
4+
The ``matplotlib.sphinxext.plot_directive`` interface has changed from
5+
the (Sphinx-)deprecated function-based interface to a class-based interface.
6+
This should not affect end users, but the
7+
``matplotlib.sphinxext.plot_directive.plot_directive`` function is now
8+
deprecated.

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@
148148
import traceback
149149
import warnings
150150

151-
from docutils.parsers.rst import directives
151+
from docutils.parsers.rst import directives, Directive
152152
from docutils.parsers.rst.directives.images import Image
153-
align = Image.align
154153
import jinja2 # Sphinx dependency.
155154

156155
import matplotlib
@@ -165,6 +164,7 @@
165164
else:
166165
import matplotlib.pyplot as plt
167166
from matplotlib import _pylab_helpers, cbook
167+
align = Image.align
168168

169169
__version__ = 2
170170

@@ -174,6 +174,7 @@
174174
# -----------------------------------------------------------------------------
175175

176176

177+
@cbook.deprecated("3.1", alternative="PlotDirective")
177178
def 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+
244275
def 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

Comments
 (0)