make_doc_file.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import shutil
  2. import os
  3. import requests
  4. #bsp.h文件列表
  5. bsp_header_list = [
  6. {"name":"Air780EPM","url":"https://github.com/openLuat/luatos-soc-2024/raw/master/project/luatos/inc/luat_conf_bsp_air780epm.h"},
  7. {"name":"Air780E","url":"https://github.com/openLuat/luatos-soc-2022/raw/master/project/luatos/inc/luat_conf_bsp.h"},
  8. {"name":"Air780EP","url":"https://github.com/openLuat/luatos-soc-2024/raw/master/project/luatos/inc/luat_conf_bsp_dft.h"},
  9. {"name":"Air780EPS","url":"https://github.com/openLuat/luatos-soc-2024/raw/master/project/luatos/inc/luat_conf_bsp_air780eps.h"},
  10. {"name":"Air201","url":"https://github.com/openLuat/luatos-soc-2024/raw/master/project/luatos/inc/luat_conf_bsp_air201.h"},
  11. ]
  12. print("getting bsp.h files...")
  13. for bsp in bsp_header_list:
  14. print("getting "+bsp["name"]+"...")
  15. res = ""
  16. #有时候获取不到完整的数据,报错的页面就是html
  17. retry = 0
  18. while len(res) < 200 or res.find("</title>") != -1:
  19. res = requests.get(bsp["url"]).text
  20. print(str(len(res)) + " bytes")
  21. retry = retry + 1
  22. if retry > 5:
  23. print("failed to get "+bsp["name"]+" bsp.h file")
  24. break
  25. bsp["url"] = res
  26. print("done "+ str(len(bsp["url"])) + " bytes")
  27. def is_supported(tag, bsp) :
  28. if bsp["url"].find(" "+tag+" ") >= 0 or bsp["url"].find(" "+tag+"\r") >= 0 or bsp["url"].find(" "+tag+"\n") >= 0:
  29. return True
  30. def get_tags(tag, is_api = False):
  31. if len(tag) == 0:
  32. if is_api:
  33. return ""
  34. else:
  35. return "{bdg-secondary}`适配状态未知`"
  36. r = []
  37. if is_api:
  38. r.append("{bdg-success}`本接口仅支持`")
  39. else:
  40. r.append("{bdg-success}`已适配`")
  41. for bsp in bsp_header_list:
  42. if bsp["url"].find(" "+tag+" ") >= 0 or bsp["url"].find(" "+tag+"\r") >= 0 or bsp["url"].find(" "+tag+"\n") >= 0:
  43. r.append("{bdg-primary}`" + bsp["name"] + "`")
  44. if len(r) > 1:
  45. return " ".join(r)
  46. else:
  47. return "{bdg-secondary}`适配状态未知`"
  48. def make(path,modules,index_text):
  49. try:
  50. shutil.rmtree(path)
  51. except:
  52. pass
  53. os.mkdir(path)
  54. is_api = str(path).endswith("api/")
  55. # 创建表格
  56. if is_api :
  57. doc = open(path+"supported.md", "w+",encoding='utf-8')
  58. doc.write("# 适配状态\n\n")
  59. doc.write("|BSP/库|简介")
  60. for bsp in bsp_header_list:
  61. doc.write("|" + bsp["name"])
  62. doc.write("|\n")
  63. doc.write("|---|---")
  64. for bsp in bsp_header_list:
  65. doc.write("|---")
  66. doc.write("|\n")
  67. for module in modules:
  68. name = module["module"]
  69. doc.write("|[{}]({}.md)|`{}`".format(name, name, module["summary"]))
  70. for bsp in bsp_header_list:
  71. if len(module["tag"]) == 0 :
  72. doc.write("|?")
  73. continue
  74. if is_supported(module["tag"], bsp) :
  75. doc.write("|Y")
  76. else:
  77. doc.write("|X")
  78. doc.write("|\n")
  79. doc.close()
  80. doc = open(path+"index.md", "a+",encoding='utf-8')
  81. doc.write(index_text)
  82. # 创建toctree
  83. doc.write("\n\n请点击左侧列表,查看各个接口。如需搜索,请使用F5进行搜索。\n\n"+
  84. "```{toctree}\n")
  85. if is_api:
  86. doc.write("supported\n")
  87. for module in modules:
  88. doc.write(module["module"]+"\n")
  89. doc.write("```\n")
  90. doc.close()
  91. for module in modules:
  92. mdoc = open(path+module["module"]+".md", "a+",encoding='utf-8')
  93. mdoc.write("# "+module["module"]+" - "+module["summary"]+"\n\n")
  94. #支持的芯片
  95. # mdoc.write(get_tags(module["tag"]))
  96. # mdoc.write("\n\n")
  97. # if len(module["url"]) > 0:
  98. # mdoc.write("```{note}\n本页文档由[这个文件]("+module["url"]+")自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!\n```\n\n")
  99. # if len(module["demo"]) > 0:
  100. # mdoc.write("```{tip}\n本库有专属demo,[点此链接查看"+module["module"]+"的demo例子]("+module["demo"]+")\n```\n")
  101. # if len(module["video"]) > 0:
  102. # mdoc.write("```{tip}\n本库还有视频教程,[点此链接查看]("+module["video"]+")\n```\n\n")
  103. # else:
  104. # mdoc.write("\n")
  105. if len(module["usage"]) > 0:
  106. mdoc.write("**示例**\n\n")
  107. mdoc.write("```lua\n"+module["usage"]+"\n```\n\n")
  108. if len(module["const"]) > 0:
  109. mdoc.write("## 常量\n\n")
  110. mdoc.write("|常量|类型|解释|\n|-|-|-|\n")
  111. for const in module["const"]:
  112. mdoc.write("|"+const["var"].replace("|","\|")+"|"+const["type"].replace("|","\|")+"|"+const["summary"].replace("|","\|")+"|\n")
  113. mdoc.write("\n\n")
  114. for api in module["api"]:
  115. mdoc.write("## "+api["api"]+"\n\n")
  116. #支持的芯片
  117. mdoc.write(get_tags(api["tag"], True))
  118. mdoc.write("\n\n")
  119. mdoc.write(api["summary"]+"\n\n")
  120. mdoc.write("**参数**\n\n")
  121. if len(api["args"]) > 0:
  122. mdoc.write("|传入值类型|解释|\n|-|-|\n")
  123. for arg in api["args"]:
  124. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  125. mdoc.write("\n")
  126. else:
  127. mdoc.write("无\n\n")
  128. mdoc.write("**返回值**\n\n")
  129. if len(api["return"]) > 0:
  130. mdoc.write("|返回值类型|解释|\n|-|-|\n")
  131. for arg in api["return"]:
  132. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  133. mdoc.write("\n")
  134. else:
  135. mdoc.write("无\n\n")
  136. mdoc.write("**例子**\n\n")
  137. if len(api["usage"]) == 0:
  138. mdoc.write("无\n\n")
  139. else:
  140. mdoc.write("```lua\n"+api["usage"]+"\n```\n\n")
  141. mdoc.write("---\n\n")
  142. mdoc.close()
  143. def get_description(api):
  144. s = api["api"]+" - "+api["summary"]+"\n"
  145. if len(api["args"]) > 0:
  146. s = s + "传入值:\n"
  147. for arg in api["args"]:
  148. s = s + arg["type"] + " " + arg["summary"]+"\n"
  149. if len(api["return"]) > 0:
  150. s = s + "返回值:\n"
  151. for arg in api["return"]:
  152. s = s + arg["type"] + " " + arg["summary"]+"\n"
  153. if len(api["usage"]) > 0:
  154. s = s + "例子:\n" + api["usage"]
  155. return s