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

113 lines
3.3 KiB
Python
Raw Normal View History

#!/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 = []
skip_first = True # 跳过第一个标题(项目名称)
for line in lines:
match = re.match(pattern, line)
if match:
# 保存前一个section
if current_section and not skip_first:
content_text = '\n'.join(current_content).strip()
if content_text: # 只添加有内容的板块
sections.append({
'title': current_section,
'content': content_text
})
# 开始新的section
current_section = match.group(1).strip().replace('"', "'")
current_content = []
if skip_first:
skip_first = False
current_section = None # 跳过第一个标题
else:
if current_section: # 只有在有当前section时才添加内容
current_content.append(line)
# 保存最后一个section
if current_section:
content_text = '\n'.join(current_content).strip()
if content_text:
sections.append({
'title': current_section,
'content': content_text
})
# 清理标题中的多余星号
for section in sections:
section['title'] = section['title'].replace('**', '').replace('*', '')
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_v2.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()