Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
try to bugfix line count again
Line 11: Line 11:
if not content then return "" end
if not content then return "" end


-- Step 1: blank multi-line comments including --[[ ... ]] and --[=[ ... ]=]
local lines = {}
content = content:gsub("%-%-%[(=*)%[(.-)%]%1%]", function(eq, body)
local in_multiline = false
-- Replace everything except newlines with spaces
local end_pattern
local blank = body:gsub("[^\n]", " ")
return "--[[" .. blank .. "]]"
end)


-- Step 2: blank single-line comments
for line in content:gmatch("([^\n]*)\n") do
content = content:gsub("%-%-[^\n]*", function(s)
local processed = line
return s:gsub("[^\n]", " ")
end)


return content
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 lineNumber = 0
local line_num = 0
for line in content:gmatch("[^\n]+") do
for line in content:gmatch("([^\n]*)\n?") do -- Make sure gmatch does not skip blank lines
lineNumber = lineNumber + 1
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 = lineNumber})
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 = lineNumber})
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"]