Difference between revisions of "Module:Template wrapper/sandbox"

From blackwiki
Jump to navigation Jump to search
test>Trappist the monk
(use template wrapper sandbox;)
test>Trappist the monk
m (cleanup;)
Line 3: Line 3:
 
local p={};
 
local p={};
 
local error_msg = '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">&#124;_template=</code> missing or empty</span>';
 
local error_msg = '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">&#124;_template=</code> missing or empty</span>';
 +
  
 
--[[--------------------------< I S _ I N _ T A B L E >--------------------------------------------------------
 
--[[--------------------------< I S _ I N _ T A B L E >--------------------------------------------------------
Line 36: Line 37:
 
--[[--------------------------< F R A M E _ A R G S _ G E T >--------------------------------------------------
 
--[[--------------------------< F R A M E _ A R G S _ G E T >--------------------------------------------------
  
Fetch the template's 'default' and control parameters
+
Fetch the template's 'default' and control parameters; adds default parameters to args
 
 
Inputs pointer to frame.args, args, and boolean list; adds default parameters to args
 
  
returns content of |_template= parameter or nil
+
returns content of |_template= parameter; nil else
  
 
]]
 
]]
Line 55: Line 54:
 
end
 
end
  
return template; -- return contenta of |_template= and |_exclude= parameters
+
return template; -- return contents of |_template= parameter
 
end
 
end
  
Line 61: Line 60:
 
--[[--------------------------< P F R A M E _ A R G S _ G E T >------------------------------------------------
 
--[[--------------------------< P F R A M E _ A R G S _ G E T >------------------------------------------------
  
Fetch the template's 'live' parameters
+
Fetch the template's 'live' parameters; adds live parameters that aren't members of the exclude table to args table
 
 
Inputs: pointer to pframe.args, args, and exclude tables; adds live parameters that aren't members of the exclude
 
table to args table
 
  
 
no return value
 
no return value
Line 75: Line 71:
 
if v and ('' ~= v) then -- pass along only those parameters that have assigned values
 
if v and ('' ~= v) then -- pass along only those parameters that have assigned values
 
if 'unset' == v:lower() then -- special keyword to unset 'default' parameters set in the wrapper template
 
if 'unset' == v:lower() then -- special keyword to unset 'default' parameters set in the wrapper template
v = ''; -- unset the value
+
v = ''; -- unset the value in the args table
 
end
 
end
 
add_parameter (k, v, args, list) -- add all other parameters to args in the style dictated by list
 
add_parameter (k, v, args, list) -- add all other parameters to args in the style dictated by list
Line 86: Line 82:
 
--[[--------------------------< _ M A I N >--------------------------------------------------------------------
 
--[[--------------------------< _ M A I N >--------------------------------------------------------------------
  
Collect the various default and live parameters into args and in a style according to boolean list.
+
Collect the various default and live parameters into args styled according to boolean list.
  
 
returns name of the wrapped or listed template or nil for an error message
 
returns name of the wrapped or listed template or nil for an error message
Line 96: Line 92:
 
local exclude = {}; -- table of parameter names for parameters that are not passed to the wrapped template
 
local exclude = {}; -- table of parameter names for parameters that are not passed to the wrapped template
 
 
if frame.args._exclude and ('' ~= frame.args._exclude) then -- if there is|_exclude= and it's not empty
+
if frame.args._exclude and ('' ~= frame.args._exclude) then -- if there is |_exclude= and it's not empty
 
exclude = mw.text.split (frame.args._exclude, "%s*,%s*"); -- make a table from its contents
 
exclude = mw.text.split (frame.args._exclude, "%s*,%s*"); -- make a table from its contents
 
end
 
end
  
 
template = frame_args_get (frame.args, args, list); -- get parameters provided in the {{#invoke:template wrapper|...|...}}
 
template = frame_args_get (frame.args, args, list); -- get parameters provided in the {{#invoke:template wrapper|...|...}}
if nil == template or '' == template then -- this is the one parameter required by this module; not present, return an error message
+
if nil == template or '' == template then -- this is the one parameter required by this module
return nil; -- tell calling funtion to emit an error message
+
return nil; -- not present, tell calling funtion to emit an error message
 
end
 
end
 
 
local pframe = frame:getParent(); -- here we get the wrapped template's 'live' parameters
+
local pframe = frame:getParent(); -- here we get the wrapped template's 'live' parameters from pframe.args
pframe_args_get (pframe.args, args, exclude, list); -- from pframe.args add parameters and values to args that are not listed in the exclude table
+
pframe_args_get (pframe.args, args, exclude, list); -- add parameters and values to args that are not listed in the exclude table
  
return template;
+
return template; -- args now has all default and live parameters, return wrapped template name
 
end
 
end
  
Line 119: Line 115:
  
 
function p.wrap (frame)
 
function p.wrap (frame)
local args = {}; -- table of parameters and their values passed to the wrapped template
+
local args = {}; -- table of default and live parameters and their values to be passed to the wrapped template
 
local template; -- the name of the wrapped template
 
local template; -- the name of the wrapped template
  
template = _main (frame, args, false);
+
template = _main (frame, args, false); -- get default and live parameters and the name of the wrapped template
if not template then
+
if not template then -- template name is required
return error_msg;
+
return error_msg; -- emit error message and abandon if template name not present
 
end
 
end
 
 
Line 144: Line 140:
  
 
function p.list (frame)
 
function p.list (frame)
local args = {}; -- table of parameters and their values passed to the wrapped template
+
local args = {}; -- table of default and live parameters and their values to be passed to the listed template
local template; -- the name of the wrapped template
+
local template; -- the name of the listed template
  
template = _main (frame, args, true);
+
template = _main (frame, args, true); -- get default and live parameters and the name of the listed template
if not template then
+
if not template then -- template name is required
return error_msg;
+
return error_msg; -- emit error message and abandon if template name not present
 
end
 
end
  

Revision as of 15:27, 20 May 2018

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

require('Module:No globals');

local p={};
local error_msg = '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">&#124;_template=</code> missing or empty</span>';


--[[--------------------------< I S _ I N _ T A B L E >--------------------------------------------------------

scan through tbl looking for value; return true if found, false else

]]

local function is_in_table (tbl, value)
    for k, v in pairs (tbl) do
        if v == value then return true end
    end
    return false;
end


--[[--------------------------< A D D _ P A R A M E T E R >----------------------------------------------------

adds parameter name and its value to args table according to the state of boolean list argument; kv pair for
template execution; k=v string for template listing.

]]

local function add_parameter (k, v, args, list)
	if list then
		table.insert( args, table.concat ({k, '=', v}));						-- write parent frame parameter names and values to args table as string
	else
		args[k] = v;															-- copy parent frame parameters to args table
	end
end


--[[--------------------------< F R A M E _ A R G S _ G E T >--------------------------------------------------

Fetch the template's 'default' and control parameters; adds default parameters to args

returns content of |_template= parameter; nil else

]]

local function frame_args_get (frame_args, args, list)
	local template;

	for k, v in pairs (frame_args) do											-- here we get the wrapper template's 'default' parameters
		if '_template' == k then
			template = v;														-- save the name of template that we are wrapping
		elseif '_exclude' ~= k then												-- _exclude already handled so ignore it here
			add_parameter (k, v, args, list);									-- add all other parameters to args in the style dictated by list
		end
	end

	return template;															-- return contents of |_template= parameter
end


--[[--------------------------< P F R A M E _ A R G S _ G E T >------------------------------------------------

Fetch the template's 'live' parameters; adds live parameters that aren't members of the exclude table to args table

no return value

]]

local function pframe_args_get (pframe_args, args, exclude, list)
	for k, v in pairs (pframe_args) do
		if 'string' == type (k) and not is_in_table (exclude, k) then			-- do not pass along positional or excluded parameters
			if v and ('' ~= v) then												-- pass along only those parameters that have assigned values
				if 'unset' == v:lower() then									-- special keyword to unset 'default' parameters set in the wrapper template
					v = '';														-- unset the value in the args table
				end
				add_parameter (k, v, args, list)								-- add all other parameters to args in the style dictated by list
			end
		end
	end
end


--[[--------------------------< _ M A I N >--------------------------------------------------------------------

Collect the various default and live parameters into args styled according to boolean list.

returns name of the wrapped or listed template or nil for an error message

]]

local function _main (frame, args, list)
	local template;
	local exclude = {};															-- table of parameter names for parameters that are not passed to the wrapped template
	
	if frame.args._exclude and ('' ~= frame.args._exclude) then					-- if there is |_exclude= and it's not empty
		exclude = mw.text.split (frame.args._exclude, "%s*,%s*");				-- make a table from its contents
	end

	template = frame_args_get (frame.args, args, list);							-- get parameters provided in the {{#invoke:template wrapper|...|...}}
	if nil == template or '' == template then									-- this is the one parameter required by this module
		return nil;																-- not present, tell calling funtion to emit an error message
	end
	
	local pframe = frame:getParent();											-- here we get the wrapped template's 'live' parameters from pframe.args
	pframe_args_get (pframe.args, args, exclude, list);							-- add parameters and values to args that are not listed in the exclude table

	return template;															-- args now has all default and live parameters, return wrapped template name
end


--[[--------------------------< W R A P >----------------------------------------------------------------------

Template entry point.  Call this function to 'execute' the wrapped template

]]

function p.wrap (frame)
	local args = {};															-- table of default and live parameters and their values to be passed to the wrapped template
	local template;																-- the name of the wrapped template

	template = _main (frame, args, false);										-- get default and live parameters and the name of the wrapped template
	if not template then														-- template name is required
		return error_msg;														-- emit error message and abandon if template name not present
	end
	
	return frame:expandTemplate {title=template, args=args};					-- render the template
end


--[[--------------------------< L I S T >----------------------------------------------------------------------

Template entry point.  Call this function to 'display' the source for the wrapped template.  This function added
as a result of a TfD here: Wikipedia:Templates_for_discussion/Log/2018_April_28#Module:PassArguments

This function replaces a similarly named function which was used in {{cite compare}}

Values in the args table are numerically indexed strings in the form 'name=value'
Template:Cite newspaper The Times/testcases
]]


function p.list (frame)
	local args = {};															-- table of default and live parameters and their values to be passed to the listed template
	local template;																-- the name of the listed template

	template = _main (frame, args, true);										-- get default and live parameters and the name of the listed template
	if not template then														-- template name is required
		return error_msg;														-- emit error message and abandon if template name not present
	end

	return frame:preprocess (table.concat ({'<code style="color:inherit; background:inherit; border:none;"><nowiki>{{', template, ' |', table.concat( args, ' |' ), '}}</nowiki></code>'}));	-- render the template
end

return p;