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

Add support for "scope" option #284

Merged
merged 14 commits into from
Oct 27, 2023
6 changes: 6 additions & 0 deletions docs/customization.rst
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,12 @@ Configuration Options

A list of dictionaries used to populate the :ref:`version_dropdown` selector.

.. themeconf:: scope

Scope of site preferences (i.e. cookie consent, content tabs, color palette).
If you have multi-site project, you can set this to "/"
to share preferences between all sub-sites.
Bizordec marked this conversation as resolved.
Show resolved Hide resolved

.. confval:: sphinx_immaterial_external_resource_cache_dir

Specifies the local directory used to cache external resources, such as
Expand Down
1 change: 1 addition & 0 deletions requirements/dev-mypy.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mypy==1.5.1
types-PyYAML
docutils-stubs
types-beautifulsoup4
types-jsonschema
types-appdirs
types-requests
Expand Down
1 change: 1 addition & 0 deletions sphinx_immaterial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def html_page_context(
"disqus": theme_options.get("disqus"),
"manifest": theme_options.get("pwa_manifest"),
"analytics": analytics,
"scope": theme_options.get("scope"),
},
"plugins": theme_options.get("plugins"),
},
Expand Down
5 changes: 5 additions & 0 deletions sphinx_immaterial/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ version_info =
# file hosted in the root of the site. You can use other file names or locations, e.g.,
# "_static/old_versions.json"
version_json = "versions.json"

# Scope of site preferences (i.e. cookie consent, content tabs, color palette).
# If you have multi-site project, you can set this to "/"
# to share preferences between all sub-sites.
scope =
44 changes: 44 additions & 0 deletions tests/extra_scope_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

from typing import TYPE_CHECKING
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved
from bs4 import BeautifulSoup
import re
import textwrap

if TYPE_CHECKING:
from sphinx.testing.util import SphinxTestApp
from bs4.element import Tag


def test_extra_scope(immaterial_make_app):
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved
expected_scope_url = "/test"

app: SphinxTestApp = immaterial_make_app(
extra_conf=textwrap.dedent(
f"""
html_theme_options = {{
"scope": "{expected_scope_url}"
}}
"""
),
files={
"index.rst": "Sample text",
},
)
app.build()

with open(app.outdir / "index.html", mode="r") as file:
2bndy5 marked this conversation as resolved.
Show resolved Hide resolved
soup = BeautifulSoup(file.read(), "html.parser")

head = soup.head
assert head is not None

scope_pattern = re.compile(r"__md_scope=new URL\(\"(?P<url>[^\"]+)\"")
matched_scope_url = ""
for script in head.find_all("script"):
scope_match = scope_pattern.search(script.text)
if scope_match:
matched_scope_url = scope_match.group("url")
break

assert matched_scope_url == expected_scope_url
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coverage[toml] >= 7.0
pytest
pytest-snapshot
beautifulsoup4
tests/issue_134/sphinx-immaterial-pybind11-issue-134

-r ../requirements.txt
Expand Down