-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathhtmlconvert.py
More file actions
executable file
·96 lines (84 loc) · 3.11 KB
/
htmlconvert.py
File metadata and controls
executable file
·96 lines (84 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
print("DEBUG: htmlconvert.py is being loaded!")
import os
import re
# Configuration
html_dir = "html"
html_file = f"{html_dir}/portal.html" # The main HTML file
css_file = f"{html_dir}/portal.css" # The CSS file
js_file = f"{html_dir}/portal.js" # The JavaScript file
output_dir = "include" # Output directory for header file
header_name = "html_portal.h" # Name for the header file
def escape_for_cpp(text):
"""Escape text for C++ string literals"""
text = text.replace('\\', '\\\\')
text = text.replace('"', '\\"')
text = text.replace('\n', '\\n"\n"')
return text
def html_to_header():
"""Convert the HTML file to a C++ header file, inlining CSS and JS"""
print("DEBUG: Starting html_to_header conversion")
# Read HTML file
try:
with open(html_file, 'r', encoding='utf-8') as f:
html_content = f.read()
print(f"DEBUG: Successfully read {html_file}")
except Exception as e:
print(f"DEBUG: Error reading HTML file: {e}")
return
# Read CSS file
try:
with open(css_file, 'r', encoding='utf-8') as f:
css_content = f.read()
print(f"DEBUG: Successfully read {css_file}")
except Exception as e:
print(f"DEBUG: CSS file not found, skipping inline: {e}")
css_content = None
# Read JavaScript file
try:
with open(js_file, 'r', encoding='utf-8') as f:
js_content = f.read()
print(f"DEBUG: Successfully read {js_file}")
except Exception as e:
print(f"DEBUG: JS file not found, skipping inline: {e}")
js_content = None
# Inline CSS if found
if css_content:
html_content = html_content.replace(
'<link rel="stylesheet" href="portal.css">',
f'<style>\n{css_content}\n</style>'
)
# Inline JavaScript if found
if js_content:
html_content = html_content.replace(
'<script src="portal.js"></script>',
f'<script>\n{js_content}\n</script>'
)
# Create output directory if it doesn't exist
try:
os.makedirs(output_dir, exist_ok=True)
print(f"DEBUG: Ensured directory {output_dir} exists")
except Exception as e:
print(f"DEBUG: Error creating directory: {e}")
return
# Create header content
header_content = f"""// Generated file - do not edit!
// Source: {html_file}
#pragma once
#include <pgmspace.h>
static const char PORTAL_HTML[] PROGMEM = "{escape_for_cpp(html_content)}";
"""
# Write header file
try:
header_path = os.path.join(output_dir, header_name)
with open(header_path, 'w', encoding='utf-8') as f:
f.write(header_content)
print(f"DEBUG: Successfully generated {header_path}")
print(f"DEBUG: Final HTML size: {len(html_content)} bytes")
print(f"DEBUG: Generated header size: {os.path.getsize(header_path)} bytes")
except Exception as e:
print(f"DEBUG: Error writing header file: {e}")
# Run the conversion
print("DEBUG: About to convert HTML")
Import("env")
html_to_header()