|
1 | 1 | from contextlib import contextmanager |
2 | 2 | from functools import partial |
3 | | -import hashlib |
4 | 3 | from pathlib import Path |
5 | 4 |
|
6 | 5 | from docutils import nodes |
7 | 6 | from docutils.parsers.rst import directives |
8 | | -from sphinx import version_info as sphinx_version |
9 | 7 | from sphinx.application import Sphinx |
10 | | -from sphinx.environment import BuildEnvironment |
11 | 8 | from sphinx.transforms import SphinxTransform |
12 | 9 |
|
13 | | -from . import compiled as static_module |
14 | | -from ._compat import findall, read_text |
15 | 10 | from .article_info import setup_article_info |
16 | 11 | from .badges_buttons import setup_badges_and_buttons |
17 | 12 | from .cards import setup_cards |
|
27 | 22 | ) |
28 | 23 | from .tabs import setup_tabs |
29 | 24 |
|
| 25 | +STATIC_DIR = Path(__file__).parent / "static" |
| 26 | + |
30 | 27 |
|
31 | 28 | def setup_extension(app: Sphinx) -> None: |
32 | 29 | """Set up the sphinx extension.""" |
33 | 30 | setup_sd_config(app) |
34 | | - app.connect("builder-inited", update_css_js) |
35 | | - app.connect("env-updated", update_css_links) |
| 31 | + app.connect("builder-inited", add_static_assets) |
36 | 32 | # we override container html visitors, to stop the default behaviour |
37 | 33 | # of adding the `container` class to all nodes.container |
38 | 34 | app.add_node( |
@@ -77,45 +73,13 @@ def _add_directive(name, directive, **kwargs): |
77 | 73 | app.add_directive = add_directive # type: ignore[method-assign] |
78 | 74 |
|
79 | 75 |
|
80 | | -def update_css_js(app: Sphinx): |
81 | | - """Copy the CSS to the build directory.""" |
82 | | - # reset changed identifier |
83 | | - app.env.sphinx_design_css_changed = False # type: ignore[attr-defined] |
84 | | - # setup up new static path in output dir |
85 | | - static_path = (Path(app.outdir) / "_sphinx_design_static").absolute() |
86 | | - static_existed = static_path.exists() |
87 | | - static_path.mkdir(exist_ok=True) |
88 | | - app.config.html_static_path.append(str(static_path)) |
89 | | - # Copy JS to the build directory. |
90 | | - js_path = static_path / "design-tabs.js" |
91 | | - app.add_js_file(js_path.name) |
92 | | - if not js_path.exists(): |
93 | | - content = read_text(static_module, "sd_tabs.js") |
94 | | - js_path.write_text(content) |
95 | | - # Read the css content and hash it |
96 | | - content = read_text(static_module, "style.min.css") |
97 | | - # Write the css file |
98 | | - if sphinx_version < (7, 1): |
99 | | - hash = hashlib.md5(content.encode("utf8"), usedforsecurity=False).hexdigest() |
100 | | - css_path = static_path / f"sphinx-design.{hash}.min.css" |
101 | | - else: |
102 | | - # since sphinx 7.1 a checksum is added to the css file URL, so there is no need to do it here |
103 | | - # https://github.com/sphinx-doc/sphinx/pull/11415 |
104 | | - css_path = static_path / "sphinx-design.min.css" |
105 | | - app.add_css_file(css_path.name) |
106 | | - if css_path.exists(): |
| 76 | +def add_static_assets(app: Sphinx) -> None: |
| 77 | + """Register the extension's static assets (HTML-format builders only).""" |
| 78 | + if app.builder.format != "html": |
107 | 79 | return |
108 | | - if static_existed: |
109 | | - app.env.sphinx_design_css_changed = True # type: ignore[attr-defined] |
110 | | - for path in static_path.glob("*.css"): |
111 | | - path.unlink() |
112 | | - css_path.write_text(content, encoding="utf8") |
113 | | - |
114 | | - |
115 | | -def update_css_links(app: Sphinx, env: BuildEnvironment): |
116 | | - """If CSS has changed, all files must be re-written, to include the correct stylesheets.""" |
117 | | - if env.sphinx_design_css_changed: # type: ignore[attr-defined] |
118 | | - return list(env.all_docs.keys()) |
| 80 | + app.config.html_static_path.append(str(STATIC_DIR)) |
| 81 | + app.add_css_file("sphinx-design.min.css") |
| 82 | + app.add_js_file("design-tabs.js") |
119 | 83 |
|
120 | 84 |
|
121 | 85 | def visit_container(self, node: nodes.Node): |
@@ -175,15 +139,15 @@ class AddFirstTitleCss(SphinxTransform): |
175 | 139 |
|
176 | 140 | def apply(self): |
177 | 141 | hide = False |
178 | | - for docinfo in findall(self.document)(nodes.docinfo): |
179 | | - for name in findall(docinfo)(nodes.field_name): |
| 142 | + for docinfo in self.document.findall(nodes.docinfo): |
| 143 | + for name in docinfo.findall(nodes.field_name): |
180 | 144 | if name.astext() == "sd_hide_title": |
181 | 145 | hide = True |
182 | 146 | break |
183 | 147 | break |
184 | 148 | if not hide: |
185 | 149 | return |
186 | | - for section in findall(self.document)(nodes.section): |
| 150 | + for section in self.document.findall(nodes.section): |
187 | 151 | if isinstance(section.children[0], nodes.title): |
188 | 152 | if "classes" in section.children[0]: |
189 | 153 | section.children[0]["classes"].append("sd-d-none") |
|
0 commit comments