-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
75 lines (57 loc) · 2.27 KB
/
Copy path__init__.py
File metadata and controls
75 lines (57 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from sphinx.addnodes import document
from sphinx.application import Sphinx
def _get_theme_options(app: Sphinx) -> dict[str, Any] | None:
if hasattr(app.builder, "theme_options"):
return app.builder.theme_options # type: ignore[attr-defined]
if hasattr(app.config, "html_theme_options"):
return app.config.html_theme_options
return None
def update_html_context(
app: Sphinx,
pagename: str,
templatename: str,
context: dict[str, Any],
doctree: document,
) -> None:
theme_options = _get_theme_options(app)
if not theme_options:
return
context["extra_navbar_items"] = theme_options.get("extra_navbar_items")
context["use_page_nav"] = theme_options.get("use_page_nav", True)
context["github_url"] = f"https://github.com/litestar-org/{theme_options['github_repo_name']}"
context["discord_url"] = theme_options.get(
"discord_link",
"https://discord.gg/litestar",
)
context["twitter_url"] = theme_options.get(
"twitter_link",
"https://twitter.com/LitestarAPI",
)
context["reddit_url"] = theme_options.get(
"reddit_link",
"https://www.reddit.com/r/LitestarAPI",
)
def update_global_config(app: Sphinx) -> None:
if not app.config["html_logo"]:
app.config["html_logo"] = "_static/logo-light.png"
if not app.config["html_favicon"]:
app.config["html_favicon"] = "_static/favicon.svg"
theme_options = _get_theme_options(app)
if not theme_options:
return
github_repo_name = theme_options.get("github_repo_name")
if not github_repo_name:
msg = "GitHub URL not provided. Set 'github_repo_name=...' in html_theme_options"
raise ValueError(msg)
def setup(app: Sphinx) -> dict[str, bool]:
theme_path = Path(__file__).parent / "theme"
app.add_html_theme("litestar_sphinx_theme", str(theme_path))
app.connect("html-page-context", update_html_context)
app.connect("builder-inited", update_global_config)
app.add_css_file("litestar-sphinx-theme.css", priority=1000)
app.add_js_file("litestar-theme.js")
return {"parallel_read_safe": True, "parallel_write_safe": True}