16
16
import shutil
17
17
import subprocess
18
18
import sys
19
+ from urllib .parse import urlsplit , urlunsplit
19
20
import warnings
20
21
21
22
import matplotlib
@@ -248,10 +249,6 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
248
249
except (subprocess .CalledProcessError , FileNotFoundError ):
249
250
SHA = matplotlib .__version__
250
251
251
- html_context = {
252
- "sha" : SHA ,
253
- }
254
-
255
252
project = 'Matplotlib'
256
253
copyright = (
257
254
'2002–2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom '
@@ -312,16 +309,50 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
312
309
313
310
github_project_url = "https://github.com/matplotlib/matplotlib/"
314
311
312
+
315
313
# Options for HTML output
316
314
# -----------------------
317
315
316
+ def add_html_cache_busting (app , pagename , templatename , context , doctree ):
317
+ """
318
+ Add cache busting query on CSS and JavaScript assets.
319
+
320
+ This adds the Matplotlib version as a query to the link reference in the
321
+ HTML, if the path is not absolute (i.e., it comes from the `_static`
322
+ directory) and doesn't already have a query.
323
+ """
324
+ from sphinx .builders .html import Stylesheet , JavaScript
325
+
326
+ css_tag = context ['css_tag' ]
327
+ js_tag = context ['js_tag' ]
328
+
329
+ def css_tag_with_cache_busting (css ):
330
+ if isinstance (css , Stylesheet ) and css .filename is not None :
331
+ url = urlsplit (css .filename )
332
+ if not url .netloc and not url .query :
333
+ url = url ._replace (query = SHA )
334
+ css = Stylesheet (urlunsplit (url ), priority = css .priority ,
335
+ ** css .attributes )
336
+ return css_tag (css )
337
+
338
+ def js_tag_with_cache_busting (js ):
339
+ if isinstance (js , JavaScript ) and js .filename is not None :
340
+ url = urlsplit (js .filename )
341
+ if not url .netloc and not url .query :
342
+ url = url ._replace (query = SHA )
343
+ js = JavaScript (urlunsplit (url ), priority = js .priority ,
344
+ ** js .attributes )
345
+ return js_tag (js )
346
+
347
+ context ['css_tag' ] = css_tag_with_cache_busting
348
+ context ['js_tag' ] = js_tag_with_cache_busting
349
+
350
+
318
351
# The style sheet to use for HTML and HTML Help pages. A file of that name
319
352
# must exist either in Sphinx' static/ path, or in one of the custom paths
320
353
# given in html_static_path.
321
- # html_style = 'matplotlib.css'
322
- # html_style = f"mpl.css?{SHA}"
323
354
html_css_files = [
324
- f "mpl.css? { SHA } " ,
355
+ "mpl.css" ,
325
356
]
326
357
327
358
html_theme = "mpl_sphinx_theme"
@@ -574,14 +605,6 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
574
605
# https://github.com/sphinx-doc/sphinx/issues/3176
575
606
# graphviz_output_format = 'svg'
576
607
577
-
578
- def setup (app ):
579
- if any (st in version for st in ('post' , 'alpha' , 'beta' )):
580
- bld_type = 'dev'
581
- else :
582
- bld_type = 'rel'
583
- app .add_config_value ('releaselevel' , bld_type , 'env' )
584
-
585
608
# -----------------------------------------------------------------------------
586
609
# Source code links
587
610
# -----------------------------------------------------------------------------
@@ -649,3 +672,15 @@ def linkcode_resolve(domain, info):
649
672
f"/{ tag } /lib/{ fn } { linespec } " )
650
673
else :
651
674
extensions .append ('sphinx.ext.viewcode' )
675
+
676
+
677
+ # -----------------------------------------------------------------------------
678
+ # Sphinx setup
679
+ # -----------------------------------------------------------------------------
680
+ def setup (app ):
681
+ if any (st in version for st in ('post' , 'dev' , 'alpha' , 'beta' )):
682
+ bld_type = 'dev'
683
+ else :
684
+ bld_type = 'rel'
685
+ app .add_config_value ('releaselevel' , bld_type , 'env' )
686
+ app .connect ('html-page-context' , add_html_cache_busting , priority = 1000 )
0 commit comments