Module:Call wikitext: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
function p.main(frame) |
function p.main(frame) |
||
⚫ | |||
-- undo nowiki sanitization |
if code:match'nowiki' then -- undo nowiki sanitization |
||
⚫ | |||
if not frame.args['sourceCode'] then |
|||
⚫ | |||
end |
|||
if code:match'nowiki' then |
|||
code = mw.text.unstripNoWiki(code) |
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] 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. |
|||
⚫ | |||
end |
end |
||
Revision as of 22:24, 30 March 2023
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 4: sourceCode arg not provided.
{{#invoke:Call wikitext|main|sourceCode=<nowiki>{{{foo}}}</nowiki>|foo=Hello!}}
→ Hello!
p = {}
function p.main(frame)
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 was missing <nowiki>") end
return frame:preprocess(code:gsub("<", "<"):gsub(">", ">"))
end
return p