76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import json
|
|||
|
|
|
|||
|
|
def read_ehs_project():
|
|||
|
|
"""
|
|||
|
|
读取EHS项目的markdown文件内容
|
|||
|
|
"""
|
|||
|
|
# 项目文件夹路径
|
|||
|
|
project_folder = "网页未导入数据/能源产业/学生完成的项目"
|
|||
|
|
|
|||
|
|
# 列出文件夹中的所有文件
|
|||
|
|
if os.path.exists(project_folder):
|
|||
|
|
files = os.listdir(project_folder)
|
|||
|
|
print(f"找到的文件:{files}")
|
|||
|
|
|
|||
|
|
# 查找EHS相关的文件
|
|||
|
|
for file in files:
|
|||
|
|
if "EHS" in file and file.endswith(".md"):
|
|||
|
|
file_path = os.path.join(project_folder, file)
|
|||
|
|
print(f"\n读取文件:{file_path}")
|
|||
|
|
|
|||
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|||
|
|
content = f.read()
|
|||
|
|
|
|||
|
|
# 输出内容预览
|
|||
|
|
print(f"\n文件内容(前500字符):")
|
|||
|
|
print(content[:500])
|
|||
|
|
print("\n" + "="*50)
|
|||
|
|
|
|||
|
|
# 生成JavaScript代码
|
|||
|
|
generate_js_code(content, file.replace(".md", ""))
|
|||
|
|
return content
|
|||
|
|
else:
|
|||
|
|
print(f"文件夹不存在:{project_folder}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def generate_js_code(content, title):
|
|||
|
|
"""
|
|||
|
|
生成JavaScript代码片段
|
|||
|
|
"""
|
|||
|
|
# 转义特殊字符
|
|||
|
|
content_escaped = content.replace('\\', '\\\\').replace('`', '\\`').replace('${', '\\${')
|
|||
|
|
|
|||
|
|
js_code = f'''
|
|||
|
|
// 可查看的项目数据更新
|
|||
|
|
const clickableProject = {{
|
|||
|
|
title: "{title}",
|
|||
|
|
content: `{content_escaped}`
|
|||
|
|
}};
|
|||
|
|
|
|||
|
|
// 使用示例:
|
|||
|
|
// 在ProjectLibraryPage组件中,将handleCardClick函数中的内容替换为:
|
|||
|
|
// if (projectTitle === "{title}") {{
|
|||
|
|
// Modal.info({{
|
|||
|
|
// title: projectTitle,
|
|||
|
|
// content: (
|
|||
|
|
// <div style={{{{ maxHeight: '60vh', overflowY: 'auto' }}}}>
|
|||
|
|
// <ReactMarkdown>{{clickableProject.content}}</ReactMarkdown>
|
|||
|
|
// </div>
|
|||
|
|
// ),
|
|||
|
|
// width: 800,
|
|||
|
|
// }});
|
|||
|
|
// }}
|
|||
|
|
'''
|
|||
|
|
|
|||
|
|
# 保存到文件
|
|||
|
|
with open("ehs_project_content.js", 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(js_code)
|
|||
|
|
|
|||
|
|
print(f"\nJavaScript代码已生成到:ehs_project_content.js")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
read_ehs_project()
|