import argparse import os import re parser = argparse.ArgumentParser(description="Embed a JS file as a C string header") parser.add_argument("--input", required=True, help="Path to input JS file") parser.add_argument("--output", required=True, help="Path to output .h file") parser.add_argument("--name", help="C identifier name (default: derived from filename)") args = parser.parse_args() if args.name: name = args.name else: stem = os.path.splitext(os.path.basename(args.input))[0] name = re.sub(r"[^a-zA-Z0-9]", "_", stem).upper() + "_JS" with open(args.input, "r", encoding="utf-8") as f: source = f.read() def escape_line(s): s = s.replace("\\", "\\\\") s = s.replace('"', '\\"') s = s.replace("\r", "\\r") s = s.replace("\t", "\\t") return s lines = source.split("\n") if lines and lines[-1] == "": lines = lines[:-1] out = [ "#pragma once", "#include ", "", f"static const char {name}[] =", ] if not lines: out[-1] += ' "";' else: for i, line in enumerate(lines): suffix = ";" if i == len(lines) - 1 else "" out.append(f' "{escape_line(line)}\\n"{suffix}') out += [ "", f"static const size_t {name}_SIZE = sizeof({name}) - 1;", "", ] os.makedirs(os.path.dirname(args.output), exist_ok=True) with open(args.output, "w", encoding="utf-8") as f: f.write("\n".join(out))