make_doc_file.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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":"Air105","url":"https://gitee.com/openLuat/luatos-soc-air105/raw/master/application/include/luat_conf_bsp.h"},
  8. {"name":"ESP32C3","url":"https://gitee.com/openLuat/luatos-soc-idf5/raw/master/luatos/include/luat_conf_bsp.h"},
  9. {"name":"Air780","url":"https://gitee.com/openLuat/luatos-soc-2022/raw/master/project/luatos/inc/luat_conf_bsp.h"},
  10. ]
  11. print("getting bsp.h files...")
  12. for bsp in bsp_header_list:
  13. print("getting "+bsp["name"]+"...")
  14. res = ""
  15. #有时候获取不到完整的数据,报错的页面就是html
  16. while len(res) < 200 or res.find("</title>") != -1:
  17. res = requests.get(bsp["url"]).text
  18. print(res)
  19. bsp["url"] = res
  20. print("done "+ str(len(bsp["url"])) + " bytes")
  21. def get_tags(tag, is_api = False):
  22. if len(tag) == 0:
  23. if is_api:
  24. return ""
  25. else:
  26. return "{bdg-secondary}`适配状态未知`"
  27. r = []
  28. if is_api:
  29. r.append("{bdg-success}`本接口仅支持`")
  30. else:
  31. r.append("{bdg-success}`已适配`")
  32. for bsp in bsp_header_list:
  33. if bsp["url"].find(" "+tag+" ") >= 0 or bsp["url"].find(" "+tag+"\r") >= 0 or bsp["url"].find(" "+tag+"\n") >= 0:
  34. r.append("{bdg-primary}`" + bsp["name"] + "`")
  35. if len(r) > 1:
  36. return " ".join(r)
  37. else:
  38. return "{bdg-secondary}`适配状态未知`"
  39. def make(path,modules,index_text):
  40. try:
  41. shutil.rmtree(path)
  42. except:
  43. pass
  44. os.mkdir(path)
  45. doc = open(path+"index.rst", "a+",encoding='utf-8')
  46. doc.write(index_text)
  47. for module in modules:
  48. mdoc = open(path+module["module"]+".md", "a+",encoding='utf-8')
  49. mdoc.write("# "+module["module"]+" - "+module["summary"]+"\n\n")
  50. #支持的芯片
  51. mdoc.write(get_tags(module["tag"]))
  52. mdoc.write("\n\n")
  53. if len(module["url"]) > 0:
  54. mdoc.write("```{note}\n本页文档由[这个文件]("+module["url"]+")自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!\n```\n\n")
  55. if len(module["demo"]) > 0:
  56. mdoc.write("```{tip}\n本库有专属demo,[点此链接查看"+module["module"]+"的demo例子]("+module["demo"]+")\n```\n")
  57. if len(module["video"]) > 0:
  58. mdoc.write("```{tip}\n本库还有视频教程,[点此链接查看]("+module["video"]+")\n```\n\n")
  59. else:
  60. mdoc.write("\n")
  61. if len(module["usage"]) > 0:
  62. mdoc.write("**示例**\n\n")
  63. mdoc.write("```lua\n"+module["usage"]+"\n```\n\n")
  64. if len(module["const"]) > 0:
  65. mdoc.write("## 常量\n\n")
  66. mdoc.write("|常量|类型|解释|\n|-|-|-|\n")
  67. for const in module["const"]:
  68. mdoc.write("|"+const["var"].replace("|","\|")+"|"+const["type"].replace("|","\|")+"|"+const["summary"].replace("|","\|")+"|\n")
  69. mdoc.write("\n\n")
  70. doc.write(" "+module["module"]+"\n")
  71. for api in module["api"]:
  72. mdoc.write("## "+api["api"]+"\n\n")
  73. #支持的芯片
  74. mdoc.write(get_tags(api["tag"], True))
  75. mdoc.write("\n\n")
  76. mdoc.write(api["summary"]+"\n\n")
  77. mdoc.write("**参数**\n\n")
  78. if len(api["args"]) > 0:
  79. mdoc.write("|传入值类型|解释|\n|-|-|\n")
  80. for arg in api["args"]:
  81. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  82. mdoc.write("\n")
  83. else:
  84. mdoc.write("无\n\n")
  85. mdoc.write("**返回值**\n\n")
  86. if len(api["return"]) > 0:
  87. mdoc.write("|返回值类型|解释|\n|-|-|\n")
  88. for arg in api["return"]:
  89. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  90. mdoc.write("\n")
  91. else:
  92. mdoc.write("无\n\n")
  93. mdoc.write("**例子**\n\n")
  94. if len(api["usage"]) == 0:
  95. mdoc.write("无\n\n")
  96. else:
  97. mdoc.write("```lua\n"+api["usage"]+"\n```\n\n")
  98. mdoc.write("---\n\n")
  99. mdoc.close()
  100. doc.close()
  101. def get_description(api):
  102. s = api["api"]+" - "+api["summary"]+"\n"
  103. if len(api["args"]) > 0:
  104. s = s + "传入值:\n"
  105. for arg in api["args"]:
  106. s = s + arg["type"] + " " + arg["summary"]+"\n"
  107. if len(api["return"]) > 0:
  108. s = s + "返回值:\n"
  109. for arg in api["return"]:
  110. s = s + arg["type"] + " " + arg["summary"]+"\n"
  111. if len(api["usage"]) > 0:
  112. s = s + "例子:\n" + api["usage"]
  113. return s