Jump to content

Module:Call wikitext: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Prepare for Template:Call wikitext wrapper
Fix wrapper check
Line 5: Line 5:
local parent = frame:getParent()
local parent = frame:getParent()
if parent and parent:getTitle():gsub('/sandbox$', '') == mw.title.new('Template:Call wikitext') then
if parent and parent:getTitle():gsub('/sandbox$', '') == 'Template:Call wikitext' then
-- Treat the "Template:Call wikitext" frame as the current frame
-- Treat the "Template:Call wikitext" frame as the current frame
frame = parent
frame = parent

Revision as of 20:46, 1 September 2024

require('strict')
local p = {}

function p.main(frame)
	local parent = frame:getParent()
	
	if parent and parent:getTitle():gsub('/sandbox$', '') == '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("&lt;", "<"):gsub("&gt;", ">")
	
	-- 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