|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import hashlib |
4 | | -from functools import cache |
5 | 3 | from pathlib import Path |
6 | 4 |
|
7 | | -import sphinx.application |
8 | | -from sphinx.builders.html import StandaloneHTMLBuilder |
9 | | - |
10 | 5 | TYPE_CHECKING = False |
11 | 6 | if TYPE_CHECKING: |
12 | | - from typing import Any |
13 | | - |
14 | 7 | from sphinx.application import Sphinx |
15 | 8 | from sphinx.util.typing import ExtensionMetadata |
16 | 9 |
|
|
19 | 12 | THEME_PATH = Path(__file__).resolve().parent |
20 | 13 |
|
21 | 14 |
|
22 | | -@cache |
23 | | -def _asset_hash(path: str) -> str: |
24 | | - """Append a `?digest=` to an url based on the file content.""" |
25 | | - full_path = THEME_PATH / path.replace("_static/", "static/") |
26 | | - digest = hashlib.sha1(full_path.read_bytes()).hexdigest() |
27 | | - |
28 | | - return f"{path}?digest={digest}" |
29 | | - |
30 | | - |
31 | | -def _add_asset_hashes(static: list[str], add_digest_to: list[str]) -> None: |
32 | | - for asset in add_digest_to: |
33 | | - index = static.index(asset) |
34 | | - static[index].filename = _asset_hash(asset) # type: ignore[attr-defined] |
35 | | - |
36 | | - |
37 | | -def _html_page_context( |
38 | | - app: sphinx.application.Sphinx, |
39 | | - pagename: str, |
40 | | - templatename: str, |
41 | | - context: dict[str, Any], |
42 | | - doctree: Any, |
43 | | -) -> None: |
44 | | - if app.config.html_theme != "python_docs_theme": |
45 | | - return |
46 | | - |
47 | | - assert isinstance(app.builder, StandaloneHTMLBuilder) |
48 | | - |
49 | | - if (4,) <= sphinx.version_info < (7, 1) and "css_files" in context: |
50 | | - if "_static/pydoctheme.css" not in context["css_files"]: |
51 | | - raise ValueError( |
52 | | - "This documentation is not using `pydoctheme.css` as the stylesheet. " |
53 | | - "If you have set `html_style` in your conf.py file, remove it." |
54 | | - ) |
55 | | - |
56 | | - _add_asset_hashes( |
57 | | - context["css_files"], |
58 | | - ["_static/pydoctheme.css"], |
59 | | - ) |
60 | | - |
61 | | - |
62 | 15 | def setup(app: Sphinx) -> ExtensionMetadata: |
63 | | - app.require_sphinx("3.4") |
| 16 | + app.require_sphinx("7.3") |
64 | 17 |
|
65 | 18 | app.add_html_theme("python_docs_theme", str(THEME_PATH)) |
66 | 19 |
|
67 | | - app.connect("html-page-context", _html_page_context) |
68 | | - |
69 | 20 | return { |
70 | 21 | "version": __version__, |
71 | 22 | "parallel_read_safe": True, |
|
0 commit comments