()
| 93 | |
| 94 | |
| 95 | def main(): |
| 96 | # 保持原始的环境变量读取逻辑 |
| 97 | input_dir = Path(os.environ.get('CONTENT_POSTPROCESSOR_INPUT', 'input')) |
| 98 | output_dir = Path(os.environ.get('CONTENT_POSTPROCESSOR_OUTPUT', 'output')) |
| 99 | os.makedirs(input_dir, exist_ok=True) |
| 100 | os.makedirs(output_dir, exist_ok=True) |
| 101 | |
| 102 | # 获取所有 JSON 文件并自然排序 |
| 103 | json_files = sorted(input_dir.glob("*.json"), key=natural_sort_key) |
| 104 | |
| 105 | combined_data = [] |
| 106 | book_title = "booktitle" # 默认书名 |
| 107 | |
| 108 | # 找到页码最小的_page_x_merged.json 文件以确定书名 |
| 109 | min_page_file = find_min_page_file(json_files) |
| 110 | if min_page_file: |
| 111 | # 需要从完整路径对象中获取文件名进行提取 |
| 112 | target_path = next((f for f in json_files if f.name == min_page_file), None) |
| 113 | if target_path: |
| 114 | book_title = extract_book_title(target_path.name) |
| 115 | else: |
| 116 | # 如果没找到匹配的文件,从任意一个文件中提取 |
| 117 | for file_path in json_files: |
| 118 | # 排除干扰文件 |
| 119 | if file_path.name not in ["file_info.json", "combined_output.json"]: |
| 120 | book_title = extract_book_title(file_path.name) |
| 121 | break |
| 122 | |
| 123 | # 读取所有 JSON 文件数据 |
| 124 | for file_path in json_files: |
| 125 | # 【关键修复】显式排除 file_info.json 和可能遗留的 combined_output.json |
| 126 | # 防止因输入目录被污染而导致数据重复合并 |
| 127 | if file_path.name not in ["file_info.json", "combined_output.json"]: |
| 128 | data = read_json_file(file_path) |
| 129 | if data: |
| 130 | combined_data.extend(data) |
| 131 | |
| 132 | # 【新增功能】归一化标题层级 |
| 133 | combined_data = normalize_levels(combined_data) |
| 134 | |
| 135 | # 直接保存最终结果到输出目录 |
| 136 | final_output_file = output_dir / f"{book_title}_final.json" |
| 137 | with open(final_output_file, 'w', encoding='utf-8') as f: |
| 138 | json.dump(combined_data, f, ensure_ascii=False, indent=2) |
| 139 | |
| 140 | print(f"处理完成,结果已保存至:{final_output_file}") |
| 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
no test coverage detected