title: HTML Script Css Inliner
This is a simple script I wrote, for [my music exercise generators here](https://john.allsup.co/musicjs/), which take all referenced `` and `` tags and replace them with inline `)")
srcre = re.compile(r"src=(['\"])(.*?)\1")
linkre = re.compile(r"(]*>)")
hrefre = re.compile(r"href=(['\"])(.*?)\1")
def procfile(fn):
try:
with open(fn) as f:
a = f.read()
except Exception as e:
ic(f"Exception reading {x}",type(e),e)
return
orig = a
m = scriptre.findall(a)
scriptsrcs = []
styles = []
dscr = {}
dsty = {}
for y in m:
print(y)
scriptsrcs.append(y)
m = linkre.findall(a)
for y in m:
print(y)
styles.append(y)
for s in scriptsrcs:
m = srcre.search(s)
if not m:
print(f"#fail src {s}")
continue
src = m.group(2)
try:
with open(src) as f:
a = f.read()
dscr[s] = "\n"
except Exception as e:
ic("Exception read src",src,type(e),e)
raise
for s in styles:
m = hrefre.search(s)
if not m:
print(f"#fail href {s}")
continue
src = m.group(2)
try:
with open(src) as f:
a = f.read()
dsty[s] = "\n"
except Exception as e:
ic("Exception read src",src,type(e),e)
raise
tmp = orig
hmap = {}
for k,v in dsty.items():
h = hashlib.sha256()
h.update(k.encode())
h = h.hexdigest()
hmap[h] = v
tmp = tmp.replace(k,h)
for k,v in dscr.items():
h = hashlib.sha256()
h.update(k.encode())
h = h.hexdigest()
hmap[h] = v
tmp = tmp.replace(k,h)
for k,v in hmap.items():
tmp = tmp.replace(k,v)
bn = fn.split("/")[-1]
ofn = f"../m/{bn}"
with open(ofn,"wt") as f:
print(tmp,file=f)
print("Written",ofn)
if __name__ == "__main__":
main()
```