Skip to content

Commit dc24c93

Browse files
authored
Merge pull request #8643 from tk0miya/refactor_basic/layout.html
refactor: Move CSS tags in basic/layout.html to ``css_files`` variable
2 parents 967aeb2 + 5b392e3 commit dc24c93

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Incompatible changes
2020
:confval:`man_make_section_directory`)
2121
* #8380: html search: search results are wrapped with ``<p>`` instead of
2222
``<div>``
23+
* html theme: Move CSS tags in basic/layout.html to ``css_files`` variable
2324
* #8508: LaTeX: uplatex becomes a default setting of latex_engine for Japanese
2425
documents
2526

sphinx/application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,8 @@ def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> Non
983983
984984
* - Priority
985985
- Main purpose in Sphinx
986+
* - 200
987+
- default priority for built-in CSS files
986988
* - 500
987989
- default priority for extensions
988990
* - 800

sphinx/builders/html/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ def _get_translations_js(self) -> str:
250250
return jsfile
251251
return None
252252

253+
def _get_style_filename(self) -> str:
254+
if self.config.html_style is not None:
255+
return self.config.html_style
256+
elif self.theme:
257+
return self.theme.get_config('theme', 'stylesheet')
258+
else:
259+
return 'default.css'
260+
253261
def get_theme_config(self) -> Tuple[str, Dict]:
254262
return self.config.html_theme, self.config.html_theme_options
255263

@@ -285,6 +293,9 @@ def init_highlighter(self) -> None:
285293
self.dark_highlighter = None
286294

287295
def init_css_files(self) -> None:
296+
self.add_css_file('pygments.css', priority=200)
297+
self.add_css_file(self._get_style_filename(), priority=200)
298+
288299
for filename, attrs in self.app.registry.css_files:
289300
self.add_css_file(filename, **attrs)
290301

@@ -470,13 +481,6 @@ def prepare_writing(self, docnames: Set[str]) -> None:
470481
rellinks.append((indexname, indexcls.localname,
471482
'', indexcls.shortname))
472483

473-
if self.config.html_style is not None:
474-
stylename = self.config.html_style
475-
elif self.theme:
476-
stylename = self.theme.get_config('theme', 'stylesheet')
477-
else:
478-
stylename = 'default.css'
479-
480484
self.globalcontext = {
481485
'embedded': self.embedded,
482486
'project': self.config.project,
@@ -499,7 +503,7 @@ def prepare_writing(self, docnames: Set[str]) -> None:
499503
'language': self.config.language,
500504
'css_files': self.css_files,
501505
'sphinx_version': __display_version__,
502-
'style': stylename,
506+
'style': self._get_style_filename(),
503507
'rellinks': rellinks,
504508
'builder': self.name,
505509
'parents': [],

sphinx/themes/basic/layout.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ <h3>{{ _('Navigation') }}</h3>
9595
{%- endmacro %}
9696

9797
{%- macro css() %}
98-
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', 1) }}" type="text/css" />
99-
<link rel="stylesheet" href="{{ pathto('_static/' + style, 1)|e }}" type="text/css" />
10098
{%- for css in css_files %}
10199
{%- if css|attr("filename") %}
102100
{{ css_tag(css) }}

tests/test_build_html.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ def test_assets_order(app):
12191219
content = (app.outdir / 'index.html').read_text()
12201220

12211221
# css_files
1222-
expected = ['_static/pygments.css', '_static/alabaster.css', '_static/early.css',
1222+
expected = ['_static/early.css', '_static/pygments.css', '_static/alabaster.css',
12231223
'_static/normal.css', '_static/late.css', '_static/css/style.css',
12241224
'https://example.com/custom.css', '_static/lazy.css']
12251225
pattern = '.*'.join('href="%s"' % f for f in expected)
@@ -1357,8 +1357,8 @@ def test_alternate_stylesheets(app, cached_etree_parse, fname, expect):
13571357
def test_html_style(app, status, warning):
13581358
app.build()
13591359
result = (app.outdir / 'index.html').read_text()
1360-
assert '<link rel="stylesheet" href="_static/default.css" type="text/css" />' in result
1361-
assert ('<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />'
1360+
assert '<link rel="stylesheet" type="text/css" href="_static/default.css" />' in result
1361+
assert ('<link rel="stylesheet" type="text/css" href="_static/alabaster.css" />'
13621362
not in result)
13631363

13641364

tests/test_theming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_dark_style(app, status, warning):
127127
assert (app.outdir / '_static' / 'pygments_dark.css').exists()
128128

129129
result = (app.outdir / 'index.html').read_text()
130-
assert '<link rel="stylesheet" href="_static/pygments.css" type="text/css" />' in result
130+
assert '<link rel="stylesheet" type="text/css" href="_static/pygments.css" />' in result
131131
assert ('<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" '
132132
'rel="stylesheet" type="text/css" '
133133
'href="_static/pygments_dark.css" />') in result

0 commit comments

Comments
 (0)