From 5e38c7b1965a869f1924ebfe05ba858cc16320b2 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 12 Dec 2023 16:56:51 +0000 Subject: [PATCH 1/6] FIX: properly handle CSS objects in css_files --- src/sphinx_book_theme/__init__.py | 34 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/sphinx_book_theme/__init__.py b/src/sphinx_book_theme/__init__.py index 722c4836..c510877a 100644 --- a/src/sphinx_book_theme/__init__.py +++ b/src/sphinx_book_theme/__init__.py @@ -77,7 +77,7 @@ def _gen_hash(path: str) -> str: return hashlib.sha1(path.read_bytes()).hexdigest() -def hash_assets_for_files(assets: list, theme_static: Path, context): +def hash_assets_for_files(assets: list, theme_static: Path, context, app): """Generate a hash for assets, and append to its entry in context. 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): context: the Sphinx context object where asset links are stored. These are: `css_files` and `script_files` keys. """ - for asset in assets: + for asset_path in assets: # CSS assets are stored in css_files, JS assets in script_files - asset_type = "css_files" if asset.endswith(".css") else "script_files" + asset_type = "css_files" if asset_path.endswith(".css") else "script_files" if asset_type in context: # Define paths to the original asset file, and its linked file in Sphinx - asset_sphinx_link = f"_static/{asset}" - asset_source_path = theme_static / asset + asset_sphinx_link = f"_static/{asset_path}" + asset_source_path = theme_static / asset_path if not asset_source_path.exists(): SPHINX_LOGGER.warning( f"Asset {asset_source_path} does not exist, not linking." ) # Find this asset in context, and update it to include the digest - if asset_sphinx_link in context[asset_type]: - hash = _gen_hash(asset_source_path) - ix = context[asset_type].index(asset_sphinx_link) - context[asset_type][ix] = asset_sphinx_link + "?digest=" + hash + for i, other_asset in enumerate(context[asset_type]): + # TODO: eventually the contents of context['css_files'] etc should probably + # only be _CascadingStyleSheet etc. For now, assume mixed with strings. + if ( + getattr(other_asset, "filename", str(other_asset)) + != asset_sphinx_link + ): + continue + # Take priority from existing asset or use default priority (500) + priority = getattr(other_asset, "priority", 500) + # Remove existing asset + del context[asset_type][i] + # Add new asset + app.add_css_file( + asset_sphinx_link, + digest=_gen_hash(asset_source_path), + priority=priority, + ) def hash_html_assets(app, pagename, templatename, context, doctree): @@ -117,7 +131,7 @@ def hash_html_assets(app, pagename, templatename, context, doctree): # run but the book theme CSS file won't be linked in Sphinx. if app.config.html_theme == "sphinx_book_theme": assets.append("styles/sphinx-book-theme.css") - hash_assets_for_files(assets, get_html_theme_path() / "static", context) + hash_assets_for_files(assets, get_html_theme_path() / "static", context, app) def update_mode_thebe_config(app): From 5c7dc45c4aaba4824bc33b1d6b2f84025e4f06ed Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 12 Dec 2023 21:30:55 +0000 Subject: [PATCH 2/6] Update src/sphinx_book_theme/__init__.py --- src/sphinx_book_theme/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sphinx_book_theme/__init__.py b/src/sphinx_book_theme/__init__.py index c510877a..a41eeb39 100644 --- a/src/sphinx_book_theme/__init__.py +++ b/src/sphinx_book_theme/__init__.py @@ -100,7 +100,7 @@ def hash_assets_for_files(assets: list, theme_static: Path, context, app): f"Asset {asset_source_path} does not exist, not linking." ) # Find this asset in context, and update it to include the digest - for i, other_asset in enumerate(context[asset_type]): + for ii, other_asset in enumerate(context[asset_type]): # TODO: eventually the contents of context['css_files'] etc should probably # only be _CascadingStyleSheet etc. For now, assume mixed with strings. if ( @@ -111,7 +111,7 @@ def hash_assets_for_files(assets: list, theme_static: Path, context, app): # Take priority from existing asset or use default priority (500) priority = getattr(other_asset, "priority", 500) # Remove existing asset - del context[asset_type][i] + del context[asset_type][ii] # Add new asset app.add_css_file( asset_sphinx_link, From bcfae9e85570c91a909fc06e5701005debd1bf86 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 13 Dec 2023 10:52:19 +0000 Subject: [PATCH 3/6] MAINT: install roboto --- .github/workflows/tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 59411c3d..80d4c1ad 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -84,6 +84,9 @@ jobs: python-version: '3.9' cache: "pip" cache-dependency-path: "pyproject.toml" + - name: Install fonts + run: sudo apt-get install -y fonts-roboto + - name: Install dependencies run: | python -m pip install --upgrade pip From 7a146b80963e9874df7edc28c498c1c894954711 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Thu, 14 Dec 2023 12:42:13 +0000 Subject: [PATCH 4/6] DOCS: ignore warning --- docs/conf.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index c050d7d2..a12d05f4 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,6 +2,7 @@ import os from urllib.request import urlopen from pathlib import Path +import warnings project = "Sphinx Book Theme" copyright = "2023" @@ -29,12 +30,17 @@ "sphinx_tabs.tabs", "sphinx_thebe", "sphinx_togglebutton", - "sphinxcontrib.bibtex", + # "sphinxcontrib.bibtex", "sphinxext.opengraph", # For the kitchen sink "sphinx.ext.todo", ] +# Ignore Glyph 10024 (\N{SPARKLES}) missing from current font from "sphinxext.opengraph" +warnings.filterwarnings( + "ignore", "Glyph 10024 .*? missing from current font", UserWarning +) + # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] From afddf2608abf462be0596df65d68d0741b711eba Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Thu, 14 Dec 2023 12:54:23 +0000 Subject: [PATCH 5/6] DOCS: restore commented extension --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index a12d05f4..891ebfa2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -30,7 +30,7 @@ "sphinx_tabs.tabs", "sphinx_thebe", "sphinx_togglebutton", - # "sphinxcontrib.bibtex", + "sphinxcontrib.bibtex", "sphinxext.opengraph", # For the kitchen sink "sphinx.ext.todo", From ecacc0f5447a0af9af20152f5f77608bad5f8ddb Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Thu, 14 Dec 2023 13:18:58 +0000 Subject: [PATCH 6/6] MAINT: move pattern matching --- .github/workflows/tests.yml | 8 +++++--- docs/conf.py | 6 ------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 80d4c1ad..353f1bc3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -85,6 +85,7 @@ jobs: cache: "pip" cache-dependency-path: "pyproject.toml" - name: Install fonts + # This is required until sphinx-opengraph fixes their fallback run: sudo apt-get install -y fonts-roboto - name: Install dependencies @@ -106,11 +107,12 @@ jobs: shell: python run: | from pathlib import Path + import re text = Path("./warnings.txt").read_text().strip() - expected_warning_snippets = ["kitchen-sink", "urllib/parse.py"] + expected_warning_patterns = [r"kitchen\-sink", r"urllib/parse\.py", r"Glyph 10024 .*? missing from current font"] print("\n=== Sphinx Warnings ===\n\n" + text) # Print just for reference so we can look at the logs - unexpected = [ii for ii in text.split("\n") if not any(snippet in ii for snippet in expected_warning_snippets)] - assert len(unexpected) == 0 + unexpected = [l for l in text.splitlines() if not any(re.search(p, ii) for p in expected_warning_patterns)] + assert len(unexpected) == 0, unexpected - name: Audit with Lighthouse uses: treosh/lighthouse-ci-action@10.1.0 diff --git a/docs/conf.py b/docs/conf.py index 891ebfa2..c050d7d2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,7 +2,6 @@ import os from urllib.request import urlopen from pathlib import Path -import warnings project = "Sphinx Book Theme" copyright = "2023" @@ -36,11 +35,6 @@ "sphinx.ext.todo", ] -# Ignore Glyph 10024 (\N{SPARKLES}) missing from current font from "sphinxext.opengraph" -warnings.filterwarnings( - "ignore", "Glyph 10024 .*? missing from current font", UserWarning -) - # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"]