Skip to content

Commit

Permalink
feat: support sphinx-book-theme (#66)
Browse files Browse the repository at this point in the history
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] authored Oct 27, 2024
1 parent a6a2592 commit d159549
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion deepmodeling_sphinx/banner.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ under MIT license

.header-logo {
height: 50px;
width: 50px;
background-image: url("https://unpkg.com/@njzjz/[email protected]/logos/deepmodeling.svg");
background-repeat: no-repeat;
background-size: 50px 50px;
display: block;
float: left;
z-index: 10;
text-decoration: none;
}

.header-logo::after {
Expand Down
58 changes: 39 additions & 19 deletions deepmodeling_sphinx/banner.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
/* The initial version is taken from
https://github.com/pytorch/pytorch_sphinx_theme
under MIT license
rewrite to remove jQuery by ChatGPT
*/
window.mobileMenu = {
bind: function () {
$("[data-behavior='open-mobile-menu']").on("click", function (e) {
e.preventDefault();
$(".mobile-main-menu").addClass("open");
$("body").addClass("no-scroll");

mobileMenu.listenForResize();
});

$("[data-behavior='close-mobile-menu']").on("click", function (e) {
e.preventDefault();
mobileMenu.close();
});
document
.querySelectorAll("[data-behavior='open-mobile-menu']")
.forEach(function (element) {
element.addEventListener("click", function (e) {
e.preventDefault();
document.querySelector(".mobile-main-menu").classList.add("open");
document.body.classList.add("no-scroll");

mobileMenu.listenForResize();
});
});

document
.querySelectorAll("[data-behavior='close-mobile-menu']")
.forEach(function (element) {
element.addEventListener("click", function (e) {
e.preventDefault();
mobileMenu.close();
});
});
},

listenForResize: function () {
$(window).on("resize.ForMobileMenu", function () {
if ($(this).width() > 768) {
function resizeHandler() {
if (window.innerWidth > 768) {
mobileMenu.close();
}
});
}

window.addEventListener("resize", resizeHandler);

// Store the handler so it can be removed later
this.resizeHandler = resizeHandler;
},

close: function () {
$(".mobile-main-menu").removeClass("open");
$("body").removeClass("no-scroll");
$(window).off("resize.ForMobileMenu");
document.querySelector(".mobile-main-menu").classList.remove("open");
document.body.classList.remove("no-scroll");

// Remove the resize event listener
if (this.resizeHandler) {
window.removeEventListener("resize", this.resizeHandler);
this.resizeHandler = null;
}
},
};
$(document).ready(function () {

document.addEventListener("DOMContentLoaded", function () {
mobileMenu.bind();
});
18 changes: 18 additions & 0 deletions deepmodeling_sphinx/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def render(self, template, render_context):
def insert_icp(app, pagename, templatename, context, doctree):
if not app.config.enable_deepmodeling:
return
if app.config.html_theme == "sphinx_book_theme":
# sphinx_book_theme has provided the option, so there is no need to hack
return
if not hasattr(app.builder.templates.render, "_deepmodeling_icp_patched"):
old_render = app.builder.templates.render

Expand Down Expand Up @@ -188,6 +191,20 @@ def rtd_config(app, config):
config.html_context["READTHEDOCS"] = True


def sphinx_book_theme(app, config):
"""Set configurations for sphinx_book_theme."""
if not config.enable_deepmodeling:
return
if config.html_theme != "sphinx_book_theme":
return
icp_footer = (
'<p><a href="https://beian.miit.gov.cn" target="_blank">%s</a></p>' % icp_no
)
config.html_theme_options["extra_footer"] = (
config.html_theme_options.get("extra_footer", "") + icp_footer
)


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

return {"parallel_read_safe": True}
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sphinx.ext.intersphinx",
"numpydoc",
]
html_theme = "sphinx_rtd_theme"
html_theme = "sphinx_book_theme"


def run_apidoc(_):
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sphinx >=4.2
sphinx_rtd_theme >=0.6
sphinx-book-theme
.
myst_parser
docutils >=0.17
Expand Down

0 comments on commit d159549

Please sign in to comment.