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 06:44, 24 September 2014 (split this up into three different functions, to allow other Lua modules to use the invocation-building algorithm and the full unsubst functionality without having to play around with frame objects). 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. The arguments are not altered.
	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. The
	-- invocation substitutes the magic word "__DATE__" for the current month
	-- and year in "Month YYYY" format. If we are not substing, the function
	-- returns the body parameter, without any type checking. This function
	-- is intended to be called from Lua modules.
	--
	-- Params:
	-- @frame - the current frame object. Requires a parent frame to be available.
	-- @body - the template body, returned as-is if not substing.

	if not mw.isSubsting() then
		return body
	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 v == '__DATE__' then
			v = mw.getContentLanguage():formatDate('F Y')
		end
		args[k] = v
	end
	for k, v in pairs(frame:getParent().args) do
		args[k] = v
	end

	return p.invocation(name, args)
end

function p.main(frame)
	-- This function is called from #invoke. It uses the parameter defined in
	-- BODY_PARAM for the template content.
	local body = frame.args[BODY_PARAM]
	if not body then
		error(string.format(
			"no template content provided (use parameter '%s')",
			BODY_PARAM
		), 2)
	end
	frame.args[BODY_PARAM] = nil
	return p._main(frame, body)
end

p[''] = p.main -- Alias for p.main, for backwards compatibility.

return p