diff --git a/gen.sh b/gen.sh new file mode 100755 index 0000000..8eaac81 --- /dev/null +++ b/gen.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# Install updates +pip3 install --upgrade --user pip && +pip3 install --upgrade --user pelican markdown htmlmin && + +# Clean and generate +(rm -rf output || true) && +python3 -c "import pelican; pelican.main()" content && + +# Optimize +tools/cssfix.py output/theme/css/* && +( + for i in `find output -wholename "*.html"` + do + htmlmin -c -s "$i" "$i" || exit 1 + done +) && + +# Compress +( + for i in `find output -wholename "*.css" -or -wholename "*.js" -or -wholename "*.html"` + do + (gzip --best --keep "$i" && brotli --best --keep "$i") || + (echo ERROR in file $i; exit 1) || exit 1 + done +) diff --git a/tools/cssfix.py b/tools/cssfix.py new file mode 100755 index 0000000..c987bf9 --- /dev/null +++ b/tools/cssfix.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +if __name__ == '__main__': + # Install cssutils + import subprocess + + subprocess.check_call(("pip", "install", "--upgrade", "--user", "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))