-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
368 lines (299 loc) · 8.67 KB
/
init.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
local directory = process.argv[1]
local string = require "string"
local table = require "table"
local utils = require "utils"
local core = require('core')
if not directory then
print("Usage: <command> <directory name>")
return
end
directory = directory:gsub("/$", "" )
local fs = require "fs"
function string.split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find (fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t, cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function string.trim (s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
local function mkdir(file)
local dir = file:split("/")
local path = ""
for i, d in pairs(dir) do
path = path .. d.."/"
if not fs.existsSync(path) then
fs.mkdirSync(path, "777")
end
end
end
local Block = core.Object:extend()
function Block:initialize(type)
self._subBlocks = {}
self.tags = {}
self.codeLines = {""}
self.type = type or ""
self.name = ""
self.title = ""
self.description = ""
self.parent = ""
self.file = ""
end
function Block:addSubBlock(block)
table.insert(self._subBlocks, block)
end
function Block:getSubBlocks()
return self._subBlocks
end
function Block:loopAll(cb, depth)
depth = depth or 0
for i, row in pairs(self._subBlocks) do
cb(row, depth)
row:loopAll(cb, depth + 1)
end
end
local function runFile(file, callback)
if file:match(".lua$") then
local niceName = file:gsub(directory, "out")
local fileName = file:gsub(directory, "")
mkdir(niceName:gsub("(.*)/(.-)$", function(a) return a end))
fs.readFile(file, function (err, data)
if err then
print (("Could not open file for reading comments: %s"):format(file))
return
end
local function isComment(line)
return string.find(line, "^[\t ]*%-%-%[%[%!")
end
local function isCommentEnd(line)
return string.find(line, "^[\t ]*]]")
end
local lines = data:split("\n")
--File block
local block = Block:new("file")
block.file = fileName
local i = 1
repeat
local codeBlock = Block:new()
codeBlock.file = fileName
--Make sure we start on the first commented line
while lines[i] and not isComment(lines[i]) do
i = i + 1
end
local startI = i
i = i + 1 -- skip --[[!
while lines[i] and not isCommentEnd(lines[i]) do
if codeBlock.comment then
codeBlock.comment = codeBlock.comment .. "\n" .. lines[i]
else
codeBlock.comment = lines[i]
end
i = i + 1
end
if codeBlock.comment then
--Parse comments
local commentLines = string.split(codeBlock.comment, "\n")
local haveTitle = false
for i, commentRow in pairs(commentLines) do
commentRow = commentRow:gsub("^[\t ]*", "")
if commentRow:find("^@(.*)") then
local tag, value, _
if commentRow:find("^@(.-) (.*)") then
_, _, tag, value = commentRow:find("^@(.-) (.*)")
else
_, _, tag = commentRow:find("^@(.*)")
end
if not value then
value = ""
end
tag = tag:lower()
value = value:trim()
--Manual overrides
if tag == "name" then
codeBlock.name = value
elseif tag == "title" then
codeBlock.title = value
elseif tag == "descriptionclear" then
codeBlock.description = ""
elseif tag == "description" then
codeBlock.description = codeBlock.description .. value
--type tags (Auto resolve)
elseif tag == "file" then
--File type: a lua file without a module
codeBlock.type = "file"
if value ~= "" then
codeBlock.name = value
codeBlock.file = value
else
codeBlock.name = block.file
end
elseif tag == "module" then
codeBlock.type = "module"
if value ~= "" then
codeBlock.module = value
codeBlock.name = value
end
elseif tag == "class" then
codeBlock.type = "class"
if value ~= "" then
codeBlock.module = value
codeBlock.name = value
end
elseif tag == "function" then
codeBlock.type = "function"
if value ~= "" then
codeBlock.name = value
end
elseif tag == "method" then
codeBlock.type = "method"
if value ~= "" then
codeBlock.name = value
end
else
commentRow = nil --Remove line
codeBlock.tags[tag] = value --Todo: check for multiple of the same type
end
elseif not haveTitle then
haveTitle = true
codeBlock.title = commentRow
else
codeBlock.description = codeBlock.description .. commentRow .."\n"
end
end
--Skip ]]
i = i + 1
end
while lines[i] and not isComment(lines[i]) do
if codeBlock.code then
codeBlock.code = codeBlock.code .. "\n" .. lines[i]
else
codeBlock.code = lines[i]
end
i = i + 1
end
if codeBlock.code then
--Nothing to do
if codeBlock.type == "file" then
codeBlock.code = ""
end
if codeBlock.type == "" then
print (("Warning: No type was given for comment block in %s:%i"):format(file, startI))
end
local codeLines = codeBlock.code:split("\n")
codeBlock.codeLines = {}
for i, row in pairs(codeLines) do
row = row:trim()
if row ~= "" then
table.insert(codeBlock.codeLines, row)
local doBreak = false
if codeBlock.type == "class" then
row:gsub("(.-) = (.*):extend()", function(name, parent)
name = name:gsub("^local ", ""):gsub("^module%.", "")
if codeBlock.name == "" then
codeBlock.name = name
end
if codeBlock.parent == "" then
codeBlock.parent = parent
end
doBreak = true
end)
elseif codeBlock.type == "method" then
row:gsub("function (.-):(.-)%((.*)%)", function(class, name, args)
if codeBlock.name == "" then
codeBlock.name = name
end
if codeBlock.parent == "" then
codeBlock.parent = class
end
codeBlock.args = args
doBreak = true
end)
elseif i < 10 then
p("CODE", codeBlock.type, codeBlock.title, row)
end
if doBreak then
break
end
end
end
end
--Set main block
if codeBlock.type == "file" or codeBlock.type == "module" then
block:loopAll(function(row)
print(("WARNING: %s will be ignored, perhaps a late @module or @file declaration?"):format(row.name))
end)
block = codeBlock
elseif codeBlock.parent and codeBlock.parent ~= "" then
local found = false
block:loopAll(function(row)
if row.name == codeBlock.parent then
row:addSubBlock(codeBlock)
found = true
end
end)
if not found then
print (("Could not find parent: %s"):format(codeBlock.parent))
block:addSubBlock(codeBlock)
end
elseif codeBlock.name ~= "" then
block:addSubBlock(codeBlock)
else
print(("Warning: no code blocks detected in file: %s"):format(file))
end
until i >= #lines
local title = "File "..block.file
if block.type == "module" then
title = "Module "..block.name
end
local str = "# "..title .. "\n"
if block.title ~= "" then
str = str .. "**" .. block.title .. "**\n"
str = str .. block.description .. "\n"
end
--Parse blocks
--TODO: recursion!
block:loopAll(function(row, depth)
str = str .. ("#"):rep(depth+2)..row.name .. "\n"
if row.title ~= "" then
str = str .. "**"..row.title.."**\n"
str = str .. row.description .. "\n"
str = str .. "```lua\n" .. row.codeLines[1] .. "\n```\n\n"
end
end)
fs.writeFile(niceName, str, function(err)
if err then
print (("Could not write to file: %s (%s)"):format(niceName, err.message))
return
end
end)
end)
end
end
local function runDir(directory, cb)
fs.readdir(directory, function (err, files)
if err then
--Try if it's a file
runFile(directory)
return
end
for i, dir in pairs(files) do
runDir(directory.."/"..dir, function() end)
end
cb()
end)
end
runDir(directory, function()
end)