make_doc_file.py 4.9 KB

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