Module:Template introspection: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
| (14 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. | ||
-- 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) | ||
local template_name = args[" | local template_name = args["template_name"] | ||
-- Get template wikitext | -- Get template wikitext | ||
| Line 65: | 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 72: | 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 | end | ||
| Line 92: | Line 102: | ||
local args = getArgs(frame) or {} | local args = getArgs(frame) or {} | ||
-- Get template name, and strip /doc | -- Get template name, with namespace, and strip /doc if present | ||
args["template_name"] = args["template_name"] or mw.title.getCurrentTitle().prefixedText | |||
template_name = template_name:gsub("/doc$", "") | args["template_name"] = args["template_name"]:gsub("/doc$", "") | ||
return | return p._template_introspection(args) | ||
end | end | ||
return p | return p | ||
Latest revision as of 10:00, 28 October 2025
- This module should not be invoked directly; use its corresponding template instead: Template:Template introspection.
| Module:Template introspection is deprecated and has no replacement. Further use of this module is not advised. This module is kept for historical purposes and should not be deleted.
Details: See Template:Dochead. |
| Introspection summary for Module:Template introspection | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| ||||||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
-- 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.
-- 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 == "" then
return ""
end
-- Gather all includeonly sections, if any exist
local includeonly_blocks = {}
for block in wikitext:gmatch("<includeonly>(.-)</includeonly>") do
table.insert(includeonly_blocks, block)
end
local text_to_scan
if #includeonly_blocks > 0 then
text_to_scan = table.concat(includeonly_blocks, " ")
else
text_to_scan = wikitext
end
-- Normalize:
-- 1. Convert all newlines to spaces
-- 2. Collapse multiple spaces/tabs/newlines
-- 3. Trim leading/trailing whitespace
local normalized = text_to_scan
:gsub("[\r\n]", " ")
:gsub("%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
end
-- Find all invokes in a wikitext string
function p.find_invokes(wikitext)
if not wikitext or wikitext == "" then
return {}
end
-- Capture all invocations and store them in a table
local results = {}
local pattern = "{{%s*#invoke%s*:%s*([^|}%c]+)%s*|%s*([^|}%c]+)"
for module, func in wikitext:gmatch(pattern) do
table.insert(results, { module = module, func = func })
end
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
function p._template_introspection(args)
local template_name = args["template_name"]
-- Get template wikitext
local titleObj = mw.title.new(template_name)
if not titleObj or not titleObj.exists then
return string.format("Template '%s' does not exist.", template_name)
end
-- Get template's code and preprocess
local wikitext = titleObj:getContent()
wikitext = p.preprocess_wikitext(wikitext)
if not wikitext then
return string.format("Could not retrieve content for '%s'.", template_name)
end
-- Run invoke finder and return as a hatnote
local invokes = p.find_invokes(wikitext)
return p.make_hatnote(invokes)
end
function p.template_introspection(frame)
local args = getArgs(frame) or {}
-- Get template name, with namespace, and strip /doc if present
args["template_name"] = args["template_name"] or mw.title.getCurrentTitle().prefixedText
args["template_name"] = args["template_name"]:gsub("/doc$", "")
return p._template_introspection(args)
end
return p