Refactor `add_sidebars() into _get_sidebars()` (#13863)

This commit is contained in:
Adam Turner authored and GitHub committed 2025-09-01 19:10:06 +01:00
1 parent 463fae68e5
commit eaebbec4d1
2 files changed
+20 -21

No files matched your search

+13 -10
View File
@@ -1040,31 +1040,30 @@ class StandaloneHTMLBuilder(Builder):
def get_outfilename(self, pagename: str) -> _StrPath:
return _StrPath(self.get_output_path(pagename))
def add_sidebars(self, pagename: str, ctx: dict[str, Any]) -> None:
def has_wildcard(pattern: str) -> bool:
return any(char in pattern for char in '*?[')
def _get_sidebars(self, pagename: str, /) -> tuple[str, ...]:
matched = None
# default sidebars settings for selected theme
sidebars = list(self.theme.sidebar_templates)
sidebars = self.theme.sidebar_templates
# user sidebar settings
html_sidebars = self.get_builder_config('sidebars', 'html')
msg = __('page %s matches two patterns in html_sidebars: %r and %r')
for pattern, pat_sidebars in html_sidebars.items():
if patmatch(pagename, pattern):
if matched and has_wildcard(pattern):
if matched and _has_wildcard(pattern):
# warn if both patterns contain wildcards
if has_wildcard(matched):
if _has_wildcard(matched):
logger.warning(msg, pagename, matched, pattern)
# else the already matched pattern is more specific
# than the present one, because it contains no wildcard
continue
matched = pattern
sidebars = pat_sidebars
sidebars = tuple(pat_sidebars)
return sidebars
ctx['sidebars'] = list(sidebars)
def add_sidebars(self, pagename: str, ctx: dict[str, Any]) -> None:
ctx['sidebars'] = list(self._get_sidebars(pagename))
# --------- these are overwritten by the serialization builder
@@ -1123,7 +1122,7 @@ class StandaloneHTMLBuilder(Builder):
ctx['hasdoc'] = hasdoc
ctx['toctree'] = lambda **kwargs: self._get_local_toctree(pagename, **kwargs)
self.add_sidebars(pagename, ctx)
ctx['sidebars'] = list(self._get_sidebars(pagename))
ctx.update(addctx)
# 'blah.html' should have content_root = './' not ''.
@@ -1292,6 +1291,10 @@ class StandaloneHTMLBuilder(Builder):
Path(search_index_tmp).replace(search_index_path)
def _has_wildcard(pattern: str, /) -> bool:
return any(char in pattern for char in '*?[')
def convert_html_css_files(app: Sphinx, config: Config) -> None:
"""Convert string styled html_css_files to tuple styled one."""
html_css_files: list[tuple[str, dict[str, str]]] = []
+7 -11
View File
@@ -30,8 +30,6 @@ from tests.test_builders.xpath_data import FIGURE_CAPTION
from tests.test_builders.xpath_util import check_xpath
if TYPE_CHECKING:
from typing import Any
from sphinx.testing.util import SphinxTestApp
@@ -416,8 +414,6 @@ def test_html_style(app: SphinxTestApp) -> None:
},
)
def test_html_sidebar(app: SphinxTestApp) -> None:
ctx: dict[str, Any] = {}
# default for alabaster
app.build(force_all=True)
result = (app.outdir / 'index.html').read_text(encoding='utf8')
@@ -433,12 +429,12 @@ def test_html_sidebar(app: SphinxTestApp) -> None:
assert '<h3>This Page</h3>' in result
assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
app.builder.add_sidebars('index', ctx)
assert ctx['sidebars'] == [
sidebars = app.builder._get_sidebars('index')
assert sidebars == (
'localtoc.html',
'searchfield.html',
'sourcelink.html',
]
)
# only sourcelink.html
app.config.html_sidebars = {'**': ['sourcelink.html']}
@@ -456,8 +452,8 @@ def test_html_sidebar(app: SphinxTestApp) -> None:
assert '<h3>This Page</h3>' in result
assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
app.builder.add_sidebars('index', ctx)
assert ctx['sidebars'] == ['sourcelink.html']
sidebars = app.builder._get_sidebars('index')
assert sidebars == ('sourcelink.html',)
# no sidebars
app.config.html_sidebars = {'**': []}
@@ -477,8 +473,8 @@ def test_html_sidebar(app: SphinxTestApp) -> None:
assert '<h3>This Page</h3>' not in result
assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
app.builder.add_sidebars('index', ctx)
assert ctx['sidebars'] == []
sidebars = app.builder._get_sidebars('index')
assert sidebars == ()
@pytest.mark.parametrize(