Skip to content

Limit copying _sphinx_design_static to HTML based builders #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ python:
- rtd

sphinx:
configuration: docs/conf.py
builder: html
fail_on_warning: true
62 changes: 33 additions & 29 deletions sphinx_design/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,39 @@ def update_css_js(app: Sphinx):
"""Copy the CSS to the build directory."""
# reset changed identifier
app.env.sphinx_design_css_changed = False
# setup up new static path in output dir
static_path = (Path(app.outdir) / "_sphinx_design_static").absolute()
static_existed = static_path.exists()
static_path.mkdir(exist_ok=True)
app.config.html_static_path.append(str(static_path))
# Copy JS to the build directory.
js_path = static_path / "design-tabs.js"
app.add_js_file(js_path.name)
if not js_path.exists():
content = read_text(static_module, "sd_tabs.js")
js_path.write_text(content)
# Read the css content and hash it
content = read_text(static_module, "style.min.css")
# Write the css file
if sphinx_version < (7, 1):
hash = hashlib.md5(content.encode("utf8"), usedforsecurity=False).hexdigest()
css_path = static_path / f"sphinx-design.{hash}.min.css"
else:
# since sphinx 7.1 a checksum is added to the css file URL, so there is no need to do it here
# https://github.com/sphinx-doc/sphinx/pull/11415
css_path = static_path / "sphinx-design.min.css"
app.add_css_file(css_path.name)
if css_path.exists():
return
if static_existed:
app.env.sphinx_design_css_changed = True
for path in static_path.glob("*.css"):
path.unlink()
css_path.write_text(content, encoding="utf8")
# only do this for html builders
if "html" in app.builder.name:
# setup up new static path in output dir
static_path = (Path(app.outdir) / "_sphinx_design_static").absolute()
static_existed = static_path.exists()
static_path.mkdir(exist_ok=True)
app.config.html_static_path.append(str(static_path))
# Copy JS to the build directory.
js_path = static_path / "design-tabs.js"
app.add_js_file(js_path.name)
if not js_path.exists():
content = read_text(static_module, "sd_tabs.js")
js_path.write_text(content)
# Read the css content and hash it
content = read_text(static_module, "style.min.css")
# Write the css file
if sphinx_version < (7, 1):
hash = hashlib.md5(
content.encode("utf8"), usedforsecurity=False
).hexdigest()
css_path = static_path / f"sphinx-design.{hash}.min.css"
else:
# since sphinx 7.1 a checksum is added to the css file URL, so there is no need to do it here
# https://github.com/sphinx-doc/sphinx/pull/11415
css_path = static_path / "sphinx-design.min.css"
app.add_css_file(css_path.name)
if css_path.exists():
return
if static_existed:
app.env.sphinx_design_css_changed = True
for path in static_path.glob("*.css"):
path.unlink()
css_path.write_text(content, encoding="utf8")


def update_css_links(app: Sphinx, env: BuildEnvironment):
Expand Down