Module:Module introspection: Difference between revisions
No edit summary |
try to bugfix line count again |
||
| Line 11: | Line 11: | ||
if not content then return "" end | if not content then return "" end | ||
local lines = {} | |||
local in_multiline = false | |||
local end_pattern | |||
for line in content:gmatch("([^\n]*)\n") do | |||
local processed = line | |||
return | if in_multiline then | ||
-- inside a multi-line comment: replace everything with spaces | |||
processed = processed:gsub(".", " ") | |||
-- check for end of multi-line comment | |||
if processed:find(end_pattern) then | |||
in_multiline = false | |||
end | |||
else | |||
-- check for start of multi-line comment | |||
local start_eq = processed:match("%-%-%[(=*)%[") | |||
if start_eq then | |||
in_multiline = true | |||
end_pattern = "%]" .. start_eq .. "%]" | |||
processed = processed:gsub(".", " ") | |||
else | |||
-- replace single-line comments | |||
processed = processed:gsub("%-%-.*", function(s) | |||
return string.rep(" ", #s) | |||
end) | |||
end | |||
end | |||
table.insert(lines, processed) | |||
end | |||
-- handle last line if it doesn’t end with newline | |||
local last_line = content:match("([^\n]+)$") | |||
if last_line then | |||
local processed = last_line | |||
if in_multiline then | |||
processed = processed:gsub(".", " ") | |||
else | |||
processed = processed:gsub("%-%-.*", function(s) return string.rep(" ", #s) end) | |||
end | |||
table.insert(lines, processed) | |||
end | |||
return table.concat(lines, "\n") | |||
end | end | ||
| Line 116: | Line 148: | ||
local funcs = {} | local funcs = {} | ||
if content then | if content then | ||
local | local line_num = 0 | ||
for line in content:gmatch("[^\n] | for line in content:gmatch("([^\n]*)\n?") do -- Make sure gmatch does not skip blank lines | ||
line_num = line_num + 1 | |||
-- Match functions defined as function p.name( | -- Match functions defined as function p.name( | ||
local name = line:match("function%s+[%w_]+%.([%w_]+)%s*%(") | local name = line:match("function%s+[%w_]+%.([%w_]+)%s*%(") | ||
if name then | if name then | ||
table.insert(funcs, {name = name, line = | table.insert(funcs, {name = name, line = line_num}) | ||
end | end | ||
| Line 129: | Line 161: | ||
name = line:match("[%w_]+%.([%w_]+)%s*=%s*function%s*%(") | name = line:match("[%w_]+%.([%w_]+)%s*=%s*function%s*%(") | ||
if name then | if name then | ||
table.insert(funcs, {name = name, line = | table.insert(funcs, {name = name, line = line_num}) | ||
end | end | ||
end | end | ||
| Line 139: | Line 171: | ||
-- Main function; to be called by wrapper | -- Main function; to be called by wrapper | ||
function p._module_introspection(args) | function p._module_introspection(args) | ||
local module_name = args["module_name" ] | local args = args or {} | ||
local module_name = args["module_name" ] or "MOS" | |||
local main_function = args["main_function"] | local main_function = args["main_function"] | ||