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

62 lines
1.9 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 = "网页未导入数据/能源产业/学生完成的项目"
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_sections_in_component():
"""
替换组件中的sections数组
"""
# 读取sections fix文件
with open("ehs_sections_fix_v2.js", 'r', encoding='utf-8') as f:
new_sections = f.read()
# 读取组件文件
component_path = "src/pages/ProjectLibraryPage/index.jsx"
with open(component_path, 'r', encoding='utf-8') as f:
content = f.read()
# 备份文件
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}")
# 查找并替换sections部分
# 找到sections: [ 开始到 ] 结束的内容
import re
# 使用正则表达式匹配整个sections数组
pattern = r'( sections: \[[\s\S]*?\n \])'
matches = re.findall(pattern, content)
if matches:
print(f"找到 {len(matches)} 个sections数组")
# 替换第一个匹配clickableProjects中的sections
content = re.sub(pattern, new_sections, content, count=1)
# 写入文件
with open(component_path, 'w', encoding='utf-8') as f:
f.write(content)
print("✅ 成功替换sections数组")
else:
print("❌ 未找到sections数组")
if __name__ == "__main__":
replace_sections_in_component()