Repository navigation
Expand file tree
/
Copy pathmodules.lua.in
More file actions
executable file
·219 lines (206 loc) · 6.72 KB
/
Copy pathmodules.lua.in
File metadata and controls
executable file
·219 lines (206 loc) · 6.72 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
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
#!@LUA_EXECUTABLE@
--
-- Copyright © 2012 Peter Colberg
--
-- This file is part of HALMD.
--
-- HALMD is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as
-- published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General
-- Public License along with this program. If not, see
-- <http://www.gnu.org/licenses/>.
--
--- Parse inline documentation from Lua module
--
-- :param sourcedir: path to source tree
-- :param source: path of source file relative to source tree
-- :returns: table with parser result
--
function parse_module(sourcedir, source)
local parsed = {}
parsed.file = source
parsed.path = ("%s/%s"):format(sourcedir, source)
parsed.docpath = source:gsub("%..*$", ""):gsub("/init$", "/index"):gsub("^halmd/", "")
parsed.module = source:gsub("%..*$", ""):gsub("/init$", ""):gsub("/", ".")
parsed.name = parsed.module:gsub("^.*%.", "")
parsed.namespace = parsed.module:gsub("%.[^.]*$", "")
local entry
for line in io.lines(parsed.path) do
if not entry then
local m = line:match("^%-%-%-%s?(.-)%s*$")
if m then
if entry then
for i = #entry, 1, -1 do
if #entry[i] > 0 then
break
end
table.remove(entry)
end
table.insert(parsed, entry)
end
entry = {}
if #m > 0 then
table.insert(entry, m)
end
end
else
local m = line:match("^%-%-%s?(.-)%s*$")
if m then
if #m > 0 or #entry > 0 then
table.insert(entry, m)
end
else
local mod, sep, func, name = line:match("^function%s*([%w_]+)([.:])(([%w_]+)%([^)]*%))")
if mod then
if sep == ":" then
entry.class = mod
else
entry.module = mod
end
entry.func = func
entry.name = name
else
local func = line:match("%s*module%s*%(%s*function%s*(%([^)]*%))")
if func then
entry.func = func
end
end
for i = #entry, 1, -1 do
if #entry[i] > 0 then
break
end
table.remove(entry)
end
table.insert(parsed, entry)
entry = nil
end
end
end
return parsed
end
--- Render result of module parser as reStructuredText
--
-- :param parsed: table with parser result
-- :returns: string with reStructuredText output
--
function render_module(parsed)
local lines = {}
local l = function(line, ...)
table.insert(lines, line and (line .. "\n"):format(...) or "\n")
end
l(".. This file was generated from %s", parsed.file)
l()
l(".. highlight:: lua")
l()
l(".. module:: %s", parsed.module)
for i = 1, #parsed do
local entry = parsed[i]
if entry.func and not entry.name then
l(" :noindex:")
break
end
end
l()
for i = 1, #parsed do
local entry = parsed[i]
local indent = ""
local constructor = false
local methods = {}
if entry.func then
if not entry.name then
constructor = true
l(".. currentmodule:: %s", parsed.namespace)
l()
l(".. class:: %s%s", parsed.name, entry.func)
elseif entry.module then
for i = 1, #parsed do
if parsed[i].class == entry.name then
table.insert(methods, parsed[i])
end
end
if #methods > 0 then
l(".. class:: %s", entry.func)
else
l(".. function:: %s", entry.func)
end
end
l()
indent = " "
end
if not entry.class then
for j = 1, #entry do
l("%s%s", indent, entry[j])
end
l()
end
if constructor then
l(".. currentmodule:: %s.%s", parsed.namespace, parsed.name)
l()
end
if #methods > 0 then
for i = 1, #methods do
local entry = methods[i]
l("%s.. method:: %s", indent, entry.func)
l()
for j = 1, #entry do
l("%s%s%s", indent, indent, entry[j])
end
l()
end
end
end
return table.concat(lines, "")
end
--- Parse arguments from command line
--
-- :param args: array with command line arguments
-- :returns: table with pairs of argument name and value
--
function parse_args(arg)
if #arg < 3 then
error("Usage: modules.lua sourcedir outdir filename [filename...]")
end
local sources = {}
for i = 3, #arg do
table.insert(sources, arg[i])
end
return {sourcedir = arg[1], outdir = arg[2], sources = sources}
end
-- table with output directory as key and directory creation function as value
local makedirs = {}
-- list of module creation functions
local makemods = {}
local args = assert(parse_args(arg))
for i = 1, #args.sources do
local parsed = parse_module(args.sourcedir, args.sources[i])
local outfile = ("%s/%s.rst"):format(args.outdir, parsed.docpath)
local outdir = outfile:gsub("/[^/]+$", "")
if #parsed > 0 then
-- store reStructuredText write function for delayed execution
table.insert(makemods, function()
local f = assert(io.open(outfile, "w"))
f:write(render_module(parsed))
f:close()
end)
-- ensure cmake is executed only once per unique directory
makedirs[outdir] = function()
os.execute(("@CMAKE_COMMAND@ -E make_directory \"%s\""):format(outdir))
end
end
end
-- create output directories for documented modules
for dir, makedir in pairs(makedirs) do
makedir()
end
-- write reStructuredText files for documented modules
for i = 1, #makemods do
makemods[i]()
end