-
Notifications
You must be signed in to change notification settings - Fork 145
/
docgen.lua
373 lines (309 loc) · 7.64 KB
/
docgen.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
--[=[
@c ClassName [x base_1 x base_2 ... x base_n]
@t tag
@mt methodTag (applies to all class methods)
@p parameterName type
@op optionalParameterName type
@d description+
]=]
--[=[
@m methodName
@t tag
@p parameterName type
@op optionalParameterName type
@r return
@d description+
]=]
--[=[
@p propertyName type description+
]=]
local fs = require('fs')
local pathjoin = require('pathjoin')
local insert, sort, concat = table.insert, table.sort, table.concat
local format = string.format
local pathJoin = pathjoin.pathJoin
local function scan(dir)
for fileName, fileType in fs.scandirSync(dir) do
local path = pathJoin(dir, fileName)
if fileType == 'file' then
coroutine.yield(path)
else
scan(path)
end
end
end
local function match(s, pattern) -- only useful for one capture
return assert(s:match(pattern), s)
end
local function gmatch(s, pattern, hash) -- only useful for one capture
local tbl = {}
if hash then
for k in s:gmatch(pattern) do
tbl[k] = true
end
else
for v in s:gmatch(pattern) do
insert(tbl, v)
end
end
return tbl
end
local function matchType(s)
return s:match('^@(%S+)')
end
local function matchComments(s)
return s:gmatch('--%[=%[%s*(.-)%s*%]=%]')
end
local function matchClassName(s)
return match(s, '@c (%S+)')
end
local function matchMethodName(s)
return match(s, '@m (%S+)')
end
local function matchDescription(s)
return match(s, '@d (.+)'):gsub('%s+', ' ')
end
local function matchParents(s)
return gmatch(s, 'x (%S+)')
end
local function matchReturns(s)
return gmatch(s, '@r (%S+)')
end
local function matchTags(s)
return gmatch(s, '@t (%S+)', true)
end
local function matchMethodTags(s)
return gmatch(s, '@mt (%S+)', true)
end
local function matchProperty(s)
local a, b, c = s:match('@p (%S+) (%S+) (.+)')
return {
name = assert(a, s),
type = assert(b, s),
desc = assert(c, s):gsub('%s+', ' '),
}
end
local function matchParameters(s)
local ret = {}
for optional, paramName, paramType in s:gmatch('@(o?)p (%S+) (%S+)') do
insert(ret, {paramName, paramType, optional == 'o'})
end
return ret
end
local function matchMethod(s)
return {
name = matchMethodName(s),
desc = matchDescription(s),
parameters = matchParameters(s),
returns = matchReturns(s),
tags = matchTags(s),
}
end
----
local docs = {}
local function newClass()
local class = {
methods = {},
statics = {},
properties = {},
}
local function init(s)
class.name = matchClassName(s)
class.parents = matchParents(s)
class.desc = matchDescription(s)
class.parameters = matchParameters(s)
class.tags = matchTags(s)
class.methodTags = matchMethodTags(s)
assert(not docs[class.name], 'duplicate class: ' .. class.name)
docs[class.name] = class
end
return class, init
end
for f in coroutine.wrap(scan), './libs' do
local d = assert(fs.readFileSync(f))
local class, initClass = newClass()
for s in matchComments(d) do
local t = matchType(s)
if t == 'c' then
initClass(s)
elseif t == 'm' then
local method = matchMethod(s)
for k, v in pairs(class.methodTags) do
method.tags[k] = v
end
method.class = class
insert(method.tags.static and class.statics or class.methods, method)
elseif t == 'p' then
insert(class.properties, matchProperty(s))
end
end
end
----
local output = 'docs'
local function link(str)
if type(str) == 'table' then
local ret = {}
for i, v in ipairs(str) do
ret[i] = link(v)
end
return concat(ret, ', ')
else
local ret = {}
for t in str:gmatch('[^/]+') do
insert(ret, docs[t] and format('[[%s]]', t) or t)
end
return concat(ret, '/')
end
end
local function sorter(a, b)
return a.name < b.name
end
local function writeHeading(f, heading)
f:write('## ', heading, '\n\n')
end
local function writeProperties(f, properties)
sort(properties, sorter)
f:write('| Name | Type | Description |\n')
f:write('|-|-|-|\n')
for _, v in ipairs(properties) do
f:write('| ', v.name, ' | ', link(v.type), ' | ', v.desc, ' |\n')
end
f:write('\n')
end
local function writeParameters(f, parameters)
f:write('(')
local optional
if #parameters > 0 then
for i, param in ipairs(parameters) do
f:write(param[1])
if i < #parameters then
f:write(', ')
end
if param[3] then
optional = true
end
end
f:write(')\n\n')
if optional then
f:write('| Parameter | Type | Optional |\n')
f:write('|-|-|:-:|\n')
for _, param in ipairs(parameters) do
local o = param[3] and '✔' or ''
f:write('| ', param[1], ' | ', link(param[2]), ' | ', o, ' |\n')
end
f:write('\n')
else
f:write('| Parameter | Type |\n')
f:write('|-|-|\n')
for _, param in ipairs(parameters) do
f:write('| ', param[1], ' | ', link(param[2]), ' |\n')
end
f:write('\n')
end
else
f:write(')\n\n')
end
end
local methodTags = {}
methodTags['http'] = 'This method always makes an HTTP request.'
methodTags['http?'] = 'This method may make an HTTP request.'
methodTags['ws'] = 'This method always makes a WebSocket request.'
methodTags['mem'] = 'This method only operates on data in memory.'
local function checkTags(tbl, check)
for i, v in ipairs(check) do
if tbl[v] then
for j, w in ipairs(check) do
if i ~= j then
if tbl[w] then
return error(string.format('mutually exclusive tags encountered: %s and %s', v, w), 1)
end
end
end
end
end
end
local function writeMethods(f, methods)
sort(methods, sorter)
for _, method in ipairs(methods) do
f:write('### ', method.name)
writeParameters(f, method.parameters)
f:write(method.desc, '\n\n')
local tags = method.tags
checkTags(tags, {'http', 'http?', 'mem'})
checkTags(tags, {'ws', 'mem'})
for k in pairs(tags) do
if k ~= 'static' then
assert(methodTags[k], k)
f:write('*', methodTags[k], '*\n\n')
end
end
f:write('**Returns:** ', link(method.returns), '\n\n----\n\n')
end
end
if not fs.existsSync(output) then
fs.mkdirSync(output)
end
local function collectParents(parents, k, ret, seen)
ret = ret or {}
seen = seen or {}
for _, parent in ipairs(parents) do
parent = docs[parent]
if parent then
for _, v in ipairs(parent[k]) do
if not seen[v] then
seen[v] = true
insert(ret, v)
end
end
end
collectParents(parent.parents, k, ret, seen)
end
return ret
end
for _, class in pairs(docs) do
local f = io.open(pathJoin(output, class.name .. '.md'), 'w')
local parents = class.parents
local parentLinks = link(parents)
if next(parents) then
f:write('#### *extends ', parentLinks, '*\n\n')
end
f:write(class.desc, '\n\n')
checkTags(class.tags, {'ui', 'abc'})
if class.tags.ui then
writeHeading(f, 'Constructor')
f:write('### ', class.name)
writeParameters(f, class.parameters)
elseif class.tags.abc then
f:write('*This is an abstract base class. Direct instances should never exist.*\n\n')
else
f:write('*Instances of this class should not be constructed by users.*\n\n')
end
local properties = collectParents(parents, 'properties')
if next(properties) then
writeHeading(f, 'Properties Inherited From ' .. parentLinks)
writeProperties(f, properties)
end
if next(class.properties) then
writeHeading(f, 'Properties')
writeProperties(f, class.properties)
end
local statics = collectParents(parents, 'statics')
if next(statics) then
writeHeading(f, 'Static Methods Inherited From ' .. parentLinks)
writeMethods(f, statics)
end
local methods = collectParents(parents, 'methods')
if next(methods) then
writeHeading(f, 'Methods Inherited From ' .. parentLinks)
writeMethods(f, methods)
end
if next(class.statics) then
writeHeading(f, 'Static Methods')
writeMethods(f, class.statics)
end
if next(class.methods) then
writeHeading(f, 'Methods')
writeMethods(f, class.methods)
end
f:close()
end