auto create favicon ico header

This commit is contained in:
lumapu 2022-11-11 23:14:04 +01:00
parent 6cb0b99de3
commit d0e7ee393c
4 changed files with 28 additions and 139 deletions

View file

@ -5,12 +5,11 @@ import glob
from pathlib import Path
def convert2Header(inFile, compress):
def convert2Header(inFile):
fileType = inFile.split(".")[1]
define = inFile.split(".")[0].upper()
define2 = inFile.split(".")[1].upper()
inFileVarName = inFile.replace(".", "_")
print(inFile + ", compress: " + str(compress))
if os.getcwd()[-4:] != "html":
outName = "html/" + "h/" + inFileVarName + ".h"
@ -20,46 +19,36 @@ def convert2Header(inFile, compress):
outName = "h/" + inFileVarName + ".h"
Path("h").mkdir(exist_ok=True)
f = open(inFile, "r")
if fileType == "ico":
f = open(inFile, "rb")
else:
f = open(inFile, "r")
data = f.read()
f.close()
if fileType == "html":
if False == compress:
data = data.replace('\n', '')
data = re.sub(r"\>\s+\<", '><', data) # whitespaces between xml tags
data = re.sub(r"(\r\n|\r|\n)(\s+|\s?)", '', data) # whitespaces inner javascript
length = len(data) # get unescaped length
if False == compress:
data = re.sub(r"\"", '\\\"', data) # escape quotation marks
elif fileType == "js":
#data = re.sub(r"(\r\n|\r|\n)(\s+|\s?)", '', data) # whitespaces inner javascript
#data = re.sub(r"\s?(\=|\!\=|\{|,)+\s?", r'\1', data) # whitespaces inner javascript
length = len(data) # get unescaped length
if False == compress:
data = re.sub(r"\"", '\\\"', data) # escape quotation marks
else:
if fileType == "css":
data = data.replace('\n', '')
data = re.sub(r"(\;|\}|\:|\{)\s+", r'\1', data) # whitespaces inner css
length = len(data) # get unescaped length # get unescaped length
length = len(data)
f = open(outName, "w")
f.write("#ifndef __{}_{}_H__\n".format(define, define2))
f.write("#define __{}_{}_H__\n".format(define, define2))
if compress:
zipped = gzip.compress(bytes(data, 'utf-8'))
zippedStr = ""
for i in range(len(zipped)):
zippedStr += "0x{:02x}".format(zipped[i]) #hex(zipped[i])
if (i + 1) != len(zipped):
zippedStr += ", "
if (i + 1) % 16 == 0 and i != 0:
zippedStr += "\n"
f.write("#define {}_len {}\n".format(inFileVarName, len(zipped)))
f.write("const uint8_t {}[] PROGMEM = {{\n{}}};\n".format(inFileVarName, zippedStr))
if fileType == "ico":
zipped = gzip.compress(bytes(data))
else:
f.write("const char {}[] PROGMEM = \"{}\";\n".format(inFileVarName, data))
f.write("const uint32_t {}_len = {};\n".format(inFileVarName, length))
zipped = gzip.compress(bytes(data, 'utf-8'))
zippedStr = ""
for i in range(len(zipped)):
zippedStr += "0x{:02x}".format(zipped[i]) #hex(zipped[i])
if (i + 1) != len(zipped):
zippedStr += ", "
if (i + 1) % 16 == 0 and i != 0:
zippedStr += "\n"
f.write("#define {}_len {}\n".format(inFileVarName, len(zipped)))
f.write("const uint8_t {}[] PROGMEM = {{\n{}}};\n".format(inFileVarName, zippedStr))
f.write("#endif /*__{}_{}_H__*/\n".format(define, define2))
f.close()
@ -69,17 +58,17 @@ if os.getcwd()[-4:] != "html":
dir = "web/html/" + dir
for f in os.listdir(dir):
if not f.startswith('favicon_ico_gz'):
os.remove(os.path.join(dir, f))
#if not f.startswith('favicon_ico_gz'):
os.remove(os.path.join(dir, f))
# grab all files with following extensions
if os.getcwd()[-4:] != "html":
os.chdir('./web/html')
types = ('*.html', '*.css', '*.js') # the tuple of file types
types = ('*.html', '*.css', '*.js', '*.ico') # the tuple of file types
files_grabbed = []
for files in types:
files_grabbed.extend(glob.glob(files))
# go throw the array
for val in files_grabbed:
convert2Header(val, True)
convert2Header(val)