"""Turns an answer written with a toolkit (kits/) into one self-contained file.
three : `import {...} from 'kit3d'` -> reads window.K3; the kit3d module is inserted before the page's module
script and the import map gets 'three' and 'three/addons/' if they are missing;
canvas : the kit2d script (global Kit) is inserted before the page's first script;
html : / """, src, re.S)
if m:
try:
j = json.loads(m.group(1))
imp = j.setdefault("imports", {})
if "three" not in imp or "three/addons/" not in imp:
imp.setdefault("three", THREE_URL)
imp.setdefault("three/addons/", ADDONS_URL)
src = src[:m.start(1)] + json.dumps(j) + src[m.end(1):]
except ValueError:
pass
kit = '\n"
m = re.search(r"""\n"
tag = re.search(r"""""", src, re.I)
if tag:
return src[:tag.start()] + kit + src[tag.end():]
if "Kit." not in src or "window.Kit" in src:
return src
m = re.search(r""
link = re.search(r"""]*href=["'](?:\./)?ui\.css["'][^>]*>""", src, re.I)
if link:
src = src[:link.start()] + css + src[link.end():]
else: # the kit comes first so the page's own