fix_bold_format.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 批量修复Markdown文件中的加粗格式
  5. 将 **文本** 替换为 <strong>文本</strong>
  6. """
  7. import re
  8. import os
  9. import glob
  10. def fix_bold_format_in_file(file_path):
  11. """修复单个文件中的加粗格式"""
  12. try:
  13. with open(file_path, 'r', encoding='utf-8') as f:
  14. content = f.read()
  15. # 使用正则表达式匹配 **文本** 并替换为 <strong>文本</strong>
  16. # 确保不匹配已经是HTML标签的情况
  17. pattern = r'\*\*([^*]+?)\*\*'
  18. replacement = r'<strong>\1</strong>'
  19. # 执行替换
  20. new_content = re.sub(pattern, replacement, content)
  21. # 如果内容有变化,写回文件
  22. if new_content != content:
  23. with open(file_path, 'w', encoding='utf-8') as f:
  24. f.write(new_content)
  25. print(f"✅ 已修复: {file_path}")
  26. return True
  27. else:
  28. print(f"ℹ️ 无需修改: {file_path}")
  29. return False
  30. except Exception as e:
  31. print(f"❌ 处理文件出错 {file_path}: {e}")
  32. return False
  33. def main():
  34. """主函数"""
  35. # 查找所有Markdown文件
  36. docs_dir = ""
  37. # 递归查找所有.md文件
  38. md_files = []
  39. for root, dirs, files in os.walk(docs_dir):
  40. for file in files:
  41. if file.endswith('.md'):
  42. md_files.append(os.path.join(root, file))
  43. print(f"找到 {len(md_files)} 个Markdown文件")
  44. print("=" * 50)
  45. modified_count = 0
  46. for file_path in md_files:
  47. if fix_bold_format_in_file(file_path):
  48. modified_count += 1
  49. print("=" * 50)
  50. print(f"处理完成!共修改了 {modified_count} 个文件")
  51. if __name__ == "__main__":
  52. main()