Module:Call wikitext: Difference between revisions
Appearance
Content deleted Content added
BrandonXLF (talk | contribs) m BrandonXLF moved page Module:Call code to Module:Call wikitext: More specific |
BrandonXLF (talk | contribs) Prepare for Template:Call wikitext wrapper |
||
Line 3: | Line 3: | ||
function p.main(frame) |
function p.main(frame) |
||
local parent = frame:getParent() |
|||
if parent and parent:getTitle():gsub('/sandbox$', '') == mw.title.new('Template:Call wikitext') then |
|||
-- Treat the "Template:Call wikitext" frame as the current frame |
|||
frame = parent |
|||
parent = frame:getParent() |
|||
end |
|||
local code = frame.args['sourceCode'] or error("sourceCode arg not provided") |
local code = frame.args['sourceCode'] or error("sourceCode arg not provided") |
||
Line 8: | Line 16: | ||
code = mw.text.unstripNoWiki(code) |
code = mw.text.unstripNoWiki(code) |
||
else |
else |
||
error("sourceCode arg |
error("sourceCode arg is missing <nowiki>") |
||
end |
end |
||
Line 23: | Line 31: | ||
-- Create a new frame without "sourceCode" |
-- Create a new frame without "sourceCode" |
||
local newFrame = |
local newFrame = parent:newChild{ |
||
title = "Module:Example", |
title = "Module:Example", |
||
args = newArgs |
args = newArgs |
Revision as of 20:37, 1 September 2024
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 13: sourceCode arg not provided.
{{#invoke:Call wikitext|main|sourceCode=<nowiki>{{{foo}}}</nowiki>|foo=Hello!}}
→ Hello!
require('strict')
local p = {}
function p.main(frame)
local parent = frame:getParent()
if parent and parent:getTitle():gsub('/sandbox$', '') == mw.title.new('Template:Call wikitext') then
-- Treat the "Template:Call wikitext" frame as the current frame
frame = parent
parent = frame:getParent()
end
local code = frame.args['sourceCode'] or error("sourceCode arg not provided")
if code:match'nowiki' then -- undo nowiki sanitization
code = mw.text.unstripNoWiki(code)
else
error("sourceCode arg is missing <nowiki>")
end
-- Unsanitize < and >
code = code:gsub("<", "<"):gsub(">", ">")
-- Remove sourceCode from the arguments
local newArgs = {}
for k, v in pairs(frame.args) do
if k ~= 'sourceCode' then
newArgs[k] = v
end
end
-- Create a new frame without "sourceCode"
local newFrame = parent:newChild{
title = "Module:Example",
args = newArgs
}
return newFrame:preprocess(code)
end
return p