Files
ALL-teach_sys/frontend_视觉设计/clean_resume_formatting.py

100 lines
3.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
清理修改版简历中的删除线内容和加粗符号
- 删除~~删除线内容~~包括符号和内容
- 保留**加粗内容**删除符号保留内容
"""
import re
import shutil
from datetime import datetime
def clean_resume_formatting():
"""清理修改版简历格式"""
file_path = "src/mocks/resumeInterviewMock.js"
# 创建备份
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_path = f"{file_path}.backup_clean_formatting_{timestamp}"
shutil.copy2(file_path, backup_path)
print(f"📦 已创建备份: {backup_path}")
try:
# 读取文件
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
print("🚀 开始清理修改版简历格式...")
# 统计原始符号数量
strikethrough_count = content.count('~~')
bold_count = content.count('**')
print(f"📊 发现删除线符号: {strikethrough_count//2}")
print(f"📊 发现加粗符号: {bold_count//2}")
# 定义清理函数
def clean_content_field(match):
"""清理content字段中的modified内容"""
full_match = match.group(0)
modified_content = match.group(1)
# 1. 删除删除线内容(包括~~符号和其中的内容~~
cleaned = re.sub(r'~~[^~]*~~', '', modified_content)
# 2. 删除加粗符号(保留**中的内容**
cleaned = re.sub(r'\*\*([^*]*)\*\*', r'\1', cleaned)
# 3. 清理多余的空格和标点符号
# 处理可能产生的多余逗号、分号等
cleaned = re.sub(r'[,、;]\s*[,、;]', '', cleaned)
cleaned = re.sub(r'[,、;]\s*$', '', cleaned, flags=re.MULTILINE)
cleaned = re.sub(r'^\s*[,、;]', '', cleaned, flags=re.MULTILINE)
# 4. 清理多余的空白行
cleaned = re.sub(r'\n\s*\n\s*\n', '\n\n', cleaned)
return f'"modified": "{cleaned}"'
# 使用正则表达式找到所有modified字段并清理
modified_pattern = r'"modified":\s*"([^"]*(?:\\.[^"]*)*)"'
processed_content = re.sub(modified_pattern, clean_content_field, content, flags=re.DOTALL)
# 统计处理后的符号数量
final_strikethrough = processed_content.count('~~')
final_bold = processed_content.count('**')
# 写入文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(processed_content)
print(f"📊 处理统计:")
print(f" - 删除线符号: {strikethrough_count//2}{final_strikethrough//2}")
print(f" - 加粗符号: {bold_count//2}{final_bold//2}")
print(f"✅ 已更新 {file_path}")
# 验证JS语法
import subprocess
try:
result = subprocess.run(['node', '-c', file_path], capture_output=True, text=True)
if result.returncode == 0:
print("✅ JavaScript语法验证通过")
else:
print(f"❌ JavaScript语法验证失败: {result.stderr}")
# 恢复备份
shutil.copy2(backup_path, file_path)
print("🔄 已恢复原始文件")
except Exception as e:
print(f"⚠️ 无法验证JavaScript语法: {e}")
except Exception as e:
print(f"❌ 处理失败: {e}")
# 恢复备份
shutil.copy2(backup_path, file_path)
print("🔄 已恢复原始文件")
if __name__ == "__main__":
print("🚀 开始清理修改版简历格式...")
clean_resume_formatting()
print("🎉 处理完成!")