Skip to content

Commit

Permalink
BUGFIX: naked plot directive naively "responsive"
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobeltran committed Dec 2, 2020
1 parent f04cf57 commit 8e13d3b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/matplotlib/sphinxext/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from pathlib import Path

_static_path = Path(__file__).resolve().parent / Path('static')
19 changes: 15 additions & 4 deletions lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
import matplotlib
from matplotlib.backend_bases import FigureManagerBase
import matplotlib.pyplot as plt
from matplotlib import _pylab_helpers, cbook
from matplotlib import _pylab_helpers, cbook, sphinxext

matplotlib.use("agg")
align = cbook.deprecated(
Expand Down Expand Up @@ -254,6 +254,13 @@ def run(self):
raise self.error(str(e))


def _copy_css_file(app, exc):
if exc is None and app.builder.format == 'html':
src = sphinxext._static_path / Path('plot_directive.css')
dst = app.outdir / Path('_static')
shutil.copy(src, dst)


def setup(app):
setup.app = app
setup.config = app.config
Expand All @@ -269,9 +276,9 @@ def setup(app):
app.add_config_value('plot_apply_rcparams', False, True)
app.add_config_value('plot_working_directory', None, True)
app.add_config_value('plot_template', None, True)

app.connect('doctree-read', mark_plot_labels)

app.add_css_file('plot_directive.css')
app.connect('build-finished', _copy_css_file)
metadata = {'parallel_read_safe': True, 'parallel_write_safe': True,
'version': matplotlib.__version__}
return metadata
Expand Down Expand Up @@ -521,7 +528,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
"""
formats = get_plot_formats(config)

# -- Try to determine if all images already exist
# Try to determine if all images already exist

code_pieces = split_code_at_show(code)

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

options.setdefault('include-source', config.plot_include_source)
if 'class' in options:
options['class'] = options['class'] + ' plot-directive'
else:
options.setdefault('class', 'plot-directive')
keep_context = 'context' in options
context_opt = None if not keep_context else options['context']

Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/sphinxext/static/plot_directive.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* plot_directive.css
* ~~~~~~~~~~~~
*
* Stylesheet controlling images created using the `plot` directive within
* Sphinx.
*
*/

img.plot-directive {
border: 0;
max-width: 100%;
}

0 comments on commit 8e13d3b

Please sign in to comment.