"""Generate repo/index.html (deployable, China-friendly) from index.html (artifact source)."""
src = open('index.html', encoding='utf-8').read()
i = src.index('</style>') + len('</style>')
head, body = src[:i], src[i:]
# 1. no Google Fonts: system fonts already in the stacks
import re
head = re.sub(r'<link rel="preconnect" href="https://fonts.googleapis.com">\s*', '', head)
head = re.sub(r'<link rel="stylesheet" href="https://fonts.googleapis.com/[^"]*">\s*', '', head)
assert 'fonts.googleapis' not in head
# 2. three.js served from the repo, not from cdnjs / jsDelivr
body = body.replace('<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>',
                    '<script src="vendor/three.min.js"></script>')
body = body.replace('<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>',
                    '<script src="vendor/OrbitControls.js"></script>')
assert 'cdnjs' not in body and 'jsdelivr' not in body
out = ('<!doctype html>\n<html lang="zh-CN">\n<head>\n<meta charset="utf-8">\n'
       '<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">\n'
       + head + '\n</head>\n<body>' + body + '\n</body>\n</html>\n')
open('repo/index.html', 'w', encoding='utf-8').write(out)
print('repo/index.html', len(out))
