Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
todo
Ganaram inukshuk (talk | contribs)
remove dependency-tracking code; this will be rewritten from the ground-up
Line 107: Line 107:
end
end
return deps
return deps
end
-- Helper function
-- Find functions used by each dependency
-- TODO: refactor so it references a table produced by the above function.
-- By matching strings of the form "([_%a][_%w%.]*)%s*%(", it can determine
-- what dependency is being used. Table produced should have entries in this
-- form:
-- ["med"] = { "find_mediants", "find_deepest" }
-- Each entry in that smaller table is a function that was used
function p.find_dependency_functions(preprocessed_module)
-- Step 1: Find all require statements with optional .function
local results = {}
for var, dep, entry in preprocessed_module: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 preprocessed_module:gmatch(var .. "%s*%(") do
used[entry] = true
end
else
-- The variable is a module table: var.func() usage
for func in preprocessed_module:gmatch(var .. "%.(%w+)%s*%(") do
used[func] = true
end
end
-- Step 3: Build result entry
local func_list = {}
for f in pairs(used) do table.insert(func_list, f) end
table.sort(func_list)
results[dep] = results[dep] or {}
table.insert(results[dep], {
variable = var,
entry = (entry ~= "" and entry or nil),
functions = func_list
})
end
return results
end
end


Line 181: Line 135:


return funcs
return funcs
end
-- Helper function; builds the table of dependencies
-- All dependencies are included regardless of use
function p.make_dependency_table(deps, dep_functions)
local dep_lines = {}
--.insert(dep_lines, string.format("'''Module:%s''' requires the following dependencies:", module_name))
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")
for _, dep in ipairs(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, "<br />")
if usage.entry then
func_str = usage.entry .. (func_str ~= "" and ("<br />" .. 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, '|}')
return dep_lines
end
end


Line 264: Line 179:
function p._module_introspection(args)
function p._module_introspection(args)
local args = args or {}
local args = args or {}
local module_name  = args["module_name"  ] or "Numlinks"
local module_name  = args["module_name"  ]
local main_function = args["main_function"]
local main_function = args["main_function"]
Line 273: Line 188:
-- Get dependencies, their functions used, then build a table
-- Get dependencies, their functions used, then build a table
local deps = p.find_dependencies(preprocessed_content)
local dep_functions = p.find_dependency_functions(preprocessed_content)
local dep_lines = p.make_dependency_table(deps, dep_functions)


-- Get module's functions, then build a table using that information
-- Get module's functions, then build a table using that information
Line 282: Line 195:


-- Return the tables as strings
-- Return the tables as strings
local summary = string.format("'''Introspection summary:''' Module:%s requires %d module(s) and provides %d functions(s).", module_name, #deps, #module_functions)
local summary = string.format("'''Introspection summary:''' Module:%s provides %d functions(s).", module_name, #module_functions)
return summary .. "\n" .. table.concat(dep_lines, "\n") .. "\n" .. table.concat(func_lines, "\n")
return summary .. "\n" .. table.concat(func_lines, "\n")
end
end