-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
67 lines (54 loc) · 2.59 KB
/
build.py
File metadata and controls
67 lines (54 loc) · 2.59 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
#!/usr/bin/env python3
"""
NukeMap Build Script - Bundles all files into a single offline HTML.
Usage: python build.py
Output: NukeMap-offline.html (fully self-contained)
"""
import os, re, urllib.request, sys
ROOT = os.path.dirname(os.path.abspath(__file__))
def read(path):
with open(os.path.join(ROOT, path), 'r', encoding='utf-8') as f:
return f.read()
def download(url):
print(f' Downloading {url}...')
try:
req = urllib.request.Request(url, headers={'User-Agent': 'NukeMap-Builder/1.0'})
with urllib.request.urlopen(req, timeout=30) as r:
return r.read().decode('utf-8')
except Exception as e:
print(f' Warning: Could not download {url}: {e}')
return None
def build():
print('NukeMap Offline Builder v3.0.0')
print('=' * 40)
# Read HTML template
html = read('index.html')
# Download CDN resources
leaflet_css = download('https://unpkg.com/leaflet@1.9.4/dist/leaflet.css') or '/* Leaflet CSS unavailable */'
leaflet_js = download('https://unpkg.com/leaflet@1.9.4/dist/leaflet.js') or '/* Leaflet JS unavailable */'
three_js = download('https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js') or '/* Three.js unavailable */'
# Read local files
css = read('css/styles.css')
js_files = ['data.js','physics.js','search.js','effects.js','animation.js','sound.js',
'mushroom3d.js','mirv.js','shelter.js','compare.js','heatmap.js','app.js']
js_all = '\n'.join(read(f'js/{f}') for f in js_files)
# Replace CDN links with inline
html = re.sub(r'<link rel="stylesheet" href="https://unpkg\.com/leaflet[^"]*"[^/]*/>', f'<style>{leaflet_css}</style>', html)
html = re.sub(r'<link rel="stylesheet" href="css/styles\.css"\s*/>', f'<style>{css}</style>', html)
html = re.sub(r'<script src="https://unpkg\.com/leaflet[^"]*"[^>]*></script>', f'<script>{leaflet_js}</script>', html)
html = re.sub(r'<script src="https://cdnjs\.cloudflare[^"]*"[^>]*></script>', f'<script>{three_js}</script>', html)
# Replace local JS includes with inline
for f in js_files:
html = re.sub(rf'<script src="js/{re.escape(f)}"></script>', '', html)
# Add all JS before </body>
html = html.replace('</body>', f'<script>\n{js_all}\n</script>\n</body>')
# Write output
out_path = os.path.join(ROOT, 'NukeMap-offline.html')
with open(out_path, 'w', encoding='utf-8') as f:
f.write(html)
size_kb = os.path.getsize(out_path) / 1024
print(f'\nBuilt: {out_path}')
print(f'Size: {size_kb:.0f} KB')
print('Done!')
if __name__ == '__main__':
build()