make_doc_file.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import shutil
  2. import os
  3. import requests
  4. #bsp.h文件列表
  5. bsp_header_list = [
  6. ]
  7. print("getting bsp.h files...")
  8. for bsp in bsp_header_list:
  9. print("getting "+bsp["name"]+"...")
  10. res = ""
  11. #有时候获取不到完整的数据,报错的页面就是html
  12. retry = 0
  13. while len(res) < 200 or res.find("</title>") != -1:
  14. res = requests.get(bsp["url"]).text
  15. print(str(len(res)) + " bytes")
  16. retry = retry + 1
  17. if retry > 5:
  18. print("failed to get "+bsp["name"]+" bsp.h file")
  19. break
  20. bsp["url"] = res
  21. print("done "+ str(len(bsp["url"])) + " bytes")
  22. def is_supported(tag, bsp) :
  23. if bsp["url"].find(" "+tag+" ") >= 0 or bsp["url"].find(" "+tag+"\r") >= 0 or bsp["url"].find(" "+tag+"\n") >= 0:
  24. return True
  25. def get_tags(tag, is_api = False):
  26. if len(tag) == 0:
  27. if is_api:
  28. return ""
  29. else:
  30. return "{bdg-secondary}`适配状态未知`"
  31. r = []
  32. if is_api:
  33. r.append("{bdg-success}`本接口仅支持`")
  34. else:
  35. r.append("{bdg-success}`已适配`")
  36. for bsp in bsp_header_list:
  37. if bsp["url"].find(" "+tag+" ") >= 0 or bsp["url"].find(" "+tag+"\r") >= 0 or bsp["url"].find(" "+tag+"\n") >= 0:
  38. r.append("{bdg-primary}`" + bsp["name"] + "`")
  39. if len(r) > 1:
  40. return " ".join(r)
  41. else:
  42. return "{bdg-secondary}`适配状态未知`"
  43. def make(path,modules,index_text):
  44. try:
  45. shutil.rmtree(path)
  46. except:
  47. pass
  48. os.mkdir(path)
  49. is_api = str(path).endswith("api/")
  50. # 创建表格
  51. if is_api :
  52. doc = open(path+"supported.md", "w+",encoding='utf-8')
  53. doc.write("# 适配状态\n\n")
  54. doc.write("|BSP/库|简介")
  55. for bsp in bsp_header_list:
  56. doc.write("|" + bsp["name"])
  57. doc.write("|\n")
  58. doc.write("|---|---")
  59. for bsp in bsp_header_list:
  60. doc.write("|---")
  61. doc.write("|\n")
  62. for module in modules:
  63. name = module["module"]
  64. doc.write("|[{}]({}.md)|`{}`".format(name, name, module["summary"]))
  65. for bsp in bsp_header_list:
  66. if len(module["tag"]) == 0 :
  67. doc.write("|?")
  68. continue
  69. if is_supported(module["tag"], bsp) :
  70. doc.write("|Y")
  71. else:
  72. doc.write("|X")
  73. doc.write("|\n")
  74. doc.close()
  75. doc = open(path+"index.md", "a+",encoding='utf-8')
  76. doc.write(index_text)
  77. # 创建toctree
  78. doc.write("\n\n请点击左侧列表,查看各个接口。如需搜索,请使用F5进行搜索。\n\n"+
  79. "```{toctree}\n")
  80. if is_api:
  81. doc.write("supported\n")
  82. for module in modules:
  83. doc.write(module["module"]+"\n")
  84. doc.write("```\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