Skip to content

Commit 3e52a1e

Browse files
Drop usage of internal API when conditionally including assets (#198)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/pre-commit-hooks: v4.3.0 → v4.6.0](pre-commit/pre-commit-hooks@v4.3.0...v4.6.0) - [github.com/mgedmin/check-manifest: 0.48 → 0.49](mgedmin/check-manifest@0.48...0.49) - [github.com/psf/black: 22.3.0 → 24.8.0](psf/black@22.3.0...24.8.0) - [github.com/PyCQA/pylint: v2.14.3 → v3.2.6](pylint-dev/pylint@v2.14.3...v3.2.6) * bump codecov-action to v4 codecov-action v1 is really old, bump to v4. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * Drop usage of internal API when conditionally including assets Fixes #197 by reversing the logic of how JS/CSS assets are added to pages. Rather than adding assets to the app and using undocumented API to *remove* them when not needed, do the exact opposite and only add them to the local html-page-context when building a page. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * drop support for really old Sphinx versions Drop the code that was used to support Sphinx version < 1.8, released 6 years ago. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * pylint: drop usage of bad-continuation `bad-continuation` has been removed from pylint See pylint-dev/pylint#3571 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> --------- Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e83dc14 commit 3e52a1e

File tree

6 files changed

+21
-44
lines changed

6 files changed

+21
-44
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
pytest --cov=sphinx_tabs --cov-report=xml --cov-report=term-missing
3939
- name: Upload to Codecov
4040
if: matrix.python-version == '3.10' && github.repository == 'executablebooks/sphinx-tabs'
41-
uses: codecov/codecov-action@v1
41+
uses: codecov/codecov-action@v4
4242
with:
4343
name: sphinx-tabs-pytests-py3.10
4444
flags: pytests

.pre-commit-config.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
repos:
22

33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.3.0
4+
rev: v4.6.0
55
hooks:
66
- id: check-json
77
- id: check-yaml
@@ -11,21 +11,21 @@ repos:
1111
".xml"
1212

1313
- repo: https://github.com/mgedmin/check-manifest
14-
rev: "0.48"
14+
rev: "0.49"
1515
hooks:
1616
- id: check-manifest
1717

1818
- repo: https://github.com/psf/black
19-
rev: 22.3.0
19+
rev: 24.8.0
2020
hooks:
2121
- id: black
2222

2323
- repo: https://github.com/PyCQA/pylint
24-
rev: v2.14.3
24+
rev: v3.2.6
2525
hooks:
2626
- id: pylint
2727
args:
28-
- --disable=missing-docstring,similarities,fixme,bad-continuation
28+
- --disable=missing-docstring,similarities,fixme
2929
additional_dependencies:
3030
- sphinx
3131
- docutils

pylint.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extension-pkg-whitelist=lxml
33

44
[MESSAGES CONTROL]
5-
disable=missing-docstring,similarities,fixme,bad-continuation
5+
disable=missing-docstring,similarities,fixme
66

77
[REPORTS]
88
reports=no

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_version():
2525
url="https://github.com/executablebooks/sphinx-tabs",
2626
license="MIT",
2727
python_requires=">=3.7",
28-
install_requires=["sphinx", "pygments", "docutils"],
28+
install_requires=["sphinx>=1.8", "pygments", "docutils"],
2929
extras_require={
3030
"testing": [
3131
"coverage",

sphinx_tabs/tabs.py

+10-30
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
from sphinx.directives.code import CodeBlock
1515

1616

17-
FILES = [
17+
JS_FILES = [
1818
"tabs.js",
19-
"tabs.css",
2019
]
2120

21+
CSS_FILES = [
22+
"tabs.css",
23+
]
2224

2325
LEXER_MAP = {}
2426
for lexer in get_all_lexers():
@@ -295,21 +297,6 @@ def found_tabs_directive(self):
295297
return self._found
296298

297299

298-
def update_config(app, config):
299-
"""Adds sphinx-tabs CSS and JS asset files"""
300-
for path in [Path(path) for path in FILES]:
301-
if not config.sphinx_tabs_disable_css_loading and path.suffix == ".css":
302-
if "add_css_file" in dir(app):
303-
app.add_css_file(path.as_posix())
304-
else:
305-
app.add_stylesheet(path.as_posix())
306-
if path.suffix == ".js":
307-
if "add_script_file" in dir(app):
308-
app.add_script_file(path.as_posix())
309-
else:
310-
app.add_js_file(path.as_posix())
311-
312-
313300
# pylint: disable=unused-argument
314301
def update_context(app, pagename, templatename, context, doctree):
315302
"""Remove sphinx-tabs CSS and JS asset files if not used in a page"""
@@ -322,18 +309,12 @@ def update_context(app, pagename, templatename, context, doctree):
322309
if sphinx.version_info >= (4, 1, 0):
323310
include_assets_in_all_pages = app.registry.html_assets_policy == "always"
324311

325-
if not visitor.found_tabs_directive and not include_assets_in_all_pages:
326-
paths = [Path("_static") / f for f in FILES]
327-
if "css_files" in context:
328-
context["css_files"][:] = context["css_files"]
329-
for path in paths:
330-
if path.suffix == ".css" and path in context["css_files"]:
331-
context["css_files"].remove(path.as_posix())
332-
if "script_files" in context:
333-
context["script_files"][:] = context["script_files"]
334-
for path in paths:
335-
if path.suffix == ".js" and path.as_posix() in context["script_files"]:
336-
context["script_files"].remove(path.as_posix())
312+
if visitor.found_tabs_directive or include_assets_in_all_pages:
313+
if not app.config.sphinx_tabs_disable_css_loading:
314+
for css in CSS_FILES:
315+
app.add_css_file(css)
316+
for js in JS_FILES:
317+
app.add_js_file(js)
337318

338319

339320
# pylint: enable=unused-argument
@@ -357,7 +338,6 @@ def setup(app):
357338
"builder-inited",
358339
(lambda app: app.config.html_static_path.insert(0, static_dir.as_posix())),
359340
)
360-
app.connect("config-inited", update_config)
361341
app.connect("html-page-context", update_context)
362342

363343
return {

tests/conftest.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from bs4 import BeautifulSoup
55
import sphinx
66

7-
from sphinx_tabs.tabs import FILES
7+
from sphinx_tabs.tabs import JS_FILES, CSS_FILES
88

99
pytest_plugins = "sphinx.testing.fixtures"
1010

@@ -152,9 +152,6 @@ def check(
152152
):
153153
content = get_sphinx_app_output(app, buildername, filename, encoding)
154154

155-
css_assets = [f for f in FILES if f.endswith(".css")]
156-
js_assets = [f for f in FILES if f.endswith(".js")]
157-
158155
soup = BeautifulSoup(content, "html.parser")
159156
stylesheets = soup.find_all("link", {"rel": "stylesheet"}, href=True)
160157
css_refs = [s["href"] for s in stylesheets]
@@ -165,12 +162,12 @@ def check(
165162
all_refs = css_refs + js_refs
166163

167164
if cssPresent:
168-
css_present = all(any(a in ref for ref in all_refs) for a in css_assets)
165+
css_present = all(any(a in ref for ref in all_refs) for a in CSS_FILES)
169166
assert css_present
170167
else:
171168
assert not "sphinx_tabs" in css_refs
172169
if jsPresent:
173-
js_present = all(any(a in ref for ref in js_refs) for a in js_assets)
170+
js_present = all(any(a in ref for ref in js_refs) for a in JS_FILES)
174171
assert js_present
175172
else:
176173
assert not "sphinx_tabs" in js_refs

0 commit comments

Comments
 (0)