Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Use Sphinx HTML assets policy to decide whether include assets #127

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sphinx_tabs/tabs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Tabbed views for Sphinx, with HTML builder """

import base64
import sphinx
from pathlib import Path
from functools import partial

Expand Down Expand Up @@ -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"][:]
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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is currently failing - I think it should use the new sphinx API?

Suggested change
check_asset_links(app, filename=docname + ".html")
app.registry.html_assets_policy == "always"
check_asset_links(app, filename=docname + ".html")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I updated this test.

However, it kept failing because it seems the output is firstly generated by test_conditional_assets test (without app.set_html_assets_policy("always")) and the result is cached.

What should I do here? Create a new testroot= equal to conditionalassets?

NOTE: in an extension that I'm a maintainer, I'm cleaning the output's path after the test is ran to avoid this problem, see https://github.com/readthedocs/sphinx-hoverxref/blob/master/tests/conftest.py#L9 --maybe something like that is a good approach here as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that looks spot on.

It looks like this is passing the CI tests now. I'm surprised though, given the issue that you describe using the same testroot. Is this failing locally for you?

If yes, please do create a separate root folder for this test. I like your fixture for cleaning up after tests, but do find the test outputs useful for debugging when the tests have failed (locally).

pre-commit is picking up on a couple of minor formatting issues below



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