Repository navigation
Expand file tree
/
Copy pathexternalize.py
More file actions
40 lines (33 loc) · 1.52 KB
/
Copy pathexternalize.py
File metadata and controls
40 lines (33 loc) · 1.52 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
import re
with open("index.html", "r", encoding="utf-8") as f:
content = f.read()
# Extract CSS
css_match = re.search(r"<style>(.*?)</style>", content, re.DOTALL)
if css_match:
css_content = css_match.group(1).strip()
with open("style.css", "w", encoding="utf-8") as f:
f.write(css_content)
content = content.replace(css_match.group(0), '<link rel="stylesheet" href="style.css">')
# Extract JS
# We need to be careful with multiple script tags.
# The main game script is at the end.
script_matches = re.findall(r"<script>(.*?)</script>", content, re.DOTALL)
if script_matches:
# Combine all scripts or just the last one?
# Usually the last one is the main logic.
# The PWA registration is also a script.
full_js = "\n\n".join([s.strip() for s in script_matches])
with open("app.js", "w", encoding="utf-8") as f:
f.write(full_js)
# Replace all script tags with one link
# This is a bit aggressive but clean.
content = re.sub(r"<script>.*?</script>", "", content, flags=re.DOTALL)
content = content.replace("</body>", '<script src="app.js"></script>\n</body>')
# One final check for the inline style in JS string
content = content.replace('style="opacity:0.6"', 'class="dimmed"')
# And add .dimmed to style.css if it's not there
with open("style.css", "a", encoding="utf-8") as f:
f.write("\n.dimmed { opacity: 0.6; }")
with open("index.html", "w", encoding="utf-8") as f:
f.write(content)
print("Moved CSS and JS to external files and removed remaining inline styles.")