maker.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. function make(lines)
  2. local text = {"","@[TOC]", ""}
  3. local lastLine = 1
  4. local moduleName
  5. --获取本文件的注释
  6. local moduleInfo = {lines[1]:match("%-%-%- *(.+)"),}
  7. for i=2,#lines do
  8. if lines[i]:find("@module") then
  9. moduleName = lines[i]:match("%-%- *@module *(.+)")
  10. table.insert(text, "# "..lines[i]:match("%-%- *@module *(.+)"))
  11. table.insert(text, "")
  12. lastLine = i
  13. break
  14. else
  15. table.insert(moduleInfo, lines[i]:match("%-%- *(.+)"))
  16. end
  17. end
  18. moduleInfo = table.concat( moduleInfo, "\r\n\r\n")
  19. table.insert(text, moduleInfo)
  20. table.insert(text, "")
  21. --每个函数循环处理
  22. while lastLine < #lines do
  23. --匹配注释第一行,开始处理该函数
  24. if lines[lastLine]:find("%-%-%- *.+") == 1 and not lines[lastLine]:find("%-%-%-%-") and lines[lastLine+1]:find("%-%-.*") == 1 then
  25. --函数解释
  26. local functionInfo = lines[lastLine]:match("%-%-%- *(.+)")
  27. --table.insert(text, functionInfo)
  28. local functionName--匹配的函数名
  29. local arg--匹配的变量名
  30. local all = {}
  31. for i=lastLine+1,#lines do
  32. if lines[i]:find("function ") == 1 then
  33. lastLine = i+2
  34. functionName = lines[i]:match("function *(.+)")
  35. if functionName and functionName:find(" return ") then
  36. local ret = functionName:find(" return ")
  37. functionName = functionName:sub(1,ret-1)
  38. end
  39. break
  40. elseif lines[i]:find("local +function ") == 1 then
  41. lastLine = i+2
  42. functionName = lines[i]:match("function *(.+)")
  43. functionName = functionName.." (local函数 无法被外部调用)"
  44. if functionName and functionName:find(" return ") then
  45. local ret = functionName:find(" return ")
  46. functionName = functionName:sub(1,ret-1)
  47. end
  48. break
  49. elseif lines[i]:find(".*%..* *= *function *%(") == 1 then
  50. lastLine = i+2
  51. local funcVars = lines[i]:match(".*%..* *= *function *(.*)")
  52. functionName = lines[i]:match("(.*%..*) *= *function *%(")..funcVars
  53. break
  54. elseif lines[i]:find("socket%.isReady") == 1 then--单独处理socket.isReady函数(太特殊了)
  55. lastLine = i+2
  56. functionName = "socket.isReady()"
  57. break
  58. elseif lines[i]:find("%w+ *=") == 1 then--匹配常量声明
  59. lastLine = i+1
  60. arg = lines[i]:match("(%w+) *=")
  61. --print(arg)
  62. break
  63. elseif lines[i]:find("%-%-%- *.+") == 1 then
  64. break
  65. else
  66. table.insert(all,lines[i])
  67. end
  68. end
  69. if functionName then--匹配成功
  70. functionName = functionName:gsub(" *, *",", ")
  71. --加上函数开头
  72. local functionHead = functionName:match("(.+)%(")
  73. if functionHead:find("%.") or functionHead:find(":") then
  74. table.insert(text, "## "..functionName)
  75. else
  76. table.insert(text, "## "..moduleName.."."..functionName)
  77. end
  78. table.insert(text, "")
  79. table.insert(text, functionInfo)
  80. --处理多行注释解释的情况
  81. local infotemp = {}
  82. for i=1,#all do
  83. if all[i]:find("%-%- *[^-](.+)") == 1 and all[i]:find("%-%- *@") ~= 1 then
  84. table.insert(infotemp, all[i]:match("%-%- *(.+)"))
  85. else
  86. break
  87. end
  88. end
  89. if #infotemp > 0 then
  90. table.insert(text, "")
  91. table.insert(text, table.concat(infotemp,"<br>"))
  92. end
  93. table.insert(text, "")
  94. --防止参数信息不在最开头
  95. local ptemp,rtemp,utemp = 0,0,0
  96. for i=1,#all do
  97. if all[i]:find("%-%- *@") == 1 then
  98. if all[i]:find("%-%- *@return") == 1 then
  99. rtemp = rtemp == 0 and i or rtemp
  100. elseif all[i]:find("%-%- *@usage") == 1 then
  101. utemp = utemp == 0 and i or utemp
  102. elseif ptemp == 0 and all[i]:find("%-%- *@see") ~= 1 then
  103. ptemp = i
  104. end
  105. end
  106. end
  107. if ptemp > rtemp and rtemp ~= 0 and utemp ~= 0 then
  108. print("wrong param "..tostring(ptemp)..","..tostring(rtemp)..","..tostring(utemp)..","..functionName)
  109. --重新整理顺序,使之正确
  110. for i=0,utemp - ptemp - 1 do
  111. table.insert(all,rtemp+i,table.remove(all,ptemp+i))
  112. end
  113. end
  114. --上次处理的函数信息行数
  115. local last = 1
  116. --筛选出传入参数值
  117. local para = {}
  118. for i=last,#all do
  119. if all[i]:find("%-%- *@return") == 1 or all[i]:find("%-%- *@usage") == 1 then
  120. last = i
  121. break
  122. else
  123. if all[i]:find("%-%- *@") == 1 then
  124. --获取参数信息与解释
  125. local choseType = nil
  126. local ft,fi = all[i]:match("%-%- *@(%w+) (.+)")
  127. if not ft then
  128. ft,fi = all[i]:match("%-%- *@(%w+)%[opt=.+%] (.+)")
  129. choseType = all[i]:match("%-%- *@%w+%[opt=(.+)%] .+")
  130. end
  131. if ft then
  132. local ppp = {type=ft}
  133. if fi then
  134. local fn, fe = fi:match("(.+) (.+)")
  135. if moduleName == "log" then
  136. --print("fn" , fn, "fe", fe)
  137. end
  138. if fn and fe and #fn < 32 then
  139. ppp["name"] = fn:trim()
  140. ppp["info"] = fe:trim()
  141. else
  142. ppp["name"] = "-"
  143. ppp["info"] = fi
  144. end
  145. else
  146. ppp["info"] = ""
  147. ppp["name"] = ""
  148. end
  149. if choseType then
  150. ppp["info"] = "**可选参数,默认为`" .. choseType .. "`** " .. ppp["info"]
  151. end
  152. table.insert(para, ppp)
  153. end
  154. else
  155. if para[#para] then
  156. para[#para].info = para[#para].info.."<br>"..(all[i]:match("%-%- *(.+)") or "")
  157. end
  158. end
  159. end
  160. if i == #all then last = i end--没匹配到下一个,避免出错,直接结束本区块
  161. end
  162. local paraText = {"|名称|传入值类型|释义|","|-|-|-|"}
  163. for i=1,#para do
  164. table.insert(paraText, "|" .. para[i].name .. "|"..para[i].type.."|"..para[i].info.."|")
  165. end
  166. table.insert(text, "* 参数")
  167. table.insert(text, "")
  168. if #para ~= 0 then
  169. table.insert(text, table.concat(paraText,"\r\n"))
  170. else
  171. table.insert(text, "无")
  172. end
  173. table.insert(text, "")
  174. --筛选出返回值参数
  175. local returnInfo = {}
  176. for i=last,#all do
  177. -- if functionName == "getState()" then
  178. -- print(all[i])
  179. -- end
  180. if all[i]:find("%-%- *@return") == 1 then
  181. table.insert(returnInfo, all[i]:match("%-%- *@return *(.+)") or "")
  182. elseif all[i]:find("%-%- *@usage") == 1 then
  183. last = i
  184. break
  185. else
  186. if returnInfo[#returnInfo] then
  187. returnInfo[#returnInfo] = returnInfo[#returnInfo].."<br>"..(all[i]:match("%-%- *@return *(.+)") or all[i]:match("%-%- *(.+)") or "")
  188. end
  189. end
  190. if i == #all then last = i end--没匹配到下一个,避免出错,直接结束本区块
  191. end
  192. table.insert(text, "* 返回值")
  193. table.insert(text, "")
  194. if #returnInfo ~= 0 then
  195. table.insert(text, table.concat(returnInfo,"\r\n"))
  196. else
  197. table.insert(text, "无")
  198. end
  199. table.insert(text, "")
  200. if last ~= #all+1 then
  201. --筛选出示例参数
  202. local example = {}
  203. for i=last,#all do
  204. local usage
  205. if all[i]:find("%-%- *@usage.+") == 1 then
  206. usage = all[i]:match("%-%- *@usage *(.+)") or ""
  207. elseif all[i]:find("%-%- *@see") == 1 then
  208. usage = "--另见:"..(all[i]:match("%-%- *@see *(.+)") or "")
  209. elseif all[i]:find("%-%- *.+") == 1 and all[i]:find("%-%- *@usage") ~= 1 and all[i]:find("%-%- *@return") ~= 1 then
  210. usage = all[i]:match("%-%- *(.+)") or ""
  211. end
  212. if usage and usage:gsub(" *","") ~= "" then--过滤掉空行
  213. table.insert(example, usage)
  214. end
  215. if i == #all then last = i end--没匹配到下一个,避免出错,直接结束本区块
  216. end
  217. --第一个字符为中文的,该行直接注释掉
  218. for i=1,#example do
  219. local first = example[i]:byte()
  220. if not (first>0 and first<=127) then
  221. example[i] = "-- "..example[i]
  222. end
  223. end
  224. table.insert(text, "* 例子")
  225. table.insert(text, "")
  226. if #example ~= 0 then
  227. table.insert(text, "```lua")
  228. table.insert(text, table.concat(example,"\r\n"))
  229. table.insert(text, "```")
  230. else
  231. table.insert(text, "无")
  232. end
  233. table.insert(text, "")
  234. end
  235. table.insert(text, "---")
  236. table.insert(text, "")
  237. elseif arg then
  238. table.insert(text, "### "..moduleName.."."..arg)
  239. table.insert(text, "")
  240. table.insert(text, "常量值,"..functionInfo)
  241. table.insert(text, "")
  242. local argInfo = table.concat(all, "\r\n\r\n"):gsub("%-%- *","")
  243. table.insert(text, argInfo)
  244. table.insert(text, "")
  245. table.insert(text, "---")
  246. table.insert(text, "")
  247. else
  248. lastLine = lastLine + 1
  249. end
  250. else
  251. lastLine = lastLine + 1
  252. end
  253. end
  254. return table.concat(text, "\r\n")
  255. end
  256. return make