-
Notifications
You must be signed in to change notification settings - Fork 0
/
docroc.lua
75 lines (64 loc) · 1.79 KB
/
docroc.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
-- docroc v0.1.0 - Lua documentation generator
-- https://github.com/bjornbytes/docroc
-- License - MIT, see LICENSE for details.
local docroc = {}
function docroc.process(filename)
local file = io.open(filename, 'r')
local text = file:read('*a')
file:close()
local comments = {}
text:gsub('%s*%-%-%-(.-)\n([%w\n][^\n%-]*)', function(chunk, context)
chunk = chunk:gsub('^%s*%-*%s*', ''):gsub('\n%s*%-*%s*', ' ')
chunk = chunk:gsub('^[^@]', '@description %1')
context = context:match('[^\n]+')
local tags = {}
chunk:gsub('@(%w+)%s?([^@]*)', function(name, body)
body = body:gsub('(%s+)$', '')
local processor = docroc.processors[name]
local tag = processor and processor(body) or {}
tag.tag = name
tag.text = body
tags[name] = tags[name] or {}
table.insert(tags[name], tag)
table.insert(tags, tag)
end)
table.insert(comments, {
tags = tags,
context = context
})
end)
return comments
end
docroc.processors = {
arg = function(body)
local name = body:match('^%s*(%w+)') or body:match('^%s*%b{}%s*(%w+)')
local description = body:match('%-%s*(.*)$')
local optional, default
local type = body:match('^%s*(%b{})'):sub(2, -2):gsub('(%=)(.*)', function(_, value)
optional = true
default = value
if #default == 0 then default = nil end
return ''
end)
return {
type = type,
name = name,
description = description,
optional = optional,
default = default
}
end,
returns = function(body)
local type
body:gsub('^%s*(%b{})', function(match)
type = match:sub(2, -2)
return ''
end)
local description = body:match('^%s*(.*)')
return {
type = type,
description = description
}
end
}
return docroc