Module:Module introspection: Difference between revisions
Jump to navigation
Jump to search
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 | ||
Revision as of 04:55, 25 October 2025
- This module should not be invoked directly; use its corresponding template instead: Template:Module introspection.
| Dependency | Variable | Function(s) used |
|---|
| Function | Line |
|---|
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local getArgs = require("Module:Arguments").getArgs
local p = {}
-- Inspects a module for its functions, its dependencies, and the functions used
-- 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
function p.list_dependencies(module_name)
local module_name = module_name --or "MOS" -- Test arg; comment out when not testing
local title = mw.title.new('Module:' .. module_name)
local content = title:getContent()
-- Blank-out comments
content = p.strip_comments(content)
-- Get dependencies for that module
-- It's the text from each "require('Module:aaaaa')" line.
local deps = {}
for dep in content:gmatch("require%s*%(%s*['\"]Module:(.-)['\"]%s*%)") do
table.insert(deps, dep)
end
return deps
end
-- Helper function
-- List functions used from each dependency
-- This assumes dependency functions are used in the format of: var.func(),
-- or var() if the module is included as var = require("Module:Dep") if it
-- returns a function or var = require("Module:Dep").func_name if a specific
-- function from that module is used.
-- Optionally accepts a table of its dependencies; if not provided, it will find
-- them itself by calling list_dependencies
function p.list_dependency_functions(module_name, deps)
local module_name = module_name --or "MOS" -- Test arg; comment out when not testing
-- Get dependencies
local deps = p.list_dependencies(module_name)
-- Load module
local title = mw.title.new('Module:' .. module_name)
local content = title:getContent()
-- Step 1: Find all require statements with optional .function
local results = {}
for var, dep, entry in content:gmatch(
"local%s+([%w_]+)%s*=%s*require%s*%(%s*['\"]Module:(.-)['\"]%s*%)%.?([%w_]*)"
) do
local used = {}
-- Step 2: Check how it's used
if entry ~= "" then
-- The module returned a single function: var() usage
for _ in content:gmatch(var .. "%s*%(") do
used[entry] = true
end
else
-- The variable is a module table: var.func() usage
for func in content:gmatch(var .. "%.(%w+)%s*%(") do
used[func] = true
end
end
-- Step 3: Build result entry
local funcList = {}
for f in pairs(used) do table.insert(funcList, f) end
table.sort(funcList)
results[dep] = results[dep] or {}
table.insert(results[dep], {
variable = var,
entry = (entry ~= "" and entry or nil),
functions = funcList
})
end
return results
end
-- Helper function
-- List functions provided by a module
-- Only returned functions are considered, not local functions
function p.list_functions(module_name)
local module_name = module_name --or "MOS" -- Test arg; comment out when not testing
-- Load module
local title = mw.title.new('Module:' .. module_name)
local content = title:getContent()
-- Blank-out comments
content = p.strip_comments(content)
-- Iterate through file and find each function and the line found at
local funcs = {}
if content then
local lineNumber = 0
for line in content:gmatch("[^\n]+") do
lineNumber = lineNumber + 1
-- Match functions defined as function p.name(
local name = line:match("function%s+[%w_]+%.([%w_]+)%s*%(")
if name then
table.insert(funcs, {name = name, line = lineNumber})
end
-- Match functions defined as p.name = function(
name = line:match("[%w_]+%.([%w_]+)%s*=%s*function%s*%(")
if name then
table.insert(funcs, {name = name, line = lineNumber})
end
end
end
return funcs
end
-- Main function; to be called by wrapper
function p._module_introspection(args)
local module_name = args["module_name"]
local main_function = args["main_name" ]
-- Get module functions
local module_functions = p.list_functions(module_name)
-- Get dependencies and functions used from each
local dep_functions = p.list_dependency_functions(module_name)
-- Build MediaWiki table for dependencies
local dep_lines = {}
table.insert(dep_lines, '{| class="wikitable sortable"')
table.insert(dep_lines, '|+ Dependencies and functions used')
table.insert(dep_lines, "! Dependency")
table.insert(dep_lines, "! Variable")
table.insert(dep_lines, "! Function(s) used")
-- Include all dependencies even if no usage is detected
local all_deps = p.list_dependencies(module_name)
for _, dep in ipairs(all_deps) do
local dep_link = string.format("[[Module: %s]]", dep)
local usages = dep_functions[dep]
if usages and #usages > 0 then
for _, usage in ipairs(usages) do
local func_str = table.concat(usage.functions, ", ")
if usage.entry then
func_str = usage.entry .. (func_str ~= "" and (", " .. func_str) or "")
end
table.insert(dep_lines, "|-")
table.insert(dep_lines, "| " .. dep_link)
table.insert(dep_lines, "| " .. usage.variable)
table.insert(dep_lines, "| " .. (func_str ~= "" and func_str or "''dependency not used''"))
end
else
table.insert(dep_lines, "|-")
table.insert(dep_lines, "| " .. dep_link)
table.insert(dep_lines, "| -")
table.insert(dep_lines, "| ''dependency not used''")
end
end
table.insert(dep_lines, '|}')
-- Build MediaWiki table for module's own functions
local func_lines = {}
local func_class = "wikitable sortable mw-collapsible"
if #module_functions > 20 then
func_class = func_class .. " mw-collapsed"
end
table.insert(func_lines, "{| class=\"" .. func_class .. "\"")
table.insert(func_lines, "|+ Functions provided by this module")
table.insert(func_lines, "! Function")
table.insert(func_lines, "! Line")
for _, f in ipairs(module_functions) do
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, "| " .. link)
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
-- Wrapper function for modules
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
return p