Files
ALL-teach_sys/frontend_化工/final_fix_project.py

122 lines
4.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import re
def escape_for_js(text):
"""完全转义文本以适合在JavaScript字符串中使用"""
# 首先转义反斜杠
text = text.replace('\\', '\\\\')
# 转义双引号
text = text.replace('"', '\\"')
# 转义换行符
text = text.replace('\n', '\\n')
# 转义回车符
text = text.replace('\r', '\\r')
# 转义制表符
text = text.replace('\t', '\\t')
return text
def main():
# 读取完整的项目数据
with open('complete_project_data.json', 'r', encoding='utf-8') as f:
complete_data = json.load(f)
# 读取当前的mock文件
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
lines = f.readlines()
# 处理每一行
new_lines = []
i = 0
while i < len(lines):
line = lines[i]
# 检查是否是项目ID行
if '"id":' in line:
# 提取ID
id_match = re.search(r'"id":\s*(\d+)', line)
if id_match:
project_id = int(id_match.group(1))
# 查找对应的完整数据
complete_project = next((p for p in complete_data if p['id'] == project_id), None)
if complete_project:
# 添加当前行
new_lines.append(line)
i += 1
# 处理后续的行查找并替换overview, process, keyPoints
replaced = {'overview': False, 'process': False, 'keyPoints': False}
while i < len(lines) and not all(replaced.values()):
current_line = lines[i]
if '"overview":' in current_line and not replaced['overview']:
# 替换overview行
overview = escape_for_js(complete_project['overview'])
indent = ' '
new_lines.append(f'{indent}"overview": "{overview}",\n')
replaced['overview'] = True
i += 1
elif '"process":' in current_line and not replaced['process']:
# 替换process行
process = escape_for_js(complete_project['process'])
indent = ' '
new_lines.append(f'{indent}"process": "{process}",\n')
replaced['process'] = True
i += 1
elif '"keyPoints":' in current_line and not replaced['keyPoints']:
# 替换keyPoints行
keypoints = escape_for_js(complete_project['keypoints'])
indent = ' '
# 检查是否是最后一个字段
if current_line.rstrip().endswith('"'):
new_lines.append(f'{indent}"keyPoints": "{keypoints}"\n')
else:
new_lines.append(f'{indent}"keyPoints": "{keypoints}",\n')
replaced['keyPoints'] = True
i += 1
else:
new_lines.append(current_line)
i += 1
else:
new_lines.append(line)
i += 1
else:
new_lines.append(line)
i += 1
else:
new_lines.append(line)
i += 1
# 保存更新后的文件
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
f.writelines(new_lines)
print("✅ 项目详情已完整更新")
# 验证JavaScript语法
import subprocess
result = subprocess.run(['node', '-c', 'src/mocks/projectLibraryMock.js'],
capture_output=True, text=True)
if result.returncode == 0:
print("✅ JavaScript语法检查通过")
else:
print(f"❌ JavaScript语法错误:\n{result.stderr}")
# 显示错误位置附近的内容
error_match = re.search(r':(\d+):', result.stderr)
if error_match:
error_line = int(error_match.group(1))
print(f"\n错误位置附近的内容(行 {error_line-2}{error_line+2}:")
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
all_lines = f.readlines()
for line_num in range(max(0, error_line-3), min(len(all_lines), error_line+2)):
print(f"{line_num+1:4}: {all_lines[line_num].rstrip()}")
if __name__ == "__main__":
main()