-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_multilang.py
More file actions
executable file
·89 lines (74 loc) · 2.96 KB
/
serve_multilang.py
File metadata and controls
executable file
·89 lines (74 loc) · 2.96 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
"""
Multi-language development server for omegaUp documentation.
This server properly serves all language versions from the site/ directory,
allowing language switching to work correctly during local development.
"""
import http.server
import re
import socketserver
import sys
from pathlib import Path
ROOT = Path(__file__).parent.resolve()
SITE_DIR = ROOT / "site"
def _warn_if_favicon_href_mismatch():
"""Partial single-language rebuilds leave other site/<lang>/ trees stale (wrong favicon, assets)."""
hrefs = {}
for lang in ("en", "es", "pt", "pt-BR"):
idx = SITE_DIR / lang / "index.html"
if not idx.is_file():
continue
text = idx.read_text(encoding="utf-8", errors="replace")
for tag in re.finditer(r"<link\b[^>]*>", text, re.IGNORECASE):
t = tag.group(0)
if not re.search(r'rel\s*=\s*["\']icon["\']', t, re.IGNORECASE):
continue
hm = re.search(r'href\s*=\s*(["\'])([^"\']+)\1', t, re.IGNORECASE)
if hm:
hrefs[lang] = hm.group(2)
break
unique = set(hrefs.values())
if len(unique) > 1:
print(
"Warning: <link rel=\"icon\"> href differs between languages (stale site/ output).\n"
" Run: python3 build_all.py\n"
f" Seen: {hrefs}"
)
if not SITE_DIR.exists():
print(f"Error: {SITE_DIR} does not exist.")
print("Please build the site first using: zensical build")
sys.exit(1)
_warn_if_favicon_href_mismatch()
PORT = 8000
class MultiLangHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
"""Serves site/ by absolute path so rebuilds (rmtree site/) do not break os.getcwd()."""
def __init__(self, request, client_address, server):
super().__init__(
request,
client_address,
server,
directory=str(SITE_DIR.resolve()),
)
def do_GET(self):
# Redirect root to /en/
if self.path == "/" or self.path == "":
self.send_response(302)
self.send_header("Location", "/en/")
self.end_headers()
return
return super().do_GET()
if __name__ == "__main__":
with socketserver.TCPServer(("", PORT), MultiLangHTTPRequestHandler) as httpd:
print(f"🌍 Multi-language documentation server running at http://localhost:{PORT}/")
print(f"📂 Serving from: {SITE_DIR}")
print(f"\n Available languages:")
print(f" • English: http://localhost:{PORT}/en/")
print(f" • Español: http://localhost:{PORT}/es/")
print(f" • Português: http://localhost:{PORT}/pt/")
print(f" • Português (Brasil): http://localhost:{PORT}/pt-BR/")
print(f"\nPress Ctrl+C to stop the server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n\n👋 Server stopped")
sys.exit(0)