Skip to content

Commit

Permalink
Load all themes defined via entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 17, 2024
1 parent 8fe915f commit 982681b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Release 7.3.2 (in development)
Bugs fixed
----------

* Preload all themes defined via entry points.
Patch by Adam Turner.

Release 7.3.1 (released Apr 17, 2024)
=====================================
Expand Down
15 changes: 5 additions & 10 deletions sphinx/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def __init__(self, app: Sphinx) -> None:
self._load_builtin_themes()
if getattr(app.config, 'html_theme_path', None):
self._load_additional_themes(app.config.html_theme_path)
self._load_entry_point_themes()

def _load_builtin_themes(self) -> None:
"""Load built-in themes."""
Expand All @@ -174,17 +175,14 @@ def _load_additional_themes(self, theme_paths: str) -> None:
for name, theme in themes.items():
self._themes[name] = theme

def _load_extra_theme(self, name: str) -> None:
def _load_entry_point_themes(self) -> None:
"""Try to load a theme with the specified name.
This uses the ``sphinx.html_themes`` entry point from package metadata.
"""
theme_entry_points = entry_points(group='sphinx.html_themes')
try:
entry_point = theme_entry_points[name]
except KeyError:
pass
else:
for entry_point in entry_points(group='sphinx.html_themes'):
if entry_point.name in self._themes:
continue # don't overwrite loaded themes
self._app.registry.load_extension(self._app, entry_point.module)
_config_post_init(self._app, self._app.config)

Expand Down Expand Up @@ -219,9 +217,6 @@ def _find_themes(theme_path: str) -> dict[str, str]:

def create(self, name: str) -> Theme:
"""Create an instance of theme."""
if name not in self._themes:
self._load_extra_theme(name)

if name not in self._themes:
raise ThemeError(__('no theme named %r found (missing theme.toml?)') % name)

Expand Down

0 comments on commit 982681b

Please sign in to comment.