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

Allow specifying multiple CSS files in themes #10465

Merged
merged 3 commits into from Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -13,6 +13,9 @@ Deprecated
Features added
--------------

* #10444: html theme: Allow to specify multiple CSS files via ``stylesheet``
setting on ``theme.conf``
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* #10444: html theme: Allow to specify multiple CSS files via ``stylesheet``
setting on ``theme.conf``
* #10444: html theme: Allow specifying multiple CSS files through the ``stylesheet``
setting in ``theme.conf`` or by setting ``html_style`` to an iterable of strings.


Bugs fixed
----------

Expand Down
11 changes: 7 additions & 4 deletions doc/development/theming.rst
Expand Up @@ -56,10 +56,10 @@ Python :mod:`ConfigParser` module) and has the following structure:
want to also inherit the stylesheet, include it via CSS' ``@import`` in your
own.

* The **stylesheet** setting gives the name of a CSS file which will be
referenced in the HTML header. If you need more than one CSS file, either
include one from the other via CSS' ``@import``, or use a custom HTML template
that adds ``<link rel="stylesheet">`` tags as necessary. Setting the
* The **stylesheet** setting gives a list of CSS filenames separated commas which
will be referenced in the HTML header. You can also use CSS' ``@import``
technique to include one from the other, or use a custom HTML template that
adds ``<link rel="stylesheet">`` tags as necessary. Setting the
:confval:`html_style` config value will override this setting.

* The **pygments_style** setting gives the name of a Pygments style to use for
Expand All @@ -82,6 +82,9 @@ Python :mod:`ConfigParser` module) and has the following structure:
.. versionadded:: 1.7
sidebar settings

.. versionchanged:: 5.1

The stylesheet setting accepts multiple CSS filenames

.. _distribute-your-theme:

Expand Down
13 changes: 12 additions & 1 deletion sphinx/builders/html/__init__.py
Expand Up @@ -272,6 +272,15 @@ def _get_style_filename(self) -> str:
else:
return 'default.css'

def get_style_filenames(self) -> str:
if self.config.html_style is not None:
return [self.config.html_style]
elif self.theme:
stylesheet = self.theme.get_config('theme', 'stylesheet')
return [s.strip() for s in stylesheet.split(',')]
else:
return ['default.css']
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def get_style_filenames(self) -> str:
if self.config.html_style is not None:
return [self.config.html_style]
elif self.theme:
stylesheet = self.theme.get_config('theme', 'stylesheet')
return [s.strip() for s in stylesheet.split(',')]
else:
return ['default.css']
def _get_style_filenames(self) -> Iterator[str]:
if isinstance(self.config.html_style, str):
yield self.config.html_style
elif self.config.html_style is not None:
yield from self.config.html_style
elif self.theme:
stylesheet = self.theme.get_config('theme', 'stylesheet')
yield from (s.strip() for s in stylesheet.split(', '))
else:
yield 'default.css'


def get_theme_config(self) -> Tuple[str, Dict]:
return self.config.html_theme, self.config.html_theme_options

Expand Down Expand Up @@ -309,7 +318,9 @@ def init_highlighter(self) -> None:
def init_css_files(self) -> None:
self.css_files = []
self.add_css_file('pygments.css', priority=200)
self.add_css_file(self._get_style_filename(), priority=200)

for filename in self.get_style_filenames():
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for filename in self.get_style_filenames():
for filename in self._get_style_filenames():

self.add_css_file(filename, priority=200)

for filename, attrs in self.app.registry.css_files:
self.add_css_file(filename, **attrs)
Expand Down
@@ -0,0 +1,3 @@
[theme]
inherit = basic
stylesheet = mytheme.css, extra.css
@@ -0,0 +1,2 @@
html_theme_path = ['_themes']
html_theme = 'mytheme'
@@ -0,0 +1,2 @@
test-build-html-theme-having-multiple-stylesheets
=================================================
9 changes: 9 additions & 0 deletions tests/test_build_html.py
Expand Up @@ -1735,3 +1735,12 @@ def test_html_code_role(app):
assert ('<div class="highlight-python notranslate">' +
'<div class="highlight"><pre><span></span>' +
common_content) in content


@pytest.mark.sphinx('html', testroot='build-html-theme-having-multiple-stylesheets')
def test_theme_having_multiple_stylesheets(app):
app.build()
content = (app.outdir / 'index.html').read_text(encoding='utf-8')

assert '<link rel="stylesheet" type="text/css" href="_static/mytheme.css" />' in content
assert '<link rel="stylesheet" type="text/css" href="_static/extra.css" />' in content