66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
import json
|
|||
|
|
import re
|
|||
|
|
|
|||
|
|
def escape_for_js(text):
|
|||
|
|
"""转义文本以适合在JavaScript字符串中使用"""
|
|||
|
|
# 转义换行符
|
|||
|
|
text = text.replace('\n', '\\n')
|
|||
|
|
# 转义双引号
|
|||
|
|
text = text.replace('"', '\\"')
|
|||
|
|
# 转义单引号
|
|||
|
|
text = text.replace("'", "\\'")
|
|||
|
|
return text
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
# 读取提取的完整数据
|
|||
|
|
with open('extracted_project_data.json', 'r', encoding='utf-8') as f:
|
|||
|
|
complete_data = json.load(f)
|
|||
|
|
|
|||
|
|
# 读取当前的mock文件
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
|
|||
|
|
lines = f.readlines()
|
|||
|
|
|
|||
|
|
# 逐行处理,寻找并替换overview, process, keyPoints
|
|||
|
|
for i, line in enumerate(lines):
|
|||
|
|
# 检查是否是id行
|
|||
|
|
if '"id":' in line:
|
|||
|
|
# 提取id
|
|||
|
|
id_match = re.search(r'"id":\s*(\d+)', line)
|
|||
|
|
if id_match:
|
|||
|
|
project_id = int(id_match.group(1))
|
|||
|
|
|
|||
|
|
# 查找对应的完整数据
|
|||
|
|
complete_project = next((p for p in complete_data if p['id'] == project_id), None)
|
|||
|
|
if complete_project:
|
|||
|
|
print(f"找到项目 {project_id}: {complete_project['name'][:30]}...")
|
|||
|
|
|
|||
|
|
# 查找并替换后续的overview, process, keyPoints行
|
|||
|
|
for j in range(i+1, min(i+20, len(lines))):
|
|||
|
|
if '"overview":' in lines[j]:
|
|||
|
|
overview = escape_for_js(complete_project['overview'])
|
|||
|
|
lines[j] = f' "overview": "{overview}",\n'
|
|||
|
|
print(f" 更新了overview")
|
|||
|
|
elif '"process":' in lines[j]:
|
|||
|
|
process = escape_for_js(complete_project['process'])
|
|||
|
|
lines[j] = f' "process": "{process}",\n'
|
|||
|
|
print(f" 更新了process")
|
|||
|
|
elif '"keyPoints":' in lines[j]:
|
|||
|
|
keypoints = escape_for_js(complete_project['keypoints'])
|
|||
|
|
# 判断是否是最后一个字段
|
|||
|
|
if lines[j].strip().endswith('"'):
|
|||
|
|
lines[j] = f' "keyPoints": "{keypoints}"\n'
|
|||
|
|
else:
|
|||
|
|
lines[j] = f' "keyPoints": "{keypoints}",\n'
|
|||
|
|
print(f" 更新了keyPoints")
|
|||
|
|
break
|
|||
|
|
|
|||
|
|
# 保存更新后的文件
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
|
|||
|
|
f.writelines(lines)
|
|||
|
|
|
|||
|
|
print("\n✅ Mock文件更新完成!")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|