Skip to content

Commit d159549

Browse files
feat: support sphinx-book-theme (#66)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a6a2592 commit d159549

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

deepmodeling_sphinx/banner.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ under MIT license
129129

130130
.header-logo {
131131
height: 50px;
132-
width: 50px;
133132
background-image: url("https://unpkg.com/@njzjz/[email protected]/logos/deepmodeling.svg");
134133
background-repeat: no-repeat;
135134
background-size: 50px 50px;
136135
display: block;
137136
float: left;
138137
z-index: 10;
138+
text-decoration: none;
139139
}
140140

141141
.header-logo::after {

deepmodeling_sphinx/banner.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
11
/* The initial version is taken from
22
https://github.com/pytorch/pytorch_sphinx_theme
33
under MIT license
4+
rewrite to remove jQuery by ChatGPT
45
*/
56
window.mobileMenu = {
67
bind: function () {
7-
$("[data-behavior='open-mobile-menu']").on("click", function (e) {
8-
e.preventDefault();
9-
$(".mobile-main-menu").addClass("open");
10-
$("body").addClass("no-scroll");
11-
12-
mobileMenu.listenForResize();
13-
});
14-
15-
$("[data-behavior='close-mobile-menu']").on("click", function (e) {
16-
e.preventDefault();
17-
mobileMenu.close();
18-
});
8+
document
9+
.querySelectorAll("[data-behavior='open-mobile-menu']")
10+
.forEach(function (element) {
11+
element.addEventListener("click", function (e) {
12+
e.preventDefault();
13+
document.querySelector(".mobile-main-menu").classList.add("open");
14+
document.body.classList.add("no-scroll");
15+
16+
mobileMenu.listenForResize();
17+
});
18+
});
19+
20+
document
21+
.querySelectorAll("[data-behavior='close-mobile-menu']")
22+
.forEach(function (element) {
23+
element.addEventListener("click", function (e) {
24+
e.preventDefault();
25+
mobileMenu.close();
26+
});
27+
});
1928
},
2029

2130
listenForResize: function () {
22-
$(window).on("resize.ForMobileMenu", function () {
23-
if ($(this).width() > 768) {
31+
function resizeHandler() {
32+
if (window.innerWidth > 768) {
2433
mobileMenu.close();
2534
}
26-
});
35+
}
36+
37+
window.addEventListener("resize", resizeHandler);
38+
39+
// Store the handler so it can be removed later
40+
this.resizeHandler = resizeHandler;
2741
},
2842

2943
close: function () {
30-
$(".mobile-main-menu").removeClass("open");
31-
$("body").removeClass("no-scroll");
32-
$(window).off("resize.ForMobileMenu");
44+
document.querySelector(".mobile-main-menu").classList.remove("open");
45+
document.body.classList.remove("no-scroll");
46+
47+
// Remove the resize event listener
48+
if (this.resizeHandler) {
49+
window.removeEventListener("resize", this.resizeHandler);
50+
this.resizeHandler = null;
51+
}
3352
},
3453
};
35-
$(document).ready(function () {
54+
55+
document.addEventListener("DOMContentLoaded", function () {
3656
mobileMenu.bind();
3757
});

deepmodeling_sphinx/inject.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def render(self, template, render_context):
8585
def insert_icp(app, pagename, templatename, context, doctree):
8686
if not app.config.enable_deepmodeling:
8787
return
88+
if app.config.html_theme == "sphinx_book_theme":
89+
# sphinx_book_theme has provided the option, so there is no need to hack
90+
return
8891
if not hasattr(app.builder.templates.render, "_deepmodeling_icp_patched"):
8992
old_render = app.builder.templates.render
9093

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

190193

194+
def sphinx_book_theme(app, config):
195+
"""Set configurations for sphinx_book_theme."""
196+
if not config.enable_deepmodeling:
197+
return
198+
if config.html_theme != "sphinx_book_theme":
199+
return
200+
icp_footer = (
201+
'<p><a href="https://beian.miit.gov.cn" target="_blank">%s</a></p>' % icp_no
202+
)
203+
config.html_theme_options["extra_footer"] = (
204+
config.html_theme_options.get("extra_footer", "") + icp_footer
205+
)
206+
207+
191208
def setup(app: Sphinx) -> Dict[str, Any]:
192209
# enable deepmodeling sidebar and icp
193210
# if the repo is outside the deepmodeling, disable it
@@ -202,5 +219,6 @@ def setup(app: Sphinx) -> Dict[str, Any]:
202219
# dark mode for rtd theme
203220
app.connect("config-inited", enable_dark_mode)
204221
app.connect("config-inited", rtd_config)
222+
app.connect("config-inited", sphinx_book_theme)
205223

206224
return {"parallel_read_safe": True}

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sphinx.ext.intersphinx",
1313
"numpydoc",
1414
]
15-
html_theme = "sphinx_rtd_theme"
15+
html_theme = "sphinx_book_theme"
1616

1717

1818
def run_apidoc(_):

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sphinx >=4.2
2-
sphinx_rtd_theme >=0.6
2+
sphinx-book-theme
33
.
44
myst_parser
55
docutils >=0.17

0 commit comments

Comments
 (0)