99 lines
2.8 KiB
Python
99 lines
2.8 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import re
|
|||
|
|
|
|||
|
|
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 parse_sections(content):
|
|||
|
|
"""
|
|||
|
|
解析markdown内容,按一级标题拆分成多个板块
|
|||
|
|
"""
|
|||
|
|
# 按 # 一级标题拆分内容
|
|||
|
|
sections = []
|
|||
|
|
|
|||
|
|
# 使用正则表达式找到所有一级标题
|
|||
|
|
pattern = r'^# (.+?)$'
|
|||
|
|
lines = content.split('\n')
|
|||
|
|
|
|||
|
|
current_section = None
|
|||
|
|
current_content = []
|
|||
|
|
|
|||
|
|
for line in lines:
|
|||
|
|
match = re.match(pattern, line)
|
|||
|
|
if match:
|
|||
|
|
# 保存前一个section
|
|||
|
|
if current_section:
|
|||
|
|
sections.append({
|
|||
|
|
'title': current_section,
|
|||
|
|
'content': '\n'.join(current_content).strip()
|
|||
|
|
})
|
|||
|
|
# 开始新的section
|
|||
|
|
current_section = match.group(1).strip().replace('"', "'")
|
|||
|
|
current_content = []
|
|||
|
|
else:
|
|||
|
|
if current_section: # 只有在有当前section时才添加内容
|
|||
|
|
current_content.append(line)
|
|||
|
|
|
|||
|
|
# 保存最后一个section
|
|||
|
|
if current_section:
|
|||
|
|
sections.append({
|
|||
|
|
'title': current_section,
|
|||
|
|
'content': '\n'.join(current_content).strip()
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
return sections
|
|||
|
|
|
|||
|
|
def generate_sections_code():
|
|||
|
|
"""
|
|||
|
|
生成正确格式的sections数组
|
|||
|
|
"""
|
|||
|
|
content, title = read_ehs_content()
|
|||
|
|
if not content:
|
|||
|
|
print("未找到EHS项目文件")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
sections = parse_sections(content)
|
|||
|
|
|
|||
|
|
# 生成JavaScript代码
|
|||
|
|
js_code = ' sections: [\n'
|
|||
|
|
|
|||
|
|
for i, section in enumerate(sections):
|
|||
|
|
# 转义特殊字符
|
|||
|
|
content_escaped = section['content'].replace('\\', '\\\\').replace('`', '\\`').replace('${', '\\${')
|
|||
|
|
|
|||
|
|
js_code += ' {\n'
|
|||
|
|
js_code += f' title: "{section["title"]}",\n'
|
|||
|
|
js_code += f' content: `{content_escaped}`\n'
|
|||
|
|
js_code += ' }'
|
|||
|
|
|
|||
|
|
if i < len(sections) - 1:
|
|||
|
|
js_code += ','
|
|||
|
|
js_code += '\n'
|
|||
|
|
|
|||
|
|
js_code += ' ]'
|
|||
|
|
|
|||
|
|
# 保存到文件
|
|||
|
|
with open("ehs_sections_fix.js", 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(js_code)
|
|||
|
|
|
|||
|
|
print(f"✅ 已生成修复后的sections代码")
|
|||
|
|
print(f"📊 共拆分为 {len(sections)} 个板块:")
|
|||
|
|
for section in sections:
|
|||
|
|
print(f" - {section['title']}")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
generate_sections_code()
|