Skip to content

Commit 52c4200

Browse files
committed
Add theme customization support via docs.yaml
1 parent 7d5ccaa commit 52c4200

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

phosphor/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"github": "",
1313
"favicon": "",
1414
},
15+
"theme": {},
1516
"nav": [],
1617
"pages": [],
1718
}
@@ -32,6 +33,7 @@ def load_config(config_path):
3233
site.update(raw.get("site", {}))
3334
cfg["site"] = site
3435

36+
cfg["theme"] = raw.get("theme", DEFAULTS["theme"])
3537
cfg["nav"] = raw.get("nav", DEFAULTS["nav"])
3638
cfg["pages"] = raw.get("pages", DEFAULTS["pages"])
3739

phosphor/renderer.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ def build_toc_html(headings):
5656
return html
5757

5858

59+
def build_theme_css(theme_config):
60+
"""Build CSS variable overrides from theme config."""
61+
if not theme_config:
62+
return ""
63+
64+
# Map docs.yaml theme keys to CSS custom properties
65+
key_map = {
66+
"accent": "--accent",
67+
"accent_dim": "--accent-dim",
68+
"accent_glow": "--accent-glow",
69+
"accent_glow_strong": "--accent-glow-strong",
70+
"accent_warm": "--accent-warm",
71+
"accent_warm_dim": "--accent-warm-dim",
72+
"accent_red": "--accent-red",
73+
"accent_blue": "--accent-blue",
74+
"accent_purple": "--accent-purple",
75+
"bg_deep": "--bg-deep",
76+
"bg_surface": "--bg-surface",
77+
"bg_raised": "--bg-raised",
78+
"bg_hover": "--bg-hover",
79+
"code_bg": "--code-bg",
80+
"text": "--text",
81+
"text_bright": "--text-bright",
82+
"text_dim": "--text-dim",
83+
"border": "--border",
84+
"border_bright": "--border-bright",
85+
}
86+
87+
overrides = []
88+
for yaml_key, css_var in key_map.items():
89+
if yaml_key in theme_config:
90+
overrides.append(f" {css_var}: {theme_config[yaml_key]};")
91+
92+
if not overrides:
93+
return ""
94+
95+
lines = "\n".join(overrides)
96+
return f"<style>\n :root {{\n{lines}\n }}\n</style>"
97+
98+
5999
def render_page(template, config, page_content, nav_html, page_filename):
60100
"""Render a page by substituting variables into the template."""
61101
site = config["site"]
@@ -84,8 +124,12 @@ def render_page(template, config, page_content, nav_html, page_filename):
84124
f'</a>'
85125
)
86126

127+
# Theme overrides
128+
theme_css = build_theme_css(config.get("theme", {}))
129+
87130
# Substitutions
88131
output = template
132+
output = output.replace("{{THEME_CSS}}", theme_css)
89133
output = output.replace("{{TITLE}}", _escape(page_title))
90134
output = output.replace("{{SITE_TITLE}}", _escape(site["title"]))
91135
output = output.replace("{{TAGLINE}}", _escape(site.get("tagline", "")))

templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<link href="https://fonts.googleapis.com/css2?family=Chakra+Petch:ital,wght@0,400;0,500;0,600;0,700;1,400&family=Nunito+Sans:opsz,wght@6..12,400;6..12,500;6..12,600;6..12,700&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet">
1010
<link rel="icon" type="image/svg+xml" href="{{FAVICON}}">
1111
<link rel="stylesheet" href="assets/style.css">
12+
{{THEME_CSS}}
1213
</head>
1314
<body>
1415
<button class="mobile-toggle" onclick="document.querySelector('.sidebar').classList.toggle('open')" aria-label="Toggle menu"><i data-lucide="menu"></i></button>

0 commit comments

Comments
 (0)