Skip to content

Commit eaebbec

Browse files
authored
Refactor add_sidebars() into _get_sidebars() (#13863)
1 parent 463fae6 commit eaebbec

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

sphinx/builders/html/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,31 +1040,30 @@ def get_output_path(self, page_name: str, /) -> Path:
10401040
def get_outfilename(self, pagename: str) -> _StrPath:
10411041
return _StrPath(self.get_output_path(pagename))
10421042

1043-
def add_sidebars(self, pagename: str, ctx: dict[str, Any]) -> None:
1044-
def has_wildcard(pattern: str) -> bool:
1045-
return any(char in pattern for char in '*?[')
1046-
1043+
def _get_sidebars(self, pagename: str, /) -> tuple[str, ...]:
10471044
matched = None
10481045

10491046
# default sidebars settings for selected theme
1050-
sidebars = list(self.theme.sidebar_templates)
1047+
sidebars = self.theme.sidebar_templates
10511048

10521049
# user sidebar settings
10531050
html_sidebars = self.get_builder_config('sidebars', 'html')
10541051
msg = __('page %s matches two patterns in html_sidebars: %r and %r')
10551052
for pattern, pat_sidebars in html_sidebars.items():
10561053
if patmatch(pagename, pattern):
1057-
if matched and has_wildcard(pattern):
1054+
if matched and _has_wildcard(pattern):
10581055
# warn if both patterns contain wildcards
1059-
if has_wildcard(matched):
1056+
if _has_wildcard(matched):
10601057
logger.warning(msg, pagename, matched, pattern)
10611058
# else the already matched pattern is more specific
10621059
# than the present one, because it contains no wildcard
10631060
continue
10641061
matched = pattern
1065-
sidebars = pat_sidebars
1062+
sidebars = tuple(pat_sidebars)
1063+
return sidebars
10661064

1067-
ctx['sidebars'] = list(sidebars)
1065+
def add_sidebars(self, pagename: str, ctx: dict[str, Any]) -> None:
1066+
ctx['sidebars'] = list(self._get_sidebars(pagename))
10681067

10691068
# --------- these are overwritten by the serialization builder
10701069

@@ -1123,7 +1122,7 @@ def hasdoc(name: str) -> bool:
11231122
ctx['hasdoc'] = hasdoc
11241123

11251124
ctx['toctree'] = lambda **kwargs: self._get_local_toctree(pagename, **kwargs)
1126-
self.add_sidebars(pagename, ctx)
1125+
ctx['sidebars'] = list(self._get_sidebars(pagename))
11271126
ctx.update(addctx)
11281127

11291128
# 'blah.html' should have content_root = './' not ''.
@@ -1292,6 +1291,10 @@ def dump_search_index(self) -> None:
12921291
Path(search_index_tmp).replace(search_index_path)
12931292

12941293

1294+
def _has_wildcard(pattern: str, /) -> bool:
1295+
return any(char in pattern for char in '*?[')
1296+
1297+
12951298
def convert_html_css_files(app: Sphinx, config: Config) -> None:
12961299
"""Convert string styled html_css_files to tuple styled one."""
12971300
html_css_files: list[tuple[str, dict[str, str]]] = []

tests/test_builders/test_build_html.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
from tests.test_builders.xpath_util import check_xpath
3131

3232
if TYPE_CHECKING:
33-
from typing import Any
34-
3533
from sphinx.testing.util import SphinxTestApp
3634

3735

@@ -416,8 +414,6 @@ def test_html_style(app: SphinxTestApp) -> None:
416414
},
417415
)
418416
def test_html_sidebar(app: SphinxTestApp) -> None:
419-
ctx: dict[str, Any] = {}
420-
421417
# default for alabaster
422418
app.build(force_all=True)
423419
result = (app.outdir / 'index.html').read_text(encoding='utf8')
@@ -433,12 +429,12 @@ def test_html_sidebar(app: SphinxTestApp) -> None:
433429
assert '<h3>This Page</h3>' in result
434430

435431
assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
436-
app.builder.add_sidebars('index', ctx)
437-
assert ctx['sidebars'] == [
432+
sidebars = app.builder._get_sidebars('index')
433+
assert sidebars == (
438434
'localtoc.html',
439435
'searchfield.html',
440436
'sourcelink.html',
441-
]
437+
)
442438

443439
# only sourcelink.html
444440
app.config.html_sidebars = {'**': ['sourcelink.html']}
@@ -456,8 +452,8 @@ def test_html_sidebar(app: SphinxTestApp) -> None:
456452
assert '<h3>This Page</h3>' in result
457453

458454
assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
459-
app.builder.add_sidebars('index', ctx)
460-
assert ctx['sidebars'] == ['sourcelink.html']
455+
sidebars = app.builder._get_sidebars('index')
456+
assert sidebars == ('sourcelink.html',)
461457

462458
# no sidebars
463459
app.config.html_sidebars = {'**': []}
@@ -477,8 +473,8 @@ def test_html_sidebar(app: SphinxTestApp) -> None:
477473
assert '<h3>This Page</h3>' not in result
478474

479475
assert isinstance(app.builder, StandaloneHTMLBuilder) # type-checking
480-
app.builder.add_sidebars('index', ctx)
481-
assert ctx['sidebars'] == []
476+
sidebars = app.builder._get_sidebars('index')
477+
assert sidebars == ()
482478

483479

484480
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)