Difference between revisions of "Module:Unsubst/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Jackmcbarn
(track usage of $N)
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)
Line 1: Line 1:
 +
local checkType = require('libraryUtil').checkType
 +
 
local p = {}
 
local p = {}
  
local specialParams = {
+
local BODY_PARAM = '$B'
['$N'] = 'template name', -- Deprecated, but keeping until it is removed from transcluding templates
+
 
['$B'] = 'template content',
+
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
  
p[''] = function ( frame )
+
-- Sanity check for the frame object.
if not frame:getParent() then
+
if type(frame) ~= 'table'
error( '{{#invoke:Unsubst|}} makes no sense without a parent frame' )
+
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
 
end
if not frame.args['$B'] then
+
 
error( '{{#invoke:Unsubst|}} requires parameter $B (template content)' )
+
-- 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
 
end
+
 
if mw.isSubsting() then
+
-- Combine passed args with passed defaults, and substitute magic words.
---- substing
+
local args = {}
-- Combine passed args with passed defaults
+
for k, v in pairs(frame.args) do
local args = {}
+
if v == '__DATE__' then
for k, v in pairs( frame.args ) do
+
v = mw.getContentLanguage():formatDate('F Y')
if not specialParams[k] 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
 
end
 +
args[k] = v
 +
end
 +
for k, v in pairs(frame:getParent().args) do
 +
args[k] = v
 +
end
  
-- Build an equivalent template invocation
+
return p.invocation(name, args)
-- First, find the title to use
+
end
local titleobj = mw.title.new(frame:getParent():getTitle())
 
local title
 
if titleobj.namespace == 10 then -- NS_TEMPLATE
 
title = titleobj.text
 
elseif titleobj.namespace == 0 then -- NS_MAIN
 
title = ':' .. titleobj.text
 
else
 
title = titleobj.prefixedText
 
end
 
  
-- Build the invocation body with numbered args first, then named
+
function p.main(frame)
local ret = '{{' .. title
+
-- This function is called from #invoke. It uses the parameter defined in
for k, v in ipairs( args ) do
+
-- BODY_PARAM for the template content.
if string.find( v, '=', 1, true ) then
+
local body = frame.args[BODY_PARAM]
-- likely something like 1=foo=bar, we need to do it as a named arg
+
if not body then
break
+
error(string.format(
end
+
"no template content provided (use parameter '%s')",
ret = ret .. '|' .. v
+
BODY_PARAM
args[k] = nil
+
), 2)
end
 
for k, v in pairs( args ) do
 
ret = ret .. '|' .. k .. '=' .. v
 
end
 
 
return ret .. '}}'
 
else
 
---- Not substing
 
-- Just return the "body"
 
return frame.args['$B'] .. (frame.args['$N'] and frame:getParent():getTitle() == mw.title.getCurrentTitle().prefixedText and '[[Category:Calls to Module:Unsubst that use $N]]' or '')
 
 
end
 
end
 +
frame.args[BODY_PARAM] = nil
 +
return p._main(frame, body)
 
end
 
end
 +
 +
p[''] = p.main -- Alias for p.main, for backwards compatibility.
  
 
return p
 
return p

Revision as of 06:44, 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. 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