Skip to content

Commit

Permalink
fix: support RTD addons (#60)
Browse files Browse the repository at this point in the history
See https://about.readthedocs.com/blog/2024/07/addons-by-default/

Changes:
1. Set configurations suggested by
https://about.readthedocs.com/blog/2024/07/addons-by-default/;
2. Keep `</head>` when minifying HTML files as RTD replaces it;
3. Set z-index of our banner to 1749, as the new RTD banner uses 1750.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
njzjz and pre-commit-ci[bot] committed Jul 18, 2024
1 parent b75d8a3 commit e1e8c6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion deepmodeling_sphinx/banner.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ under MIT license
padding-right: 0;
padding-left: 0;
/* pydata theme has z-index 1030; we should be the largest */
z-index: 2000;
/* RTD banner use 1750; we should be smaller than RTD */
z-index: 1749;
}

.header-holder .container {
Expand Down
23 changes: 22 additions & 1 deletion deepmodeling_sphinx/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ def minify_html_files(app, pagename, templatename, context, doctree):
def render(self, template, render_context):
content = old_render(template, render_context)
try:
return minify_html.minify(content, minify_js=True, minify_css=True)
return minify_html.minify(
content,
minify_js=True,
minify_css=True,
keep_html_and_head_opening_tags=True,
keep_closing_tags=True,
)
except SyntaxError:
return content

Expand Down Expand Up @@ -168,6 +174,20 @@ def enable_dark_mode(app, config):
app.add_css_file("dark_rtd.css")


def rtd_config(app, config):
"""Set RTD configurations.
See https://about.readthedocs.com/blog/2024/07/addons-by-default/
"""
config.html_baseurl = os.environ.get("READTHEDOCS_CANONICAL_URL", "")

# Tell Jinja2 templates the build is running on Read the Docs
if os.environ.get("READTHEDOCS", "") == "True":
if "html_context" not in config:
config.html_context = {}
config.html_context["READTHEDOCS"] = True


def setup(app: Sphinx) -> Dict[str, Any]:
# enable deepmodeling sidebar and icp
# if the repo is outside the deepmodeling, disable it
Expand All @@ -181,5 +201,6 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.connect("build-finished", minify_css_files)
# dark mode for rtd theme
app.connect("config-inited", enable_dark_mode)
app.connect("config-inited", rtd_config)

return {"parallel_read_safe": True}

0 comments on commit e1e8c6d

Please sign in to comment.