Module:Introspection utils: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 324: Line 324:
return title and title.exists
return title and title.exists
end
local function normalize_module_name(name)
if not name then return "" end
-- Trim, replace spaces with underscores, remove namespace
name = mw.text.trim(name)
name = name:gsub(" ", "_")
name = name:gsub("^[Mm]odule:", "")
return name
end
end


Line 329: Line 338:
-- of invocations, from find_invokes, and mod_name is the module name.
-- of invocations, from find_invokes, and mod_name is the module name.
function p.invocation_exists(invokes, mod_name)
function p.invocation_exists(invokes, mod_name)
-- Normalize module name
if type(invokes) ~= "table" then
local function normalize(name)
return false
if type(name) ~= "string" then
name = tostring(name or "")
end
name = name:gsub("_", " "):lower():gsub("^module:%s*", "")
return name
end
end
local target_mod = normalize_module_name(mod_name)


local target_mod = normalize(mod_name)
-- Search
for _, invoke in ipairs(invokes) do
for _, invoke in ipairs(invokes) do
if normalize(invoke.module) == target_mod then
if normalize_module_name(invoke["module"]) == target_mod then
return true
return true
end
end
end
end
return false
return false
end
function p.tester()
local wikitext = p.get_and_preprocess_content("Template", "MOS intro")
local invokes = p.find_invokes(wikitext)
return p.invocation_exists(invokes, "MOS intro")
end
end


return p
return p