# import os # import json # import re # from modules import outliner_dir, output_dir # # ---------- Helper: sanitize file names ---------- # def safe_filename(name: str) -> str: # return re.sub(r'[\\/*?:"<>|]', "", name).strip() # # ---------- Main function ---------- # def split_json(input_file: str, output_dir: str): # # Load JSON # with open(input_file, "r", encoding="utf-8") as f: # data = json.load(f) # main_headings = data.get("main_headings", []) # sub_headings = data.get("sub_headings", []) # sub_sub_headings = data.get("sub_sub_headings", None) # # Ensure output folder exists # os.makedirs(output_dir, exist_ok=True) # for idx, main in enumerate(main_headings): # headers = sub_headings[idx] if idx < len(sub_headings) else [] # # لو فيه sub_sub_headings نجيبها، لو مش فيه نخليها null # if sub_sub_headings and idx < len(sub_sub_headings): # subs = sub_sub_headings[idx] # else: # subs = None # section = {"headers": headers, "sub_sub_headings": subs} # file_name = safe_filename(main) + ".json" # file_path = os.path.join(output_dir, file_name) # with open(file_path, "w", encoding="utf-8") as out_f: # json.dump(section, out_f, ensure_ascii=False, indent=2) # print(f"✅ Saved {file_name}") # # ---------- Example usage ---------- # if __name__ == "__main__": # split_json("/content/ai_agent_output/step_1_outlines.json", "output_sections")