Module:Call wikitext
Appearance
Implements {{Call wikitext}}. This module was originally designed for providing an effective way to create testcases for Module:ArgRest, by "mocking" a transcluded template.
Usage
This module can also be used directly. See Template:Call wikitext/doc for documentation and replace {{Call wikitext
with {{#invoke:Call wikitext|main
. For example:
{{Call wikitext|sourceCode=<nowiki>{{{foo}}}</nowiki>|foo=Hello!}}
→ Lua error at line 10: attempt to index local 'code' (a nil value).
{{#invoke:Call wikitext|main|sourceCode=<nowiki>{{{foo}}}</nowiki>|foo=Hello!}}
→ Lua error at line 18: attempt to perform arithmetic on a nil value.
p = {}
function p.main(frame)
-- undo nowiki sanitization
local code = frame.args['sourceCode'] or frame.args[1]
local i = 0
if not frame.args['sourceCode'] then
i = 1
end
if code:match'nowiki' then
code = mw.text.unstripNoWiki(code)
end
local wikitext = code:gsub("<", "<"):gsub(">", ">") -- angle brackets are still sanitized
local function replaceTripleBraces(parameter, _, default) -- extract corresponding arguments from the parent function. the _ is necessary because the pipe still gets caught in the second capture group
if default=="" then default = nil end
return frame.args[parameter:gsub("%d+", tostring(tonumber("%1")+i))] or default
end
local processed = wikitext:gsub("{{{([^{}<>|]+)(|?([^{}<>|]*))}}}", replaceTripleBraces) -- Someday, I'm going to go to programmer prison for doing... this, and I'm okay with that.
return frame:preprocess(processed)
end
return p