add_footer.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/05/03 13:47:45
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : add header to all markdown files
  8. '''
  9. import os,logging
  10. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  11. logger = logging.getLogger(__name__)
  12. def add_header():
  13. """
  14. param parameter_list:
  15. return:
  16. """
  17. header='''---
  18. title: 项目介绍
  19. date: 2023-05-02 12:00:00
  20. ---
  21. '''
  22. for root, dirs, files in os.walk("../"):
  23. for file in files:
  24. if file.endswith('.md'):
  25. try:
  26. with open(os.path.join(root,file),'r+',encoding='utf-8') as f:
  27. content=f.read()
  28. if content.startswith('---') or content.startswith('\n---'):
  29. logger.info('file {} already has header'.format(os.path.join(root,file)))
  30. else:
  31. f.seek(0,0)
  32. f.write(header)
  33. f.write(content)
  34. logger.info('file {} add header success'.format(os.path.join(root,file)))
  35. f.close()
  36. except Exception as e:
  37. logger.error('file {} add header failed'.format(os.path.join(root,file)))
  38. logger.error(e)
  39. continue
  40. def add_footer():
  41. '''
  42. add footer to all markdown files
  43. '''
  44. copyright='''
  45. ## 版权说明:
  46. 本文档版权隶属 天问科技 ,**仅用于天问科技旗下公司,团队为客户展示项目案例所用**,任何盗用本公司图文,描述,案例的行为均属违法,我们保留追究法律责任的权利。
  47. '''
  48. for root, dirs, files in os.walk("../"):
  49. for file in files:
  50. if file.endswith('.md'):
  51. with open(file,'a+',encoding='utf-8') as f:
  52. content=f.read()
  53. if content.find('## 版权说明:')!=-1:
  54. logger.info('file {} already has footer'.format(file))
  55. else:
  56. f.write('\n'+copyright)
  57. f.close()
  58. def run():
  59. '''
  60. run
  61. '''
  62. add_header()
  63. add_footer()
  64. if __name__=='__main__':
  65. run()