collect_projects.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python
  2. # Copyright (c) OpenMMLab. All rights reserved.
  3. import os
  4. import os.path as osp
  5. import re
  6. from glob import glob
  7. def _get_project_docs():
  8. """Get all project document files.
  9. Returns:
  10. list[str]: file paths
  11. """
  12. project_root = osp.join('..', '..', 'projects')
  13. pattern = osp.sep.join(['*'] * 2) + '.md'
  14. docs = glob(osp.join(project_root, pattern))
  15. docs = [
  16. doc for doc in docs
  17. if 'example_project' not in doc and '_CN' not in doc
  18. ]
  19. return docs
  20. def _parse_project_doc_path(fn):
  21. """Get project name and banner from a project reference file.
  22. Returns:
  23. tuple:
  24. - project_name (str)
  25. - project_banner (str)
  26. """
  27. project_banner, project_name = None, None
  28. with open(fn, 'r', encoding='utf-8') as f:
  29. for line in f.readlines():
  30. if re.match('^( )*<img', line) and not project_banner:
  31. project_banner = line
  32. if line.startswith('# ') and not project_name:
  33. project_name = line
  34. if project_name and project_banner:
  35. break
  36. if project_name is None or project_banner is None:
  37. raise ValueError(f'Invalid paper reference file {fn}')
  38. project_name = re.sub(r'^\# ', '', project_name).strip()
  39. project_banner = project_banner.strip()
  40. return project_name, project_banner
  41. def _get_project_intro_doc():
  42. project_intro_doc = []
  43. with open(
  44. osp.join('..', '..', 'projects', 'README.md'), 'r',
  45. encoding='utf-8') as f:
  46. for line in f.readlines():
  47. if line.startswith('# Welcome'):
  48. continue
  49. if './faq.md' in line:
  50. line = line.replace('./faq.md', '#faq')
  51. if './' in line:
  52. line = line.replace('./', '/projects/')
  53. project_intro_doc.append(line)
  54. if line.startswith('## Project List'):
  55. break
  56. return project_intro_doc
  57. def _get_faq_doc():
  58. faq_doc = ['\n']
  59. with open(
  60. osp.join('..', '..', 'projects', 'faq.md'), 'r',
  61. encoding='utf-8') as f:
  62. for line in f.readlines():
  63. if '#' in line:
  64. line = re.sub(r'^\#', '##', line)
  65. faq_doc.append(line)
  66. return faq_doc
  67. def main():
  68. # Build output folders
  69. os.makedirs('projects', exist_ok=True)
  70. # Collect all document contents
  71. project_doc_list = _get_project_docs()
  72. project_lines = []
  73. for path in project_doc_list:
  74. name, banner = _parse_project_doc_path(path)
  75. _path = path.split(osp.sep)
  76. _rel_path = _path[_path.index('projects'):-1]
  77. url = '/' + '/'.join(_rel_path)
  78. _name = name.split(':', 1)
  79. name, description = _name[0], '' if len(
  80. _name) < 2 else f': {_name[-1]}'
  81. project_lines += [
  82. f'- **{name}**{description} [\\[github\\]]({url})', '',
  83. '<div align="center">', ' ' + banner, '</div>', '<br/>', ''
  84. ]
  85. project_intro_doc = _get_project_intro_doc()
  86. faq_doc = _get_faq_doc()
  87. with open(
  88. osp.join('projects', 'community_projects.md'), 'w',
  89. encoding='utf-8') as f:
  90. f.write('# Projects from Community Contributors\n')
  91. f.write(''.join(project_intro_doc))
  92. f.write('\n'.join(project_lines))
  93. f.write(''.join(faq_doc))
  94. if __name__ == '__main__':
  95. print('collect project documents')
  96. main()