Skip to content

Commit

Permalink
Fix theme inheritance (#24)
Browse files Browse the repository at this point in the history
Search inherited themes for inherited layout.html
  • Loading branch information
guyer authored Mar 7, 2024
1 parent 61be6bf commit deaff4f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ntd2d/ntd2d_action/files/templates/ntd2d/layout.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{% extends "{inherited_theme}/layout.html" %}}
{{% extends "{inherited_layout}" %}}

{{%- block extrahead %}}
{{{{ super() }}}}
Expand Down
25 changes: 23 additions & 2 deletions ntd2d/ntd2d_action/sphinxdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ def epub_file(self):
def pdf_file(self):
return self.build_dir / "latex" / f"{self.conf.project.lower()}.pdf"

def get_theme(self, theme_name):
theme_factory = HTMLThemeFactory(self.sphinx_app)
return theme_factory.create(theme_name)

@property
def stylesheet(self):
theme_factory = HTMLThemeFactory(self.sphinx_app)
theme = theme_factory.create(self.sphinx_app.config.html_theme)
theme = self.get_theme(self.sphinx_app.config.html_theme)

return theme.get_config("theme", "stylesheet")

Expand Down Expand Up @@ -149,6 +152,23 @@ def make_conf_file(self):
def inherited_theme(self):
return self.original_docs.conf.html_theme

@property
def inherited_layout(self):
"""Find inherited layout.html
Inherited theme may not define its own :file:`layout.html`, but
rely on a theme that it, in turn, derives from.
`{% extends "!layout.html" %}` should work, but doesn't.
https://github.com/sphinx-doc/sphinx/issues/12049
"""
def get_theme_layout(theme):
if (pathlib.Path(theme.themedir) / "layout.html").exists():
return f"{theme.name}/layout.html"
else:
return get_theme_layout(theme.base)

return get_theme_layout(self.get_theme(self.inherited_theme))

def assimilate_theme(self, name, insert_header_footer=True):
"""Replace configuration directory with customized html theme."""

Expand All @@ -160,6 +180,7 @@ def assimilate_theme(self, name, insert_header_footer=True):
self.theme = TemplateHierarchy(name=name,
destination_dir=self.conf.theme_path,
inherited_theme=self.inherited_theme,
inherited_layout=self.inherited_layout,
inherited_css=self.stylesheet,
header_footer_script=header_footer)
self.theme.write()
Expand Down

0 comments on commit deaff4f

Please sign in to comment.