|
1 | 1 | import datetime |
| 2 | +import subprocess |
| 3 | +from urllib.parse import urlsplit, urlunsplit |
2 | 4 |
|
3 | 5 | # -- Project information ----------------------------------------------------- |
4 | 6 |
|
|
31 | 33 | # This pattern also affects html_static_path and html_extra_path. |
32 | 34 | exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] |
33 | 35 |
|
| 36 | +# General substitutions. |
| 37 | +try: |
| 38 | + SHA = subprocess.check_output( |
| 39 | + ['git', 'rev-parse', 'HEAD']).decode('utf-8').strip() |
| 40 | +# Catch the case where git is not installed locally, and use the setuptools_scm |
| 41 | +# version number instead. |
| 42 | +except (subprocess.CalledProcessError, FileNotFoundError): |
| 43 | + import hashlib |
| 44 | + import matplotlib |
| 45 | + import mpl_sphinx_theme |
| 46 | + import pydata_sphinx_theme |
| 47 | + SHA = hashlib.sha256( |
| 48 | + (f'{matplotlib.__version__} ' |
| 49 | + f'{mpl_sphinx_theme.__version__} ' |
| 50 | + f'{pydata_sphinx_theme.__version__}').encode('utf-8')).hexdigest() |
| 51 | +SHA = SHA[:20] |
| 52 | + |
| 53 | + |
34 | 54 | # -- Options for HTML output ------------------------------------------------- |
35 | 55 |
|
| 56 | +def add_html_cache_busting(app, pagename, templatename, context, doctree): |
| 57 | + """ |
| 58 | + Add cache busting query on CSS and JavaScript assets. |
| 59 | +
|
| 60 | + This adds the Matplotlib version as a query to the link reference in the |
| 61 | + HTML, if the path is not absolute (i.e., it comes from the `_static` |
| 62 | + directory) and doesn't already have a query. |
| 63 | + """ |
| 64 | + from sphinx.builders.html import Stylesheet, JavaScript |
| 65 | + |
| 66 | + css_tag = context['css_tag'] |
| 67 | + js_tag = context['js_tag'] |
| 68 | + |
| 69 | + def css_tag_with_cache_busting(css): |
| 70 | + if isinstance(css, Stylesheet) and css.filename is not None: |
| 71 | + url = urlsplit(css.filename) |
| 72 | + if not url.netloc and not url.query: |
| 73 | + url = url._replace(query=SHA) |
| 74 | + css = Stylesheet(urlunsplit(url), priority=css.priority, |
| 75 | + **css.attributes) |
| 76 | + return css_tag(css) |
| 77 | + |
| 78 | + def js_tag_with_cache_busting(js): |
| 79 | + if isinstance(js, JavaScript) and js.filename is not None: |
| 80 | + url = urlsplit(js.filename) |
| 81 | + if not url.netloc and not url.query: |
| 82 | + url = url._replace(query=SHA) |
| 83 | + js = JavaScript(urlunsplit(url), priority=js.priority, |
| 84 | + **js.attributes) |
| 85 | + return js_tag(js) |
| 86 | + |
| 87 | + context['css_tag'] = css_tag_with_cache_busting |
| 88 | + context['js_tag'] = js_tag_with_cache_busting |
| 89 | + |
| 90 | + |
36 | 91 | html_css_files = ['css/normalize.css', 'css/landing.css'] |
37 | 92 | html_theme = "mpl_sphinx_theme" |
38 | 93 | html_favicon = "_static/favicon.ico" |
|
54 | 109 |
|
55 | 110 | # Prefix added to all the URLs generated in the 404 page. |
56 | 111 | notfound_urls_prefix = '/' |
| 112 | + |
| 113 | + |
| 114 | +# ----------------------------------------------------------------------------- |
| 115 | +# Sphinx setup |
| 116 | +# ----------------------------------------------------------------------------- |
| 117 | +def setup(app): |
| 118 | + app.connect('html-page-context', add_html_cache_busting, priority=1000) |
0 commit comments