ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/helper_scripts/html2cpp.py

80 lines
3.4 KiB
Python
Raw Permalink Normal View History

2024-01-01 15:13:54 +00:00
#!/usr/bin/env python
2024-01-01 09:38:14 +00:00
import os
import re
html_files = []
directory = "lib/ESPMegaPRO/html"
for filename in os.listdir(directory):
if filename.endswith(".html"):
html_files.append(os.path.join(directory, filename))
# Iterate over the list of html files
for html_file in html_files:
# Open the html file
f = open(html_file, "r")
# Read the content of the html file
html_content = f.read()
# Close the html file
f.close()
2024-05-19 05:24:57 +00:00
# Section Disabled, Using client side JS to replace variables
2024-01-01 09:38:14 +00:00
# First, replace % with %%
2024-05-19 05:24:57 +00:00
#html_content = html_content.replace("%", "%%")
2024-01-01 09:38:14 +00:00
# The html file contains placeholders for the variables
# These placeholders are in the form of $(variable_name)$
# Replace these with %variable_name%
2024-05-19 05:24:57 +00:00
#html_content = re.sub(r"\$\((.*?)\)\$", r"%\1%", html_content)
2024-01-01 09:38:14 +00:00
# Create the temp folder if it doesn't exist
2024-05-19 05:24:57 +00:00
2024-01-01 09:38:14 +00:00
if not os.path.exists("temp"):
os.makedirs("temp")
# Save the new content to a new file in the temp directory
# The new file will have the same name as the original file
nf = open("temp/" + os.path.basename(html_file), "w")
nf.write(html_content)
nf.close()
# Use xxd to convert the html file to a C++ file
# The C++ file will have the same name as the original file but with a .h extension
# Save the C++ file in lib/ESPMegaPRO/html
2024-01-02 06:22:00 +00:00
os.system("xxd -i temp/" + os.path.basename(html_file) + " > lib/ESPMegaPRO/" + os.path.basename(html_file).replace(".html", "_html.h"))
2024-01-01 09:38:14 +00:00
# Now delete the temp file
os.remove("temp/" + os.path.basename(html_file))
# Next we open the C++ file
2024-01-02 06:22:00 +00:00
f = open("lib/ESPMegaPRO/" + os.path.basename(html_file).replace(".html", "_html.h"), "r")
2024-01-01 09:38:14 +00:00
# Read the content of the C++ file
cpp_content = f.read()
# Close the C++ file
f.close()
# By default, xxd will create a unsigned char array
# We need to change this to a const char array
# We also need to add a null terminator at the end of the array
# Additionally we want to place the array in PROGMEM
# Ex. From unsigned char index_html[] = { to const char index_html[] PROGMEM = {
# We will use regex to do this
cpp_content = re.sub(r"unsigned char (.*?)\[\] = \{", r"const char \1[] PROGMEM = {", cpp_content)
# Add null terminator
cpp_content = cpp_content.replace("};", ", 0x00};")
# Lastly, we do not need the length of the array so we remove it
# Ex. From unsigned int index_html_len = 1000; to
cpp_content = re.sub(r"unsigned int (.*?)_len = (.*?);", r"", cpp_content)
# Change the name of the variable to <filename>_html where <filename> is the name of the html file
filename = os.path.basename(html_file).replace(".html", "")
cpp_content = re.sub(r"const char (.*?)\[\] PROGMEM = {", r"const char " + filename + r"_html[] PROGMEM = {", cpp_content)
# Reopen the C++ file
2024-01-02 06:22:00 +00:00
f = open("lib/ESPMegaPRO/" + os.path.basename(html_file).replace(".html", "_html.h"), "w")
2024-01-01 09:38:14 +00:00
# Write the new content to the C++ file
f.write(cpp_content)
# Close the C++ file
f.close()
# Lastly, we need to create a header file that includes all the html files
# Open the header file
2024-01-02 06:22:00 +00:00
f = open("lib/ESPMegaPRO/all_html.h", "w")
2024-01-01 09:38:14 +00:00
# Write the pragma once directive
f.write("#pragma once\n")
# Write the includes for all the html files
for html_file in html_files:
2024-01-02 06:22:00 +00:00
f.write("#include \"" + os.path.basename(html_file).replace(".html", "_html.h") + "\"\n")
2024-01-01 09:38:14 +00:00
# Close the header file