sidebar_toggle.py (1861B)
1 """Enable the sidebar workaround only for the affected upstream assets.""" 2 3 import hashlib 4 from importlib import import_module 5 from pathlib import Path 6 7 8 # Exact affected bundles: Book 1.1.3/1.1.4 and PyData 0.16.1, including the 9 # distribution build served by docs.taler.net. An upstream update or 10 # distributor backport changes these hashes and disables BOTH workaround files, 11 # even if the package version stays the same. 12 # https://github.com/executablebooks/sphinx-book-theme/issues/865 13 _AFFECTED_SCRIPTS = { 14 "sphinx_book_theme": ( 15 {"1.1.3", "1.1.4"}, 16 "sphinx-book-theme.js", 17 {"2a8659f18c2652c150f637144de5459d84d6212b1b4b591bf99ba3a6923f08f1"}, 18 ), 19 "pydata_sphinx_theme": ( 20 {"0.16.1"}, 21 "pydata-sphinx-theme.js", 22 { 23 "a4d0b4f65b7650a66f8b1c2fadd150055b26faad9e5aab98fe5fff1c726742eb", 24 "f264bd85cd58f330ce33afffc7839c247a93498680ab7968d7d2d1ddecf955fe", 25 }, 26 ), 27 } 28 29 30 def _add_workaround(app): 31 if app.builder.format != "html" or app.config.html_theme != "sphinx_book_theme": 32 return 33 34 for module_name, (versions, filename, affected_hashes) in _AFFECTED_SCRIPTS.items(): 35 module = import_module(module_name) 36 if getattr(module, "__version__", None) not in versions: 37 return 38 script = ( 39 Path(module.__file__).parent / "theme" / module_name 40 / "static" / "scripts" / filename 41 ) 42 try: 43 digest = hashlib.sha256(script.read_bytes()).hexdigest() 44 except OSError: 45 return 46 if digest not in affected_hashes: 47 return 48 49 app.add_css_file("sidebar-toggle-fix.css") 50 app.add_js_file("sidebar-toggle-fix.js") 51 52 53 def setup(app): 54 app.connect("builder-inited", _add_workaround) 55 return {"parallel_read_safe": True, "parallel_write_safe": True}