Skip to content

Commit 8e13d3b

Browse files
committed
BUGFIX: naked plot directive naively "responsive"
1 parent f04cf57 commit 8e13d3b

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/matplotlib/sphinxext/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pathlib import Path
2+
3+
_static_path = Path(__file__).resolve().parent / Path('static')

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
import matplotlib
157157
from matplotlib.backend_bases import FigureManagerBase
158158
import matplotlib.pyplot as plt
159-
from matplotlib import _pylab_helpers, cbook
159+
from matplotlib import _pylab_helpers, cbook, sphinxext
160160

161161
matplotlib.use("agg")
162162
align = cbook.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+
257264
def 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
@@ -521,7 +528,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
521528
"""
522529
formats = get_plot_formats(config)
523530

524-
# -- Try to determine if all images already exist
531+
# Try to determine if all images already exist
525532

526533
code_pieces = split_code_at_show(code)
527534

@@ -624,6 +631,10 @@ def run(arguments, content, options, state_machine, state, lineno):
624631
default_fmt = formats[0][0]
625632

626633
options.setdefault('include-source', config.plot_include_source)
634+
if 'class' in options:
635+
options['class'] = options['class'] + ' plot-directive'
636+
else:
637+
options.setdefault('class', 'plot-directive')
627638
keep_context = 'context' in options
628639
context_opt = None if not keep_context else options['context']
629640

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* plot_directive.css
3+
* ~~~~~~~~~~~~
4+
*
5+
* Stylesheet controlling images created using the `plot` directive within
6+
* Sphinx.
7+
*
8+
*/
9+
10+
img.plot-directive {
11+
border: 0;
12+
max-width: 100%;
13+
}

0 commit comments

Comments
 (0)