Skip to content

Commit d9623d6

Browse files
authored
Merge pull request #791 from executablebooks/agoose77/fix-sphinx-726-warning
FIX: properly handle CSS objects in css_files
2 parents 29a73f6 + ecacc0f commit d9623d6

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

.github/workflows/tests.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ jobs:
8484
python-version: '3.9'
8585
cache: "pip"
8686
cache-dependency-path: "pyproject.toml"
87+
- name: Install fonts
88+
# This is required until sphinx-opengraph fixes their fallback
89+
run: sudo apt-get install -y fonts-roboto
90+
8791
- name: Install dependencies
8892
run: |
8993
python -m pip install --upgrade pip
@@ -103,11 +107,12 @@ jobs:
103107
shell: python
104108
run: |
105109
from pathlib import Path
110+
import re
106111
text = Path("./warnings.txt").read_text().strip()
107-
expected_warning_snippets = ["kitchen-sink", "urllib/parse.py"]
112+
expected_warning_patterns = [r"kitchen\-sink", r"urllib/parse\.py", r"Glyph 10024 .*? missing from current font"]
108113
print("\n=== Sphinx Warnings ===\n\n" + text) # Print just for reference so we can look at the logs
109-
unexpected = [ii for ii in text.split("\n") if not any(snippet in ii for snippet in expected_warning_snippets)]
110-
assert len(unexpected) == 0
114+
unexpected = [l for l in text.splitlines() if not any(re.search(p, ii) for p in expected_warning_patterns)]
115+
assert len(unexpected) == 0, unexpected
111116
112117
- name: Audit with Lighthouse
113118
uses: treosh/[email protected]

src/sphinx_book_theme/__init__.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _gen_hash(path: str) -> str:
7777
return hashlib.sha1(path.read_bytes()).hexdigest()
7878

7979

80-
def hash_assets_for_files(assets: list, theme_static: Path, context):
80+
def hash_assets_for_files(assets: list, theme_static: Path, context, app):
8181
"""Generate a hash for assets, and append to its entry in context.
8282
8383
assets: a list of assets to hash, each path should be relative to
@@ -88,22 +88,36 @@ def hash_assets_for_files(assets: list, theme_static: Path, context):
8888
context: the Sphinx context object where asset links are stored. These are:
8989
`css_files` and `script_files` keys.
9090
"""
91-
for asset in assets:
91+
for asset_path in assets:
9292
# CSS assets are stored in css_files, JS assets in script_files
93-
asset_type = "css_files" if asset.endswith(".css") else "script_files"
93+
asset_type = "css_files" if asset_path.endswith(".css") else "script_files"
9494
if asset_type in context:
9595
# Define paths to the original asset file, and its linked file in Sphinx
96-
asset_sphinx_link = f"_static/{asset}"
97-
asset_source_path = theme_static / asset
96+
asset_sphinx_link = f"_static/{asset_path}"
97+
asset_source_path = theme_static / asset_path
9898
if not asset_source_path.exists():
9999
SPHINX_LOGGER.warning(
100100
f"Asset {asset_source_path} does not exist, not linking."
101101
)
102102
# Find this asset in context, and update it to include the digest
103-
if asset_sphinx_link in context[asset_type]:
104-
hash = _gen_hash(asset_source_path)
105-
ix = context[asset_type].index(asset_sphinx_link)
106-
context[asset_type][ix] = asset_sphinx_link + "?digest=" + hash
103+
for ii, other_asset in enumerate(context[asset_type]):
104+
# TODO: eventually the contents of context['css_files'] etc should probably
105+
# only be _CascadingStyleSheet etc. For now, assume mixed with strings.
106+
if (
107+
getattr(other_asset, "filename", str(other_asset))
108+
!= asset_sphinx_link
109+
):
110+
continue
111+
# Take priority from existing asset or use default priority (500)
112+
priority = getattr(other_asset, "priority", 500)
113+
# Remove existing asset
114+
del context[asset_type][ii]
115+
# Add new asset
116+
app.add_css_file(
117+
asset_sphinx_link,
118+
digest=_gen_hash(asset_source_path),
119+
priority=priority,
120+
)
107121

108122

109123
def hash_html_assets(app, pagename, templatename, context, doctree):
@@ -117,7 +131,7 @@ def hash_html_assets(app, pagename, templatename, context, doctree):
117131
# run but the book theme CSS file won't be linked in Sphinx.
118132
if app.config.html_theme == "sphinx_book_theme":
119133
assets.append("styles/sphinx-book-theme.css")
120-
hash_assets_for_files(assets, get_html_theme_path() / "static", context)
134+
hash_assets_for_files(assets, get_html_theme_path() / "static", context, app)
121135

122136

123137
def update_mode_thebe_config(app):

0 commit comments

Comments
 (0)