Skip to content

Commit 05d4293

Browse files
committed
Support sphinx-gallery 0.16.0
There was a small change to the `EXAMPLE_HEADER` that we patch, so update that to match the new version. However, since that change is only a single period, I did not bother to keep the old version around. Additionally, sphinx-gallery has changed its recommendations for its configuration to use strings denoting importable callables. As this doesn't work in the older version, this has been made conditional instead of bumping the minimum requirement.
1 parent f00b22d commit 05d4293

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

doc/conf.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from urllib.parse import urlsplit, urlunsplit
2323
import warnings
2424

25+
from packaging.version import parse as parse_version
2526
import sphinx
2627
import yaml
2728

@@ -178,9 +179,21 @@ def _check_dependencies():
178179

179180

180181
# Import only after checking for dependencies.
181-
# gallery_order.py from the sphinxext folder provides the classes that
182-
# allow custom ordering of sections and subsections of the gallery
183-
import sphinxext.gallery_order as gallery_order
182+
183+
import sphinx_gallery
184+
185+
if parse_version(sphinx_gallery.__version__) >= parse_version('0.16.0'):
186+
matplotlib_reduced_latex_scraper = 'sphinxext.matplotlib_reduced_latex_scraper'
187+
reset_modules_basic_units = 'sphinxext.reset_modules_basic_units'
188+
gallery_order_sectionorder = 'sphinxext.gallery_order.sectionorder'
189+
gallery_order_subsectionorder = 'sphinxext.gallery_order.subsectionorder'
190+
else:
191+
# gallery_order.py from the sphinxext folder provides the classes that
192+
# allow custom ordering of sections and subsections of the gallery
193+
from sphinxext.gallery_order import sectionorder as gallery_order_sectionorder
194+
from sphinxext.gallery_order import subsectionorder as gallery_order_subsectionorder
195+
from sphinxext import matplotlib_reduced_latex_scraper
196+
from sphinxext import reset_modules_basic_units
184197

185198
# The following import is only necessary to monkey patch the signature later on
186199
from sphinx_gallery import gen_rst
@@ -230,20 +243,6 @@ def _check_dependencies():
230243

231244
# Sphinx gallery configuration
232245

233-
def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
234-
**kwargs):
235-
"""
236-
Reduce srcset when creating a PDF.
237-
238-
Because sphinx-gallery runs *very* early, we cannot modify this even in the
239-
earliest builder-inited signal. Thus we do it at scraping time.
240-
"""
241-
from sphinx_gallery.scrapers import matplotlib_scraper
242-
243-
if gallery_conf['builder_name'] == 'latex':
244-
gallery_conf['image_srcset'] = []
245-
return matplotlib_scraper(block, block_vars, gallery_conf, **kwargs)
246-
247246
gallery_dirs = [f'{ed}' for ed in
248247
['gallery', 'tutorials', 'plot_types', 'users/explain']
249248
if f'{ed}/*' not in skip_subdirs]
@@ -254,7 +253,7 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
254253
example_dirs += [f'../galleries/{gd}']
255254

256255
sphinx_gallery_conf = {
257-
'backreferences_dir': Path('api') / Path('_as_gen'),
256+
'backreferences_dir': Path('api', '_as_gen'),
258257
# Compression is a significant effort that we skip for local and CI builds.
259258
'compress_images': ('thumbnails', 'images') if is_release_build else (),
260259
'doc_module': ('matplotlib', 'mpl_toolkits'),
@@ -269,14 +268,10 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
269268
'plot_gallery': 'True', # sphinx-gallery/913
270269
'reference_url': {'matplotlib': None},
271270
'remove_config_comments': True,
272-
'reset_modules': (
273-
'matplotlib',
274-
# clear basic_units module to re-register with unit registry on import
275-
lambda gallery_conf, fname: sys.modules.pop('basic_units', None)
276-
),
277-
'subsection_order': gallery_order.sectionorder,
271+
'reset_modules': ('matplotlib', reset_modules_basic_units),
272+
'subsection_order': gallery_order_sectionorder,
278273
'thumbnail_size': (320, 224),
279-
'within_subsection_order': gallery_order.subsectionorder,
274+
'within_subsection_order': gallery_order_subsectionorder,
280275
'capture_repr': (),
281276
'copyfile_regex': r'.*\.rst',
282277
}
@@ -333,7 +328,7 @@ def gallery_image_warning_filter(record):
333328
:class: sphx-glr-download-link-note
334329
335330
:ref:`Go to the end <sphx_glr_download_{1}>`
336-
to download the full example code{2}
331+
to download the full example code.{2}
337332
338333
.. rst-class:: sphx-glr-example-title
339334
@@ -758,7 +753,6 @@ def js_tag_with_cache_busting(js):
758753

759754
if link_github:
760755
import inspect
761-
from packaging.version import parse
762756

763757
extensions.append('sphinx.ext.linkcode')
764758

@@ -814,7 +808,7 @@ def linkcode_resolve(domain, info):
814808
if not fn.startswith(('matplotlib/', 'mpl_toolkits/')):
815809
return None
816810

817-
version = parse(matplotlib.__version__)
811+
version = parse_version(matplotlib.__version__)
818812
tag = 'main' if version.is_devrelease else f'v{version.public}'
819813
return ("https://github.com/matplotlib/matplotlib/blob"
820814
f"/{tag}/lib/{fn}{linespec}")

doc/sphinxext/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
3+
4+
# Sphinx gallery configuration
5+
6+
def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf, **kwargs):
7+
"""
8+
Reduce srcset when creating a PDF.
9+
10+
Because sphinx-gallery runs *very* early, we cannot modify this even in the earliest
11+
builder-inited signal. Thus we do it at scraping time.
12+
"""
13+
from sphinx_gallery.scrapers import matplotlib_scraper
14+
15+
if gallery_conf['builder_name'] == 'latex':
16+
gallery_conf['image_srcset'] = []
17+
return matplotlib_scraper(block, block_vars, gallery_conf, **kwargs)
18+
19+
20+
def reset_modules_basic_units(gallery_conf, fname):
21+
# Clear basic_units module to re-register with unit registry on import
22+
sys.modules.pop('basic_units', None)

lib/matplotlib/tests/test_doc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def test_sphinx_gallery_example_header():
99
EXAMPLE_HEADER, this test will start to fail. In that case, please update
1010
the monkey-patching of EXAMPLE_HEADER in conf.py.
1111
"""
12-
gen_rst = pytest.importorskip('sphinx_gallery.gen_rst')
12+
pytest.importorskip('sphinx_gallery', minversion='0.16.0')
13+
from sphinx_gallery import gen_rst
1314

1415
EXAMPLE_HEADER = """
1516
.. DO NOT EDIT.
@@ -24,7 +25,7 @@ def test_sphinx_gallery_example_header():
2425
:class: sphx-glr-download-link-note
2526
2627
:ref:`Go to the end <sphx_glr_download_{1}>`
27-
to download the full example code{2}
28+
to download the full example code.{2}
2829
2930
.. rst-class:: sphx-glr-example-title
3031

requirements/doc/doc-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pydata-sphinx-theme~=0.15.0
1818
mpl-sphinx-theme~=3.8.0
1919
pyyaml
2020
sphinxcontrib-svg2pdfconverter>=1.1.0
21-
sphinx-gallery>=0.12.0
21+
sphinx-gallery>=0.16.0
2222
sphinx-copybutton
2323
sphinx-design
2424
sphinx-tags>=0.3.0

0 commit comments

Comments
 (0)