78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import json
|
||
|
|
import re
|
||
|
|
|
||
|
|
def extract_sections(content):
|
||
|
|
"""从项目内容中提取各个部分"""
|
||
|
|
# 提取项目概述
|
||
|
|
overview_match = re.search(r'# 一、项目概述\s*\n\n(.*?)(?=\n# 二、|\n\n# 二、|$)', content, re.DOTALL)
|
||
|
|
overview = overview_match.group(1).strip() if overview_match else ""
|
||
|
|
|
||
|
|
# 提取项目整体流程介绍
|
||
|
|
process_match = re.search(r'# 二、项目整体流程介绍\s*\n\n(.*?)(?=\n# 三、|\n\n# 三、|$)', content, re.DOTALL)
|
||
|
|
process = process_match.group(1).strip() if process_match else ""
|
||
|
|
|
||
|
|
# 提取项目案例关键技术点
|
||
|
|
keypoints_match = re.search(r'# 三、项目案例关键技术点\s*\n\n(.*?)$', content, re.DOTALL)
|
||
|
|
keypoints = keypoints_match.group(1).strip() if keypoints_match else ""
|
||
|
|
|
||
|
|
# 转义换行符
|
||
|
|
overview = overview.replace('\n', '\\n')
|
||
|
|
process = process.replace('\n', '\\n')
|
||
|
|
keypoints = keypoints.replace('\n', '\\n')
|
||
|
|
|
||
|
|
return overview, process, keypoints
|
||
|
|
|
||
|
|
def main():
|
||
|
|
# 读取原始数据
|
||
|
|
with open('网页未导入数据/化工产业/化工项目案例.json', 'r', encoding='utf-8') as f:
|
||
|
|
data = json.load(f)
|
||
|
|
|
||
|
|
# 读取当前的mock文件
|
||
|
|
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
|
||
|
|
mock_content = f.read()
|
||
|
|
|
||
|
|
# 处理每个项目
|
||
|
|
for i, project in enumerate(data):
|
||
|
|
project_id = i + 1
|
||
|
|
content = project['项目案例内容']
|
||
|
|
overview, process, keypoints = extract_sections(content)
|
||
|
|
|
||
|
|
# 如果内容为空,跳过
|
||
|
|
if not overview and not process and not keypoints:
|
||
|
|
print(f"项目 {project_id} ({project['案例名称']}) 内容为空,跳过")
|
||
|
|
continue
|
||
|
|
|
||
|
|
print(f"处理项目 {project_id}: {project['案例名称']}")
|
||
|
|
print(f" 概述长度: {len(overview)}")
|
||
|
|
print(f" 流程长度: {len(process)}")
|
||
|
|
print(f" 技术点长度: {len(keypoints)}")
|
||
|
|
|
||
|
|
# 查找并替换项目的overview
|
||
|
|
if overview:
|
||
|
|
# 使用更精确的模式匹配
|
||
|
|
pattern = rf'("id": {project_id},.*?"overview": )"[^"]*"'
|
||
|
|
replacement = rf'\1"{overview}"'
|
||
|
|
mock_content = re.sub(pattern, replacement, mock_content, flags=re.DOTALL)
|
||
|
|
|
||
|
|
# 查找并替换项目的process
|
||
|
|
if process:
|
||
|
|
pattern = rf'("id": {project_id},.*?"process": )"[^"]*"'
|
||
|
|
replacement = rf'\1"{process}"'
|
||
|
|
mock_content = re.sub(pattern, replacement, mock_content, flags=re.DOTALL)
|
||
|
|
|
||
|
|
# 查找并替换项目的keyPoints
|
||
|
|
if keypoints:
|
||
|
|
pattern = rf'("id": {project_id},.*?"keyPoints": )"[^"]*"'
|
||
|
|
replacement = rf'\1"{keypoints}"'
|
||
|
|
mock_content = re.sub(pattern, replacement, mock_content, flags=re.DOTALL)
|
||
|
|
|
||
|
|
# 保存更新后的文件
|
||
|
|
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(mock_content)
|
||
|
|
|
||
|
|
print("\n✅ 所有项目详情已更新完成!")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|