Difference between revisions of "Module:Unsubst/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Mr. Stradivarius
(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)
blackwiki>Mr. Stradivarius
(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.)
Line 7: Line 7:
 
function p.invocation(name, args)
 
function p.invocation(name, args)
 
-- This function makes a template invocation from the name and the arguments
 
-- This function makes a template invocation from the name and the arguments
-- given. The arguments are not altered.
+
-- 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', 1, name, 'string')
 
checkType('p.invocation', 2, args, 'table')
 
checkType('p.invocation', 2, args, 'table')
Line 43: Line 47:
 
end
 
end
  
function p._main(frame, body)
+
function p.main(frame, body)
-- If we are substing, this function returns a template invocation. The
+
-- If we are substing, this function returns a template invocation, and if
-- invocation substitutes the magic word "__DATE__" for the current month
+
-- not, it returns the template body. The template body can be specified in
-- and year in "Month YYYY" format. If we are not substing, the function
+
-- the body parameter, or in the template parameter defined in the
-- returns the body parameter, without any type checking. This function
+
-- BODY_PARAM variable. This function can be called from Lua or from
-- is intended to be called from Lua modules.
+
-- #invoke.
--
 
-- Params:
 
-- @frame - the current frame object. Requires a parent frame to be available.
 
-- @body - the template body, returned as-is if not substing.
 
  
 +
-- Return the template body if we aren't substing.
 
if not mw.isSubsting() then
 
if not mw.isSubsting() then
return body
+
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
 
end
  
Line 64: Line 74:
 
then
 
then
 
error(
 
error(
"argument #1 to '_main' must be a frame object with a parent " ..
+
"argument #1 to 'main' must be a frame object with a parent " ..
"frame available", 2
+
"frame available",
 +
2
 
)
 
)
 
end
 
end
Line 83: Line 94:
 
local args = {}
 
local args = {}
 
for k, v in pairs(frame.args) do
 
for k, v in pairs(frame.args) do
if v == '__DATE__' then
+
if k ~= BODY_PARAM then
v = mw.getContentLanguage():formatDate('F Y')
+
if v == '__DATE__' then
 +
v = mw.getContentLanguage():formatDate('F Y')
 +
end
 +
args[k] = v
 
end
 
end
args[k] = v
 
 
end
 
end
 
for k, v in pairs(frame:getParent().args) do
 
for k, v in pairs(frame:getParent().args) do
Line 95: Line 108:
 
end
 
end
  
function p.main(frame)
+
p[''] = p.main -- For backwards compatibility
-- 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
 
return p

Revision as of 09:08, 24 September 2014

Documentation for this module may be created at Module:Unsubst/sandbox/doc

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