Jump to content

Module:Unsubst/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 09:08, 24 September 2014 (Avoid expanding the $B parameter if we are being substituted, and as doing this means moving most of the p.main code into p._main, just use one p.main function with an optional body parameter.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local checkType = require('libraryUtil').checkType

local p = {}

local BODY_PARAM = '$B'

function p.invocation(name, args)
	-- This function makes a template invocation from the name and the arguments
	-- given. There is no substitution of magic words, etc.; the arguments are
	-- not purposely altered. Note that this function isn't perfect: we have no
	-- way of knowing what whitespace was in the original invocation, the order
	-- of the parameters may be changed, and any parameters with duplicate keys
	-- will be removed.
	checkType('p.invocation', 1, name, 'string')
	checkType('p.invocation', 2, args, 'table')
	
	-- Copy the invocation args and convert magic words.
	-- We need to make a copy of the table rather than just using the original,
	-- as some of the values may be erased when building the invocation.
	local invArgs = {}
	for k, v in pairs(args) do
		invArgs[k] = v
	end

	-- Build the invocation body with numbered args first, then named.
	local ret = {}
	ret[#ret + 1] = '{{'
	ret[#ret + 1] = name
	for k, v in ipairs(invArgs) do
		if string.find(v, '=', 1, true) then
			-- Likely something like 1=foo=bar, we need to do it as a named arg
			break
		end
		ret[#ret + 1] = '|'
		ret[#ret + 1] = v
		invArgs[k] = nil -- Erase the key so that we don't add the value twice
	end
	for k, v in pairs(invArgs) do
		ret[#ret + 1] = '|'
		ret[#ret + 1] = k
		ret[#ret + 1] = '='
		ret[#ret + 1] = v
	end
	ret[#ret + 1] = '}}'

	return table.concat(ret)
end

function p.main(frame, body)
	-- If we are substing, this function returns a template invocation, and if
	-- not, it returns the template body. The template body can be specified in
	-- the body parameter, or in the template parameter defined in the
	-- BODY_PARAM variable. This function can be called from Lua or from
	-- #invoke.

	-- Return the template body if we aren't substing.
	if not mw.isSubsting() then
		if body ~= nil then
			return body
		elseif frame.args[BODY_PARAM] ~= nil then
			return frame.args[BODY_PARAM]
		else
			error(string.format(
				"no template content specified (use parameter '%s' from #invoke)",
				BODY_PARAM
			), 2)
		end
	end

	-- Sanity check for the frame object.
	if type(frame) ~= 'table'
		or type(frame.getParent) ~= 'function'
		or not frame:getParent()
	then
		error(
			"argument #1 to 'main' must be a frame object with a parent " ..
			"frame available",
			2
		)
	end

	-- Find the invocation name.
	local titleobj = mw.title.new(frame:getParent():getTitle())
	local name
	if titleobj.namespace == 10 then -- NS_TEMPLATE
		name = titleobj.text
	elseif titleobj.namespace == 0 then -- NS_MAIN
		name = ':' .. titleobj.text
	else
		name = titleobj.prefixedText
	end

	-- Combine passed args with passed defaults, and substitute magic words.
	local args = {}
	for k, v in pairs(frame.args) do
		if k ~= BODY_PARAM then
			if v == '__DATE__' then
				v = mw.getContentLanguage():formatDate('F Y')
			end
			args[k] = v
		end
	end
	for k, v in pairs(frame:getParent().args) do
		args[k] = v
	end

	return p.invocation(name, args)
end

p[''] = p.main -- For backwards compatibility

return p