Skip to content

Commit 6620135

Browse files
committed
FIX: Refactor
1 parent 8a10364 commit 6620135

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@
148148
import traceback
149149
import warnings
150150

151-
from docutils import nodes
152151
from docutils.parsers.rst import directives, Directive
153152
from docutils.parsers.rst.directives.images import Image
154-
align = Image.align
155153
import jinja2 # Sphinx dependency.
156154

157155
import matplotlib
@@ -166,6 +164,7 @@
166164
else:
167165
import matplotlib.pyplot as plt
168166
from matplotlib import _pylab_helpers, cbook
167+
align = Image.align
169168

170169
__version__ = 2
171170

@@ -175,12 +174,12 @@
175174
# -----------------------------------------------------------------------------
176175

177176

177+
@cbook.deprecated("3.1", alternative="PlotDirective")
178178
def plot_directive(name, arguments, options, content, lineno,
179179
content_offset, block_text, state, state_machine):
180-
"""Deprecated function-based implementation of the ``.. plot::`` directive.
180+
"""Implementation of the ``.. plot::`` directive.
181181
182-
Use PlotDirective instead.
183-
See the module docstring for details on configuration.
182+
See the module docstring for details.
184183
"""
185184
return run(arguments, content, options, state_machine, state, lineno)
186185

@@ -270,17 +269,14 @@ class PlotDirective(Directive):
270269
def run(self):
271270
"""Run the plot directive."""
272271
return run(self.arguments, self.content, self.options,
273-
self.state_machine, self.state, self.lineno,
274-
function=False)
272+
self.state_machine, self.state, self.lineno)
275273

276274

277275
def setup(app):
276+
import matplotlib
278277
setup.app = app
279278
setup.config = app.config
280279
setup.confdir = app.confdir
281-
# Old, function-based method was equivalent to:
282-
# app.add_directive('plot', plot_directive, True, (0, 2, False),
283-
# **PlotDirective.option_spec)
284280
app.add_directive('plot', PlotDirective)
285281
app.add_config_value('plot_pre_code', None, True)
286282
app.add_config_value('plot_include_source', False, True)
@@ -296,7 +292,7 @@ def setup(app):
296292
app.connect('doctree-read', mark_plot_labels)
297293

298294
metadata = {'parallel_read_safe': True, 'parallel_write_safe': True,
299-
'version': 0.2}
295+
'version': matplotlib.__version__}
300296
return metadata
301297

302298

@@ -653,8 +649,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
653649
return results
654650

655651

656-
def run(arguments, content, options, state_machine, state, lineno,
657-
function=True):
652+
def run(arguments, content, options, state_machine, state, lineno):
658653
document = state_machine.document
659654
config = document.settings.env.config
660655
nofigs = 'nofigs' in options
@@ -824,6 +819,9 @@ def run(arguments, content, options, state_machine, state, lineno,
824819
total_lines.extend(result.split("\n"))
825820
total_lines.extend("\n")
826821

822+
if total_lines:
823+
state_machine.insert_input(total_lines, source=source_file_name)
824+
827825
# copy image files to builder's output directory, if necessary
828826
Path(dest_dir).mkdir(parents=True, exist_ok=True)
829827

@@ -839,14 +837,4 @@ def run(arguments, content, options, state_machine, state, lineno,
839837
unescape_doctest(code) if source_file_name == rst_file else code,
840838
encoding='utf-8')
841839

842-
if function:
843-
if total_lines:
844-
state_machine.insert_input(total_lines, source=source_file_name)
845-
out = errors
846-
else:
847-
if len(errors):
848-
out = errors
849-
else:
850-
out = [nodes.raw('\n'.join(total_lines))] if total_lines else []
851-
852-
return out
840+
return errors

0 commit comments

Comments
 (0)