@@ -77,7 +77,7 @@ def _gen_hash(path: str) -> str:
77
77
return hashlib .sha1 (path .read_bytes ()).hexdigest ()
78
78
79
79
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 ):
81
81
"""Generate a hash for assets, and append to its entry in context.
82
82
83
83
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):
88
88
context: the Sphinx context object where asset links are stored. These are:
89
89
`css_files` and `script_files` keys.
90
90
"""
91
- for asset in assets :
91
+ for asset_path in assets :
92
92
# 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"
94
94
if asset_type in context :
95
95
# 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
98
98
if not asset_source_path .exists ():
99
99
SPHINX_LOGGER .warning (
100
100
f"Asset { asset_source_path } does not exist, not linking."
101
101
)
102
102
# 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
+ )
107
121
108
122
109
123
def hash_html_assets (app , pagename , templatename , context , doctree ):
@@ -117,7 +131,7 @@ def hash_html_assets(app, pagename, templatename, context, doctree):
117
131
# run but the book theme CSS file won't be linked in Sphinx.
118
132
if app .config .html_theme == "sphinx_book_theme" :
119
133
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 )
121
135
122
136
123
137
def update_mode_thebe_config (app ):
0 commit comments