make_doc_file.py 6.1 KB

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