Files
ALL-teach_sys/frontend_能源/replace_clickable_projects.py

106 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import datetime
import shutil
def read_ehs_content():
"""
读取EHS项目的完整内容
"""
project_folder = "网页未导入数据/能源产业/学生完成的项目"
# 查找EHS文件
for file in os.listdir(project_folder):
if "EHS" in file and file.endswith(".md"):
file_path = os.path.join(project_folder, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return content, file.replace(".md", "")
return None, None
def replace_clickable_projects():
"""
替换组件中的clickableProjects数组
"""
content, title = read_ehs_content()
if not content:
print("❌ 未找到EHS项目文件")
return
# 转义特殊字符
content_escaped = content.replace('\\', '\\\\').replace('`', '\\`').replace('${', '\\${')
# 生成新的clickableProjects数组
new_clickable_projects = f''' const clickableProjects = [
{{
id: "energy-clickable-1",
name: "{title}",
unitName: "EHS管理体系",
isClickable: true,
content: {{
title: "{title}",
description: "本项目以"安绿一体"EHS管理体系建设与运行示范项目体现了在EHS环境、健康、安全体系中将安全管理与环境管理深度融合的理念。",
images: [],
sections: [
{{
title: "项目详情",
content: `{content_escaped}`
}}
],
relatedPositions: [
{{ name: "EHS工程师", level: "技术骨干岗" }},
{{ name: "安全管理专员", level: "普通岗" }},
{{ name: "环保工程师", level: "技术骨干岗" }}
]
}}
}}
];'''
# 读取组件文件
component_path = "src/pages/ProjectLibraryPage/index.jsx"
with open(component_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# 备份文件
backup_path = f"{component_path}.backup_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}"
shutil.copy(component_path, backup_path)
print(f"✅ 已备份文件到:{backup_path}")
# 查找clickableProjects的起始和结束位置
start_line = -1
end_line = -1
bracket_count = 0
in_array = False
for i, line in enumerate(lines):
if "const clickableProjects = [" in line:
start_line = i
in_array = True
bracket_count = 1
elif in_array:
bracket_count += line.count('[') - line.count(']')
bracket_count += line.count('{') - line.count('}')
if bracket_count == 0 and '];' in line:
end_line = i
break
if start_line != -1 and end_line != -1:
# 替换内容
new_lines = lines[:start_line] + [new_clickable_projects + '\n'] + lines[end_line+1:]
# 写入文件
with open(component_path, 'w', encoding='utf-8') as f:
f.writelines(new_lines)
print(f"✅ 成功替换clickableProjects数组")
print(f"📋 项目标题:{title}")
print(f"📁 替换行数:{start_line+1} - {end_line+1}")
else:
print(f"❌ 未找到clickableProjects数组定义")
print(f" 起始行:{start_line+1 if start_line != -1 else '未找到'}")
print(f" 结束行:{end_line+1 if end_line != -1 else '未找到'}")
if __name__ == "__main__":
replace_clickable_projects()