make_doc_file.py 6.4 KB

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