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
|
--[[
Copyright (c) 2013 Remko Tronçon
Licensed under the GNU General Public License.
See the COPYING file for more information.
--]]
require "sluift"
local function get_anchor(...)
return table.concat({...}, "-")
end
local function convert_links(text)
result = text:gsub("(@{(%w*)})", "[`%2`](#%2)")
return result
end
local function add_help(help, document, class, level)
-- Collect help of children
local methods = {}
for _, method in pairs(help.methods or {}) do
local description
local method_help = sluift.get_help(method.ref)
if method_help and method_help.description then
description = method_help.synopsis
end
methods[#methods+1] = { name = method.name, description = description, ref = method.ref }
end
local fields = sluift.copy(help.fields or {})
table.sort(methods, function (a, b) return (a.name or "") < (b.name or "") end)
table.sort(fields, function (a, b) return (a.name or "") < (b.name or "") end)
local classes = {}
for _, class in pairs(help.classes or {}) do
classes[#classes+1] = { name = class, description = sluift.get_help(class).synopsis }
end
table.insert(document, convert_links(help.description or ""))
for _, p in ipairs({
{"Parameters", help.parameters}, {"Options", help.options}, {"Fields", fields}, {"Methods", methods}}) do
if p[2] and next(p[2]) ~= nil then
table.insert(document, "\n\n" .. level .. " " .. p[1] .. "\n")
for _, parameter in ipairs(p[2]) do
parameter_name = "`" .. parameter.name .. "`"
if p[1] == "Methods" then
parameter_name = "[" .. parameter_name .. "](#" .. get_anchor(class, parameter.name) .. ")"
end
if parameter.description then
table.insert(document, "- " .. parameter_name .. ": " .. convert_links(parameter.description))
else
table.insert(document, "- " .. parameter_name)
end
end
if p[1] == "Methods" then
for _, method in ipairs(p[2]) do
table.insert(document, "\n#### <a name=\"" .. get_anchor(class, method.name) .. "\"></a> `" .. method.name .. "`\n")
if method.ref then
add_help(sluift.get_help(method.ref) or {}, document, class, level .. "#")
end
end
end
end
end
end
local document = {}
table.insert(document, [[
# Sluift
This document describes the API of the `sluift` module.
The entry points of Sluift are in the `sluift` module, described below.
## `sluift` module
]])
help = sluift.get_help(sluift)
add_help(help, document, "sluift", "###")
for _, class in pairs(help.classes) do
class_help = sluift.get_help(class)
if class_help then
table.insert(document, "\n## <a name=\"" .. class .. "\"></a> `" .. class .. "` class\n")
add_help(class_help, document, class, "###")
end
end
document = table.concat(document, "\n") .. "\n"
print(document)
|