docgen.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. import subprocess
  3. import glob
  4. folder_name = os.getcwd().split("/")[-1] # 获取当前目录的名称
  5. python_files = [] # 用来保存找到的文件
  6. for folder_item in os.listdir("."):
  7. if os.path.isdir(folder_item): # 判断是否为文件夹
  8. for file in os.listdir(folder_item):
  9. if file.endswith(".py") or file.endswith(".pyi"): # 判断文件后缀是否为 .py 或者 .pyi
  10. # 判断文件是否和当前目录名称相同
  11. if file == folder_item + ".py" or file == folder_item + ".pyi":
  12. python_files.append(os.path.join(
  13. folder_item, file)) # 将找到的文件添加到列表中
  14. def generate_documentation(path_to_file):
  15. # Set the output file path based on the input file name
  16. file_name = os.path.splitext(os.path.basename(path_to_file))[0]
  17. output_file = f"doc/API_{file_name}.md"
  18. # Create the 'doc' directory if it does not exist
  19. if not os.path.exists("doc"):
  20. os.mkdir("doc")
  21. # Build the command to run rust-msc-latest-linux
  22. cmd = ["rust-msc-latest-linux", "--docgen",
  23. path_to_file, "-o", output_file]
  24. # Run the command and capture the output
  25. output = subprocess.run(cmd, capture_output=True, text=True)
  26. # Print the output
  27. print(output.stdout)
  28. # Check if there was an error
  29. if output.returncode != 0:
  30. print(output.stderr)
  31. for item in python_files:
  32. # 调用 rust-msc-latest-linux --docgen <path-to-file.[py/pyi]> -o <file.md>
  33. generate_documentation(item)
  34. def add_title_to_md_files(directory):
  35. # 获取目录下所有的md文件路径
  36. md_files = [f for f in os.listdir(directory) if f.endswith('.md')]
  37. for md_file in md_files:
  38. file_path = os.path.join(directory, md_file)
  39. with open(file_path, 'r') as f:
  40. lines = f.readlines()
  41. file_name = os.path.splitext(md_file)[0]
  42. title = file_name.replace('API_', '')
  43. # 在文件开头添加一级标题
  44. lines.insert(0, f'## API\n\n')
  45. lines.insert(0, f'# {title} 模块 API 文档\n\n')
  46. with open(file_path, 'w') as f:
  47. f.writelines(lines)
  48. print(f'{len(md_files)}个.md文件已添加标题')
  49. # 指定目录路径
  50. directory = 'doc'
  51. add_title_to_md_files(directory)
  52. # 获取目录下所有的md文件路径
  53. md_files = glob.glob(os.path.join(directory, '*.md'))
  54. def remove_prefix(filename): # 如果文件名以“API_”开头,则去掉该前缀
  55. if os.path.basename(filename).startswith("API_"):
  56. return os.path.basename(filename)[4:] # 否则返回原文件名 else: return filename
  57. md_files_sort = sorted(md_files, key=lambda x: (remove_prefix(x).casefold()))
  58. print(md_files_sort)
  59. # 生成toctree指令内容
  60. toctree_content = ''
  61. for md_file in md_files_sort:
  62. # 获取文件名(不包含扩展名)
  63. file_name = os.path.splitext(os.path.basename(md_file))[0]
  64. # 生成toctree指令项
  65. toctree_item = f' {file_name}\n'
  66. toctree_content += toctree_item
  67. # 生成API文档文件内容
  68. doc_content = f'''模块 API 文档
  69. ============================
  70. .. toctree::
  71. :maxdepth: 1
  72. {toctree_content}
  73. '''
  74. # 写入API文档文件
  75. with open('doc/index_api.rst', 'w') as f:
  76. f.write(doc_content)
  77. # 获得 module list
  78. module_list = [remove_prefix(md_file).replace('.md', '')
  79. for md_file in md_files]
  80. print(module_list)
  81. # 收集 ../examples/<module> 下的所有py文件
  82. for module in module_list:
  83. if module == 'PikaStdDevice':
  84. module_example = 'Device'
  85. elif module == 'pika_lvgl':
  86. module_example = 'lvgl'
  87. elif module == 'pika_cjson':
  88. module_example = 'cJSON'
  89. elif module == 'PikaStdLib':
  90. module_example = 'BuiltIn'
  91. else:
  92. module_example = module
  93. example_list = glob.glob(f'../examples/{module_example}/*.py')
  94. print(example_list)
  95. with open(f'doc/API_{module}.md', 'a') as f:
  96. f.write('\n\n## Examples\n\n')
  97. for example in example_list:
  98. with open(example, 'r') as example_file:
  99. f.write(f'### {os.path.basename(example)}\n\n')
  100. f.write('```python\n')
  101. f.write(example_file.read())
  102. f.write('\n```\n')