forked from michal-h21/LuaXML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldoc-latex.lua
More file actions
83 lines (78 loc) · 2.45 KB
/
ldoc-latex.lua
File metadata and controls
83 lines (78 loc) · 2.45 KB
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
local function escape(s)
local escapes = {
_ = "\\_{}",
["\\"] = "\\backspace{}",
["%"] = "\\%",
["{"] = "\\{",
["}"] = "\\}",
}
-- only process strings
if type(s) == "string" then
s = s:gsub("%s+", " ")
return s:gsub("([_\\%%{}])", function(a) return escapes[a] end)
end
return s
end
local function print_template(format, s)
print(string.format(format, escape(s)))
end
local function print_module(mod)
print_template("\\modulename{%s}", mod.mod_name)
print_template("\\modulesummary{%s}", mod.summary)
-- for k,v in pairs(mod.sections.by_name) do print("mod", k,v) end
end
local function print_class(mod, class, items)
print_template("\\moduleclass{%s}", class)
local items = items or {}
for _, item in ipairs(items) do
local par = {}
local map = item.params.map or {}
for k,v in ipairs(item.params or {}) do
par[#par+1] = escape(v)
end
print(string.format("\\functionname{%s}{%s}", escape(item.name), table.concat(par, ", ")))
print_template("\\functionsummary{%s}", item.summary)
for x,y in ipairs(item.params) do
print(string.format("\\functionparam{%s}{%s}", escape(y), escape(map[y])))
-- print(x,y)
end
for _, ret in ipairs(item.ret or {}) do
print_template("\\functionreturn{%s}", ret)
end
-- print(string.format("\\functionreturn{%s}", escape(item.ret ) ))
end
end
return {
filter = function (t)
local modules = {}
local class_sequence = {}
for modid, mod in ipairs(t) do
-- print basic information about module
print_module(mod)
local classes = {}
for _, item in ipairs(mod.items) do
if item.type == 'function' or item.type == "lfunction" then
-- move functions to tables corresponding to their classes
local curr_class = item.section
if curr_class then
local class = classes[curr_class] or {}
class[#class+1] = item
-- we want to list classes in the order as they appear in the module
if not classes[curr_class] then table.insert(class_sequence, curr_class) end
classes[curr_class] = class
end
end
end
for _,k in ipairs(class_sequence) do
local v = classes[k]
if k == "lfunction" then
k = "Local functions"
elseif k == "function" then
k = "Functions"
end
-- print class info and functions
print_class(mod, k,v)
end
end
end
}