Module:Template introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
Created page with "-- This module follows User:Ganaram inukshuk/Provisional style guide for Lua local getArgs = require("Module:Arguments").getArgs local p = {} -- Inspects a template for any modules' functions being invoked. -- TODO: write wrapper -- Helper function: preprocess wikitext so that: -- - Only text inside includeonly tags is captured. -- - That captured text has normalized spacing and line breaks function p.preprocess_wikitext(wikitext) if not wikitext or wikitext == "..."
 
Ganaram inukshuk (talk | contribs)
mNo edit summary
 
(16 intermediate revisions by the same user not shown)
Line 5: Line 5:


-- Inspects a template for any modules' functions being invoked.
-- Inspects a template for any modules' functions being invoked.
-- TODO: write wrapper


-- Helper function: preprocess wikitext so that:
-- Helper function: preprocess wikitext so that:
Line 36: Line 35:
:gsub("%s+", " ")
:gsub("%s+", " ")
:match("^%s*(.-)%s*$")
:match("^%s*(.-)%s*$")
-- Normalize more by removing subst, safesubst, and any extra curly brackets
normalized = normalized
:gsub("{{{|safesubst:}}}", "")  -- Remove {{{{|safesubst:}}} if used
:gsub("{{{|subst:}}}", "")      -- Remove {{{{|subst:}}} if used
:gsub("{{{{", "{{")            -- Collapse excessive curly brackets
:gsub("}}}}", "}}")            -- Collapse excessive curly brackets


return normalized
return normalized
Line 54: Line 60:
return results
return results
end
function p.make_hatnote(invokes)
local hatnote = ""
if #invokes == 0 then
hatnote = "This template does not invoke any functions."
return string.format(":''%s''\n", hatnote)
else
hatnote = "This template invokes the following functions:"
local calls = {}
for _, call in ipairs(invokes) do
table.insert(calls, string.format("'''%s''' from [[Module:%s]]", call.func, call.module))
end
return string.format(":''%s %s''\n", hatnote, table.concat(calls, ", "))
end
end
end


function p._template_introspection(args)
function p._template_introspection(args)
args = args or {}
local template_name = args["template_name"]
local template_name = args["Template Name"] or "Template:Module introspection"


-- Get template wikitext
-- Get template wikitext
Line 66: Line 86:
end
end


-- Get template's code and preprocess
local wikitext = titleObj:getContent()
local wikitext = titleObj:getContent()
wikitext = p.preprocess_wikitext(wikitext)
wikitext = p.preprocess_wikitext(wikitext)
Line 73: Line 94:
end
end
-- Run invoke finder
-- Run invoke finder and return as a hatnote
local invokes = p.find_invokes(wikitext)
local invokes = p.find_invokes(wikitext)
return p.make_hatnote(invokes)
end
function p.template_introspection(frame)
local args = getArgs(frame) or {}
-- Format result for readability (or later, you could return a table)
-- Get template name, with namespace, and strip /doc if present
local out = {}
args["template_name"] = args["template_name"] or mw.title.getCurrentTitle().prefixedText
table.insert(out, "== Invoked modules ==")
args["template_name"] = args["template_name"]:gsub("/doc$", "")
for _, call in ipairs(invokes) do
table.insert(out, string.format("* %s → %s", call.module, call.func))
end
if #invokes == 0 then
table.insert(out, "No #invoke calls found.")
end
return table.concat(out, "\n")
return p._template_introspection(args)
end
end


return p
return p