34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
|
|
if __name__ == '__main__':
|
|
# Install cssutils
|
|
import subprocess
|
|
|
|
subprocess.check_call(("pip", "install", "--upgrade", "cssutils"))
|
|
|
|
# Run tool
|
|
import base64
|
|
import cssutils
|
|
import cssutils.scripts
|
|
import mimetypes
|
|
import os
|
|
import sys
|
|
|
|
for i in sys.argv[1:]:
|
|
file = cssutils.parseFile(i)
|
|
|
|
# Inline urls
|
|
def urlInlineFunction(url):
|
|
tmp = os.path.join(os.path.split(i)[0], url)
|
|
try:
|
|
urlType = mimetypes.guess_type(tmp)[0]
|
|
return (b"data:" + urlType.encode() + b";base64," + base64.encodebytes(open(tmp, "rb").read()).replace(b"\n", b"")).decode()
|
|
except Exception as e:
|
|
print("Can't use file: " + repr(url))
|
|
print(e)
|
|
return url
|
|
cssutils.replaceUrls(file, urlInlineFunction, True)
|
|
|
|
with open(i, "wb") as tmp:
|
|
tmp.write(cssutils.scripts.csscombine(path=i, cssText=file.cssText, minify=True))
|