1- # http ://webui.me
1+ # https ://webui.me
22# https://github.com/webui-dev/webui
33# Copyright (c) 2020-2024 Hassan Draga.
44# Licensed under MIT License.
55# All rights reserved.
6+ # Canada.
67#
78# WebUI JavaScript to C Header
89
910def js_to_c_header (input_filename , output_filename ):
1011 try :
1112 # Read JS file content
12- with open (input_filename , 'r' , encoding = 'utf-8' ) as f :
13- content = f .read ()
13+ with open (input_filename , 'r' , encoding = 'utf-8' ) as file_js :
14+ content = file_js .read ()
15+ file_js .close ()
1416
1517 print (f"Converting '{ input_filename } ' to '{ output_filename } '..." )
1618
19+ # comment
20+ comment = (
21+ "// WebUI v2.5.0-beta.2\n "
22+ "// https://webui.me\n "
23+ "// https://github.com/webui-dev/webui\n "
24+ "// Copyright (c) 2020-2024 Hassan Draga.\n "
25+ "// Licensed under MIT License.\n "
26+ "// All rights reserved.\n "
27+ "// Canada.\n \n "
28+ )
29+
1730 # Convert each character in JS content to its hexadecimal value
1831 hex_values = ["0x{:02x}" .format (ord (char )) for char in content ]
1932
2033 # Prepare the content for the C header file
2134 header_content = (
35+ comment +
2236 "// --- PLEASE DO NOT EDIT THIS FILE -------\n "
2337 "// --- THIS FILE IS GENERATED BY JS2C.PY --\n \n "
2438 "#ifndef WEBUI_BRIDGE_H\n "
@@ -29,12 +43,18 @@ def js_to_c_header(input_filename, output_filename):
2943 # Split the hexadecimal values to make the output more readable, adding a new line every 10 values
3044 for i in range (0 , len (hex_values ), 10 ):
3145 header_content += "\n " + ', ' .join (hex_values [i :i + 10 ]) + ','
32-
3346 header_content += "\n 0x00\n };\n \n #endif // WEBUI_BRIDGE_H"
3447
3548 # Write the header content to the output file
36- with open (output_filename , 'w' , encoding = 'utf-8' ) as f :
37- f .write (header_content )
49+ with open (output_filename , 'w' , encoding = 'utf-8' ) as file_h :
50+ file_h .write (header_content )
51+ file_h .close ()
52+
53+ # Add comment to js
54+ new_content = comment + content
55+ with open (input_filename , 'w' ) as file_js :
56+ file_js .write (new_content )
57+ file_js .close ()
3858
3959 except FileNotFoundError :
4060 print (f"Error: File '{ input_filename } ' not found." )
0 commit comments