Module:Introspection utils: Difference between revisions
mNo edit summary |
No edit summary |
||
| (15 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
-- TODO: | -- TODO: | ||
-- - | -- Add support for detecting modules providing data, not just functions. | ||
-- Address edge case of lazy-loaded modules (loaded within a function) | |||
-- Common functions for module and template introspection. | -- Common functions for module and template introspection. | ||
| Line 82: | Line 83: | ||
end | end | ||
-- Normalize | -- Normalize tags | ||
- | text_to_scan = text_to_scan | ||
-- | :gsub("<noinclude>.-</noinclude>", "") | ||
-- | :gsub("<noinclude%s*/>", "") | ||
:gsub("<includeonly>", "") | |||
:gsub("</includeonly>", "") | |||
:gsub("<onlyinclude>", "") | |||
:gsub("</onlyinclude>", "") | |||
-- Normalize subst/safesubst | |||
text_to_scan = text_to_scan | |||
:gsub("{{+%s*[Ss]afesubst:", "{{") | |||
:gsub("{{+%s*[Ss]ubst:", "{{") | |||
-- Normalize spaces | |||
text_to_scan = text_to_scan | |||
:gsub("[\r\n]", " ") | :gsub("[\r\n]", " ") | ||
:gsub("%s+", " ") | :gsub("%s+", " ") | ||
:match("^%s*(.-)%s*$") | :match("^%s*(.-)%s*$") | ||
return | return text_to_scan | ||
end | end | ||
| Line 113: | Line 118: | ||
end | end | ||
end | end | ||
--[[ | |||
function p.get_and_preprocess_content(fullpagename) | |||
if not fullpagename then return "" end | |||
local title = mw.title.new(fullpagename) | |||
local content = title and title:getContent() or "" | |||
local lowercase_name = string.lower(fullpagename) | |||
if string.match(lowercase_name, "^module:") then | |||
return p.preprocess_code(content) | |||
elseif string.match(lowercase_name, "^template:") then | |||
return p.preprocess_wikitext(content) | |||
else | |||
return "" | |||
end | |||
end | |||
]]-- | |||
-- ----------------------------------------------------------------------------- | -- ----------------------------------------------------------------------------- | ||
| Line 279: | Line 302: | ||
-- Trim and replace spaces with underscores | -- Trim and replace spaces with underscores | ||
local raw_module_name = mw.text.trim( | local raw_module_name = mw.text.trim(raw_module_name):gsub(" ", "_") | ||
-- Add namespace if missing | -- Add namespace if missing | ||
| Line 307: | Line 330: | ||
end | end | ||
-- Check whether a template invokes a function from a module. | local function normalize_module_name(name) | ||
-- | if not name then return "" end | ||
-- Trim, replace spaces with underscores, remove namespace, lowercase first | |||
function p.invocation_exists( | -- letter | ||
name = mw.text.trim(name) | |||
name = name:gsub(" ", "_") | |||
name = name:gsub("^[Mm]odule:", "") | |||
name = name:gsub("^%a", string.lower) | |||
return name | |||
end | |||
-- Check whether a template invokes a function from a module. Input is a table | |||
-- of invocations, from find_invokes, and mod_name is the module name. | |||
function p.invocation_exists(invokes, mod_name) | |||
if type(invokes) ~= "table" then | |||
return false | |||
end | |||
local target_mod = normalize_module_name(mod_name) | |||
for _, invoke in ipairs(invokes) do | |||
if normalize_module_name(invoke["module"]) == target_mod then | |||
return true | |||
end | |||
end | end | ||
return false | |||
return | |||
end | end | ||
function p.tester() | function p.tester() | ||
local | local wikitext = p.get_and_preprocess_content("Template", "MOS intro") | ||
return p.invocation_exists( | local invokes = p.find_invokes(wikitext) | ||
return p.invocation_exists(invokes, "MOS intro") | |||
end | end | ||
return p | return p | ||