Skip to content

Commit 7f2146a

Browse files
foster999humitos
andauthored
👌 IMPROVE: Use Sphinx HTML assets policy to decide whether include assets (#132)
* 👌 IMPROVE: Use Sphinx HTML assets policy to decide whether include assets Sphinx v4.1.0 will include `Sphinx.registry.html_assets_policy` that defines the policy to whether include or not HTML assets in pages where the extension is not used. This PR makes usage of this policy to decide if sphinx-tabs assets should be included or not in the page that's being rendered. Reference: sphinx-doc/sphinx#9115 * Update test for conditional assets * Run pre-commit Co-authored-by: Manuel Kaufmann <[email protected]>
1 parent 7bd3fdb commit 7f2146a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

‎sphinx_tabs/tabs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import base64
44
from pathlib import Path
55
from functools import partial
6+
import sphinx
7+
68

79
from docutils import nodes
810
from docutils.parsers.rst import directives
@@ -315,7 +317,12 @@ def update_context(app, pagename, templatename, context, doctree):
315317
return
316318
visitor = _FindTabsDirectiveVisitor(doctree)
317319
doctree.walk(visitor)
318-
if not visitor.found_tabs_directive:
320+
321+
include_assets_in_all_pages = False
322+
if sphinx.version_info >= (4, 1, 0):
323+
include_assets_in_all_pages = app.registry.html_assets_policy == "always"
324+
325+
if not visitor.found_tabs_directive and not include_assets_in_all_pages:
319326
paths = [Path("_static") / f for f in FILES]
320327
if "css_files" in context:
321328
context["css_files"] = context["css_files"][:]

‎tests/test_build.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ def test_conditional_assets(app, docname, check_asset_links):
2525
)
2626

2727

28+
@pytest.mark.noautobuild
29+
@pytest.mark.parametrize("docname", ["index", "no_tabs1", "no_tabs2"])
30+
@pytest.mark.sphinx(testroot="conditionalassets")
31+
@pytest.mark.skipif(
32+
sphinx.version_info[:2] < (4, 1), reason="Test uses Sphinx 4.1 config"
33+
)
34+
def test_conditional_assets_html_assets_policy(
35+
app,
36+
docname,
37+
status,
38+
warning,
39+
check_build_success,
40+
get_sphinx_app_doctree,
41+
regress_sphinx_app_output,
42+
check_asset_links,
43+
):
44+
app.set_html_assets_policy("always")
45+
46+
# Following lines are copied from ``auto_build_and_check`` since we need to
47+
# set a config in the build object before auto build. Because of this, we
48+
# need to use ``noautobuild``.
49+
app.build()
50+
check_build_success(status, warning)
51+
get_sphinx_app_doctree(app, regress=True)
52+
regress_sphinx_app_output(app)
53+
54+
check_asset_links(app, filename=docname + ".html")
55+
56+
2857
@pytest.mark.sphinx(testroot="linenos")
2958
@pytest.mark.skipif(
3059
sphinx.version_info[:2] >= (4, 0), reason="Test uses Sphinx 3 code blocks"

0 commit comments

Comments
 (0)