"""Rebuild the portable, offline Bramblewatch HTML. Python standard library only."""
from pathlib import Path
import base64,re,json
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT.parent/'Bramblewatch.html'
def data_uri(path):
 mime={'.webp':'image/webp','.svg':'image/svg+xml','.png':'image/png','.jpg':'image/jpeg'}[path.suffix.lower()]
 return f'data:{mime};base64,'+base64.b64encode(path.read_bytes()).decode('ascii')
html=(ROOT/'index.html').read_text()
html=html.replace('href="assets/favicon.svg"','href="'+data_uri(ROOT/'assets/favicon.svg')+'"')
html=html.replace('<link rel="stylesheet" href="src/styles.css">','<style>\n'+(ROOT/'src/styles.css').read_text()+'\n</style>')
manifest={p.stem:data_uri(p) for p in (ROOT/'assets').glob('*.webp')}
for name in ['assets','content','engine','game']:
 code='window.BW_ASSETS = '+json.dumps(manifest,separators=(',',':'))+';' if name=='assets' else (ROOT/'src'/f'{name}.js').read_text()
 html=html.replace(f'<script src="src/{name}.js"></script>','<script>\n'+code.replace('</script','<\\/script')+'\n</script>')
OUT.write_text(html)
print(OUT,OUT.stat().st_size,'bytes')
