Blob Blame History Raw
From 8f68aaf020fab4c53c2bc7849ba1d89a0f811bea Mon Sep 17 00:00:00 2001
From: Manuel Kaufmann <humitos@gmail.com>
Date: Mon, 5 Jul 2021 10:54:28 +0200
Subject: [PATCH 1/3] :ok_hand: 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: https://github.com/sphinx-doc/sphinx/issues/9115
---
 sphinx_tabs/tabs.py | 8 +++++++-
 tests/test_build.py | 9 +++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/sphinx_tabs/tabs.py b/sphinx_tabs/tabs.py
index 93742ca..33090af 100644
--- a/sphinx_tabs/tabs.py
+++ b/sphinx_tabs/tabs.py
@@ -1,6 +1,7 @@
 """ Tabbed views for Sphinx, with HTML builder """
 
 import base64
+import sphinx
 from pathlib import Path
 from functools import partial
 
@@ -313,7 +314,12 @@ def update_context(app, pagename, templatename, context, doctree):
         return
     visitor = _FindTabsDirectiveVisitor(doctree)
     doctree.walk(visitor)
-    if not visitor.found_tabs_directive:
+
+    include_assets_in_all_pages = False
+    if sphinx.version_info >= (4, 1, 0):
+        include_assets_in_all_pages = app.registry.html_assets_policy == 'always'
+
+    if not visitor.found_tabs_directive and not include_assets_in_all_pages:
         paths = [Path("_static") / f for f in FILES]
         if "css_files" in context:
             context["css_files"] = context["css_files"][:]
diff --git a/tests/test_build.py b/tests/test_build.py
index 5f1d3f9..ed8331e 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -25,6 +25,15 @@ def test_conditional_assets(app, docname, check_asset_links):
         )
 
 
+@pytest.mark.parametrize("docname", ["index", "no_tabs1", "no_tabs2"])
+@pytest.mark.sphinx(testroot="conditionalassets")
+@pytest.mark.skipif(
+    sphinx.version_info[:2] >= (4, 1), reason="Test uses Sphinx 4.1 config"
+)
+def test_conditional_assets(app, docname, check_asset_links):
+    check_asset_links(app, filename=docname + ".html")
+
+
 @pytest.mark.sphinx(testroot="linenos")
 @pytest.mark.skipif(
     sphinx.version_info[:2] >= (4, 0), reason="Test uses Sphinx 3 code blocks"

From 33e8075ffa0105b6e789037aa701d29149c5465a Mon Sep 17 00:00:00 2001
From: Manuel Kaufmann <humitos@gmail.com>
Date: Mon, 26 Jul 2021 11:38:58 +0200
Subject: [PATCH 2/3] Update test for conditional assets

---
 tests/test_build.py | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/tests/test_build.py b/tests/test_build.py
index ed8331e..ee01aa9 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -25,12 +25,32 @@ def test_conditional_assets(app, docname, check_asset_links):
         )
 
 
+@pytest.mark.noautobuild
 @pytest.mark.parametrize("docname", ["index", "no_tabs1", "no_tabs2"])
 @pytest.mark.sphinx(testroot="conditionalassets")
 @pytest.mark.skipif(
-    sphinx.version_info[:2] >= (4, 1), reason="Test uses Sphinx 4.1 config"
+    sphinx.version_info[:2] < (4, 1), reason="Test uses Sphinx 4.1 config"
 )
-def test_conditional_assets(app, docname, check_asset_links):
+def test_conditional_assets_html_assets_policy(
+    app,
+    docname,
+    status,
+    warning,
+    check_build_success,
+    get_sphinx_app_doctree,
+    regress_sphinx_app_output,
+    check_asset_links,
+):
+    app.set_html_assets_policy("always")
+
+    # Following lines are copied from ``auto_build_and_check`` since we need to
+    # set a config in the build object before auto build. Because of this, we
+    # need to use ``noautobuild``.
+    app.build()
+    check_build_success(status, warning)
+    get_sphinx_app_doctree(app, regress=True)
+    regress_sphinx_app_output(app)
+
     check_asset_links(app, filename=docname + ".html")
 
 

From a97fee53cdfa7abbead9404383b9d42e9adeb4a5 Mon Sep 17 00:00:00 2001
From: foster999 <foster.dev999@gmail.com>
Date: Wed, 4 Aug 2021 22:56:19 +0100
Subject: [PATCH 3/3] Run pre-commit

---
 sphinx_tabs/tabs.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/sphinx_tabs/tabs.py b/sphinx_tabs/tabs.py
index 33090af..3852b7d 100644
--- a/sphinx_tabs/tabs.py
+++ b/sphinx_tabs/tabs.py
@@ -1,9 +1,10 @@
 """ Tabbed views for Sphinx, with HTML builder """
 
 import base64
-import sphinx
 from pathlib import Path
 from functools import partial
+import sphinx
+
 
 from docutils import nodes
 from docutils.parsers.rst import directives
@@ -317,7 +318,7 @@ def update_context(app, pagename, templatename, context, doctree):
 
     include_assets_in_all_pages = False
     if sphinx.version_info >= (4, 1, 0):
-        include_assets_in_all_pages = app.registry.html_assets_policy == 'always'
+        include_assets_in_all_pages = app.registry.html_assets_policy == "always"
 
     if not visitor.found_tabs_directive and not include_assets_in_all_pages:
         paths = [Path("_static") / f for f in FILES]