Skip to content

Commit

Permalink
👌 IMPROVE: Use Sphinx HTML assets policy to decide whether include as…
Browse files Browse the repository at this point in the history
…sets (#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]>
  • Loading branch information
foster999 and humitos authored Aug 4, 2021
1 parent 7bd3fdb commit 7f2146a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sphinx_tabs/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import base64
from pathlib import Path
from functools import partial
import sphinx


from docutils import nodes
from docutils.parsers.rst import directives
Expand Down Expand Up @@ -315,7 +317,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"][:]
Expand Down
29 changes: 29 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ 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"
)
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")


@pytest.mark.sphinx(testroot="linenos")
@pytest.mark.skipif(
sphinx.version_info[:2] >= (4, 0), reason="Test uses Sphinx 3 code blocks"
Expand Down

0 comments on commit 7f2146a

Please sign in to comment.