make_doc_file.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import shutil
  2. import os
  3. def make(path,modules,index_text):
  4. try:
  5. shutil.rmtree(path)
  6. except:
  7. pass
  8. os.mkdir(path)
  9. doc = open(path+"index.rst", "a+",encoding='utf-8')
  10. doc.write(index_text)
  11. for module in modules:
  12. mdoc = open(path+module["module"]+".md", "a+",encoding='utf-8')
  13. mdoc.write("# "+module["module"]+" - "+module["summary"]+"\n\n")
  14. if len(module["url"]) > 0:
  15. mdoc.write("> 本页文档由[这个文件]("+module["url"]+")自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!\n\n")
  16. if len(module["demo"]) > 0:
  17. mdoc.write("> 本库有专属demo,[点此链接查看"+module["module"]+"的demo例子]("+module["demo"]+")\n")
  18. if len(module["video"]) > 0:
  19. mdoc.write("> 本库还有视频教程,[点此链接查看]("+module["video"]+")\n\n")
  20. else:
  21. mdoc.write("\n")
  22. if len(module["usage"]) > 0:
  23. mdoc.write("**示例**\n\n")
  24. mdoc.write("```lua\n"+module["usage"]+"\n```\n\n")
  25. if len(module["const"]) > 0:
  26. mdoc.write("## 常量\n\n")
  27. mdoc.write("|常量|类型|解释|\n|-|-|-|\n")
  28. for const in module["const"]:
  29. mdoc.write("|"+const["var"].replace("|","\|")+"|"+const["type"].replace("|","\|")+"|"+const["summary"].replace("|","\|")+"|\n")
  30. mdoc.write("\n\n")
  31. doc.write(" "+module["module"]+"\n")
  32. for api in module["api"]:
  33. mdoc.write("## "+api["api"]+"\n\n")
  34. mdoc.write(api["summary"]+"\n\n")
  35. mdoc.write("**参数**\n\n")
  36. if len(api["args"]) > 0:
  37. mdoc.write("|传入值类型|解释|\n|-|-|\n")
  38. for arg in api["args"]:
  39. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  40. mdoc.write("\n")
  41. else:
  42. mdoc.write("无\n\n")
  43. mdoc.write("**返回值**\n\n")
  44. if len(api["return"]) > 0:
  45. mdoc.write("|返回值类型|解释|\n|-|-|\n")
  46. for arg in api["return"]:
  47. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  48. mdoc.write("\n")
  49. else:
  50. mdoc.write("无\n\n")
  51. mdoc.write("**例子**\n\n")
  52. if len(api["usage"]) == 0:
  53. mdoc.write("无\n\n")
  54. else:
  55. mdoc.write("```lua\n"+api["usage"]+"\n```\n\n")
  56. mdoc.write("---\n\n")
  57. mdoc.close()
  58. doc.close()
  59. def get_description(api):
  60. s = api["api"]+" - "+api["summary"]+"\n"
  61. if len(api["args"]) > 0:
  62. s = s + "传入值:\n"
  63. for arg in api["args"]:
  64. s = s + arg["type"] + " " + arg["summary"]+"\n"
  65. if len(api["return"]) > 0:
  66. s = s + "返回值:\n"
  67. for arg in api["return"]:
  68. s = s + arg["type"] + " " + arg["summary"]+"\n"
  69. if len(api["usage"]) > 0:
  70. s = s + "例子:\n" + api["usage"]
  71. return s