Module:Module introspection: Difference between revisions
bugfix comment removal |
add main function identification; bugfix comment removal so that it doesn't affect line numbers |
||
| Line 6: | Line 6: | ||
-- from those dependencies. | -- from those dependencies. | ||
-- Helper function | |||
-- Blanks comments but preserves line numbers | |||
function p.strip_comments(content) | |||
if not content then return "" end | |||
-- Replace single-line comments with spaces | |||
content = content:gsub("%-%-.*", function(s) | |||
return string.rep(" ", #s) | |||
end) | |||
-- Replace multi-line comments with spaces, preserving line breaks | |||
content = content:gsub("%-%-%[%[(.-)%]%]", function(s) | |||
local lines = {} | |||
for line in s:gmatch("([^\n]*)\n?") do | |||
table.insert(lines, string.rep(" ", #line)) | |||
end | |||
return table.concat(lines, "\n") | |||
end) | |||
return content | |||
end | |||
-- Helper function | |||
-- List dependencies for a module | -- List dependencies for a module | ||
function p.list_dependencies(module_name) | function p.list_dependencies(module_name) | ||
| Line 13: | Line 36: | ||
local content = title:getContent() | local content = title:getContent() | ||
-- | -- Blank-out comments | ||
content = | content = p.strip_comments(content) | ||
-- Get dependencies for that module | -- Get dependencies for that module | ||
| Line 27: | Line 48: | ||
end | end | ||
-- Helper function | |||
-- List functions used from each dependency | -- List functions used from each dependency | ||
-- This assumes dependency functions are used in the format of: var.func(), | -- This assumes dependency functions are used in the format of: var.func(), | ||
| Line 80: | Line 102: | ||
end | end | ||
-- Helper function | |||
-- List functions provided by a module | -- List functions provided by a module | ||
-- Only returned functions are considered, not local functions | -- Only returned functions are considered, not local functions | ||
| Line 89: | Line 112: | ||
local content = title:getContent() | local content = title:getContent() | ||
-- | -- Blank-out comments | ||
content = | content = p.strip_comments(content) | ||
-- Iterate through file and find each function and the line found at | -- Iterate through file and find each function and the line found at | ||
local funcs = {} | local funcs = {} | ||
| Line 120: | Line 141: | ||
-- 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 module_name = args["module_name"] | ||
local main_function = args["main_name" ] | |||
-- Get module functions | -- Get module functions | ||
| Line 175: | Line 197: | ||
for _, f in ipairs(module_functions) do | for _, f in ipairs(module_functions) do | ||
local link = string.format("[[Module:%s#L-%d|%s]]", module_name, f.line, f.name) | local link = string.format("[[Module:%s#L-%d|%s]]", module_name, f.line, f.name) | ||
-- If the function is the main function, add "main" to that cell | |||
if f.name == main_function then | |||
link = link .. " (main)" | |||
end | |||
table.insert(func_lines, "|-") | table.insert(func_lines, "|-") | ||
table.insert(func_lines, "| " .. link) | table.insert(func_lines, "| " .. link) | ||
table.insert(func_lines, "| " .. f.line) | table.insert(func_lines, "| " .. f.line) | ||
end | |||
table.insert(func_lines, "|}") | |||
-- Return the tables as strings | |||
return table.concat(dep_lines, "\n"), table.concat(func_lines, "\n") | |||
end | end | ||
| Line 188: | Line 216: | ||
-- Wrapper function for modules | -- Wrapper function for modules | ||
function p.module_introspection(frame) | function p.module_introspection(frame) | ||
-- Extract arguments using getArgs | |||
local args = getArgs(frame) | |||
-- Get module name from arguments, or default to current page | |||
local module_name = args["module_name"] or mw.title.getCurrentTitle().text | |||
-- Strip trailing "/doc" if the template is used on a documentation subpage | |||
module_name = module_name:gsub("/doc$", "") | |||
-- Normalize module name so it can be used to find the main function, which | |||
-- is assumed to be the same name as the module. | |||
local main_function = args["main_function"] or "_" .. module_name:gsub("[^%w]", "_") -- Replace all non-alphanumeric characters with underscores | |||
-- Call the introspection function | |||
local dep_table, func_table = p._module_introspection({ | |||
["module_name"] = module_name , | |||
["main_function"] = main_function_name, | |||
}) | |||
-- Return combined tables | |||
return dep_table .. '\n' .. func_table | |||
end | end | ||
return p | return p | ||