From b1480a93d9df411ff59084e52b5ed84ee1888d91 Mon Sep 17 00:00:00 2001 From: rzeldent Date: Mon, 19 Sep 2022 12:51:53 +0200 Subject: [PATCH] Add files via upload --- html_to_cpp_gzip.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 html_to_cpp_gzip.py diff --git a/html_to_cpp_gzip.py b/html_to_cpp_gzip.py new file mode 100644 index 0000000..8ccdf12 --- /dev/null +++ b/html_to_cpp_gzip.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 + +import os +import sys +import htmlmin +import gzip + +if (len(sys.argv) <= 2): + print('Usage: bin_to_cpp_gzip.py ') + sys.exit(1) + +input_dir = sys.argv[1] +file_h = sys.argv[2] + +file_names = os.listdir(input_dir) +file_names = filter(lambda x: x[0] != '.' and +os.path.isfile(os.path.join(input_dir, x)), file_names) +file_names = sorted(file_names) + +output_file = open(file_h, 'w') +output_file.write( + '//*******************************************************************************\n' + '// HTML import gzipped\n' + '// Machine generated file\n' + '// ******************************************************************************\n' + '\n\n') + +for file_name in file_names: + print(f'Processing: {file_name}... ') + + file_path = os.path.join(input_dir, file_name) + file_data_name = f'file_data_{file_name}'.replace('.', '_') + + file = open(file_path, 'r') + html = file.read() + file.close() + + html_mimified = htmlmin.minify(html, remove_empty_space=True) + html_mimified_gzip = gzip.compress(bytes(html_mimified, 'utf-8')) + html_mimified_gzip_values = ','.join(f'0x{i:02x}' for x in html_mimified_gzip) + + output_file.write(f'constexpr unsigned char {file_data_name}[] = {{{html_mimified_gzip_values}}};\n\n') + +output_file.close() + +print('Done.')