-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·73 lines (60 loc) · 2.34 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·73 lines (60 loc) · 2.34 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
#!/usr/bin/env python3
import os
import glob
import sys
def inject_with_indent(template, placeholder, content):
"""
Finds the placeholder in the template, calculates the indentation
on that line, and indents every line of content to match.
"""
lines = template.splitlines()
final_lines = []
for line in lines:
if placeholder in line:
# Get the leading whitespace before the placeholder
indent = line[:line.find(placeholder)]
# Split content into lines, indent them, and join them
content_lines = content.strip().splitlines()
indented_content = "\n".join([f"{indent}{c_line}" if c_line.strip() else "" for c_line in content_lines])
final_lines.append(indented_content)
else:
final_lines.append(line)
return "\n".join(final_lines)
def main():
SOURCE_FILE = "index.src.html"
OUTPUT_FILE = "index.html"
CSS_FILE = "styles.css"
SCRIPTS_DIR = "scripts"
try:
if not os.path.exists(SOURCE_FILE):
print(f"❌ Error: {SOURCE_FILE} not found.")
return
with open(SOURCE_FILE, "r", encoding="utf-8") as f:
html = f.read()
# 1. Process CSS
if os.path.exists(CSS_FILE):
with open(CSS_FILE, "r", encoding="utf-8") as f:
css_text = f.read()
html = inject_with_indent(html, "/* %%% styles.css %%% */", css_text)
else:
print(f"⚠️ Warning: {CSS_FILE} missing.")
# 2. Process JS
script_files = sorted(glob.glob(os.path.join(SCRIPTS_DIR, "*.js")))
if script_files:
js_parts = []
for f_path in script_files:
with open(f_path, "r", encoding="utf-8") as f:
js_parts.append(f.read().strip())
combined_js = "\n\n".join(js_parts)
html = inject_with_indent(html, "/* %%% scripts %%% */", combined_js)
else:
print(f"⚠️ Warning: No JS found in {SCRIPTS_DIR}/")
# 3. Write Output
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write(html)
print(f"✅ Built {OUTPUT_FILE} with correct indentation.")
except Exception as e:
print(f"❌ Build failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()