#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import re def create_js_template_string(text): """ 创建JavaScript模板字符串格式,用反引号包围以支持多行 """ if not text: return '``' # 转义反引号和反斜杠 text = text.replace('\\', '\\\\') text = text.replace('`', '\\`') text = text.replace('${', '\\${') # 转义模板字符串插值 return f'`{text}`' def extract_sections_from_content(content): """ 从项目案例内容中提取overview、process、keyPoints等部分 """ # 提取一、项目概述部分 overview_match = re.search(r'# 一、项目概述\n\n(.*?)(?=\n# 二、)', content, re.DOTALL) overview = overview_match.group(1).strip() if overview_match else "" # 提取二、项目整体流程介绍部分 process_match = re.search(r'# 二、项目整体流程介绍\n\n(.*?)(?=\n# 三、)', content, re.DOTALL) process = process_match.group(1).strip() if process_match else "" # 提取三、项目案例关键技术点部分 keypoints_match = re.search(r'# 三、项目案例关键技术点\n\n(.*)', content, re.DOTALL) keypoints = keypoints_match.group(1).strip() if keypoints_match else "" return overview, process, keypoints def fix_project_details_final(): """ 最终修复projectLibraryMock.js文件中的项目详情数据 """ # 读取能源项目案例数据 with open("网页未导入数据/能源产业/能源项目案例.json", 'r', encoding='utf-8') as f: energy_projects = json.load(f) # 转换项目详情数据 converted_details = [] for idx, project in enumerate(energy_projects, 1): # 提取项目案例内容的各个部分 content = project.get("项目案例内容", "") overview, process, keypoints = extract_sections_from_content(content) # 处理岗位信息 resume_names = project.get("对应个人简历名称", "").split(",") positions = [] for name in resume_names: name = name.strip() if name: positions.append({ "level": "技术骨干岗", "position": name }) if not positions: positions = [{"level": "技术骨干岗", "position": "能源工程师"}] # 提取对应单元名称 vertical_units = project.get("对应单元名称(垂直能力课)", "").split(",") unit = vertical_units[0].strip() if vertical_units else "能源技术基础" # 使用模板字符串处理文本内容 safe_overview = create_js_template_string(overview) if overview else '`本项目聚焦能源产业核心技术领域,通过系统化的技术实施和管理优化,实现高效、安全、可持续的能源系统运营。`' safe_process = create_js_template_string(process) if process else '`### 流程一:前期准备与需求分析\n\n1. 进行全面的技术需求调研和可行性分析\n2. 制定详细的项目实施方案和时间计划\n\n### 流程二:系统设计与优化\n\n1. 完成核心技术方案设计\n2. 进行系统集成和性能优化\n\n### 流程三:实施与验收\n\n1. 按计划实施项目各项内容\n2. 完成系统测试和项目验收`' safe_keypoints = create_js_template_string(keypoints) if keypoints else '`### (一)核心技术应用\n\n1. 应用先进的能源技术和管理方法\n2. 实现系统的高效稳定运行\n\n### (二)安全管理体系\n\n1. 建立完善的安全管理制度\n2. 确保项目实施过程的安全可控\n\n### (三)持续优化改进\n\n1. 建立持续改进机制\n2. 不断提升系统性能和效益`' converted_detail = { "id": idx, "name": project["案例名称"], "positions": positions, "unit": unit, "overview": safe_overview, "process": safe_process, "keyPoints": safe_keypoints } converted_details.append(converted_detail) # 读取现有Mock文件 with open("src/mocks/projectLibraryMock.js", 'r', encoding='utf-8') as f: content = f.read() # 生成新的项目详情数据JavaScript格式 details_js = "[\n" for detail in converted_details: # 处理positions数组 positions_str = "" for pos in detail['positions']: positions_str += f' {{\n "level": "{pos["level"]}",\n "position": "{pos["position"]}"\n }},\n' positions_str = positions_str.rstrip(',\n') details_js += f''' {{ "id": {detail['id']}, "name": "{detail['name']}", "positions": [ {positions_str} ], "unit": "{detail['unit']}", "overview": {detail['overview']}, "process": {detail['process']}, "keyPoints": {detail['keyPoints']} }}, ''' details_js = details_js.rstrip(',\n') + "\n ]" # 替换项目详情数组,查找getMockProjectDetail函数中的projects数组 pattern = r'(export const getMockProjectDetail = \(id\) => \{\s*// 直接根据ID返回对应项目的详情\s*const projects = )\[[\s\S]*?\];' replacement = f'\\g<1>{details_js};' updated_content = re.sub(pattern, replacement, content) # 写入更新后的内容 with open("src/mocks/projectLibraryMock.js", 'w', encoding='utf-8') as f: f.write(updated_content) print(f"✅ 最终修复完成,已使用模板字符串处理 {len(converted_details)} 个能源项目详情") if __name__ == "__main__": fix_project_details_final()