Jump to content

Module:Call wikitext

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lemondoge (talk | contribs) at 17:36, 30 March 2023 (added error message). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

p = {}
-- need to do {{#Invoke:Sandbox/Lemondoge/SpoofTemplate|main|sourceCode=blah|param1|param2}}
function p.main(frame)
	-- undo nowiki sanitization
	local code = frame.args['sourceCode']
	if not code then error("sourceCode argument not provided") end -- check if sourceCode was given
	if code:match'nowiki' then
		code = mw.text.unstripNoWiki(code)
	end
	
	local wikitext = frame:preprocess(code):gsub("&lt;", "<"):gsub("&gt;", ">") -- 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
		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.
    
    return frame:preprocess(processed)
end

return p