Jump to content

Module:Call wikitext: Difference between revisions

From Humanipedia
hp>BrandonXLF
Spelling
 
hp>BrandonXLF
Also support other unprocessed wikitext
Line 1: Line 1:
<includeonly>{{#invoke:Call wikitext|main}}</includeonly><noinclude>
require('strict')
{{Documentation}}</noinclude>
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
end
 
local code = frame.args['sourceCode'] or error("sourceCode arg not provided")
code = mw.text.unstripNoWiki(code) -- Undo nowiki sanitization
code = code:gsub("&lt;", "<"):gsub("&gt;", ">") -- Unsanitize < and >
-- 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 = frame:newChild{
title = "Called wikitext",
args = newArgs
}
 
    return newFrame:preprocess(code)
end
 
return p

Revision as of 13:32, 5 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: Template:Nowiki template demo Template:Nowiki template demo


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
	end

	local code = frame.args['sourceCode'] or error("sourceCode arg not provided")
	
	code = mw.text.unstripNoWiki(code) -- Undo nowiki sanitization
	code = code:gsub("&lt;", "<"):gsub("&gt;", ">") -- Unsanitize < and >
	
	-- 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 = frame:newChild{
		title = "Called wikitext",
		args = newArgs
	}

    return newFrame:preprocess(code)
end

return p