Difference between revisions of "Module:ProcessArgs"

From Feed The Beast Wiki
Jump to: navigation, search
(updates to make compatible with upcoming changes to Module:Language, hopefully this won't break)
(Add keepEmptyArgs parameter to normDefault)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
   
local function trim( origArgs )
+
local function trim( origArgs, keepEmptyArgs )
  +
local keepEmptyArgs = keepEmptyArgs or false
 
local args = {}
 
local args = {}
 
 
 
for k, v in pairs( origArgs ) do
 
for k, v in pairs( origArgs ) do
 
v = mw.text.trim( tostring( v ) )
 
v = mw.text.trim( tostring( v ) )
if v ~= '' then
+
if v ~= '' or keepEmptyArgs then
 
args[k] = v
 
args[k] = v
 
end
 
end
Line 14: Line 15:
 
end
 
end
   
  +
-- If default is set to true (p.normDefault), the args will default to the current
-- A frame's arg table can never be nil since it is a metatable
 
  +
-- frame's args if the parent frame is nil or its args are empty.
function p.norm( origArgs )
 
  +
-- If "fromThis" is set, will use this frame's args regardless.
if type( origArgs ) ~= 'table' then
 
  +
--
frame = mw.getCurrentFrame()
 
  +
-- If default is set to false (p.norm), the parent frame args will always be used
  +
-- or return an empty table if they do not exist.
  +
local function norm( origArgs, default, keepEmptyArgs )
 
if type( origArgs ) ~= 'table' or origArgs == mw.getCurrentFrame() then
 
local frame = mw.getCurrentFrame()
 
local parent = frame:getParent()
 
local parent = frame:getParent()
 
local normedParent = {}
 
local normedParent = {}
 
if parent ~= nil then
 
if parent ~= nil then
 
-- A frame's arg table can never be nil since it is a metatable!
normedParent = trim( parent.args )
 
  +
-- trim() also always at least returns an empty table
 
normedParent = trim( parent.args, keepEmptyArgs )
  +
end
  +
if not default then -- p.norm functionality
  +
return normedParent
 
end
 
end
local normedCurrent = trim( frame.args )
 
 
-- Table is empty if this is nil
 
-- Table is empty if this is nil
if next( normedParent ) == nil then
+
if next( normedParent ) == nil or frame.args.fromThis then
return normedCurrent
+
return trim( frame.args, keepEmptyArgs ) -- normedCurrent
 
else
 
else
 
return normedParent
 
return normedParent
Line 32: Line 42:
 
end
 
end
 
 
return trim( origArgs )
+
return trim( origArgs, keepEmptyArgs )
  +
end
  +
 
function p.norm( origArgs )
  +
return norm( origArgs, false, false )
  +
end
  +
  +
function p.normDefault( origArgs, keepEmptyArgs )
  +
return norm( origArgs, true, keepEmptyArgs or false )
 
end
 
end
   
Line 43: Line 61:
 
if type( origArgs ) ~= 'table' then
 
if type( origArgs ) ~= 'table' then
 
-- Normalize to make sure arg fromParent is non-empty if it exists
 
-- Normalize to make sure arg fromParent is non-empty if it exists
args = trim( mw.getCurrentFrame().args )
+
args = trim( mw.getCurrentFrame().args, false )
 
if args.fromParent then
 
if args.fromParent then
args = trim( mw.getCurrentFrame():getParent().args )
+
args = trim( mw.getCurrentFrame():getParent().args, false )
 
end
 
end
 
else
 
else
args = trim( origArgs )
+
args = trim( origArgs, false )
 
end
 
end
 
 

Latest revision as of 00:21, 28 December 2020

Template-info.png Documentation

This module allows arguments to be merged and normalized. This also has the side-effect of making the arguments a real table instead of an empty table with a metatable to access the arguments. This allows the # operator to work, as well as allowing new values to be added to the table without being ignored when iterating. The downsize of using a real table is that some performance is lost if the values are not actually used.

norm function

The norm function will normalize the arguments passed to it, trimming whitespace and removing empty arguments. If a table isn't passed to the function or the current frame is passed instead, it will automatically use the parent frame's arguments table if it exists or an empty table if it doesn't.

normDefault function

The normDefault function will normalize the arguments passed to it, trimming whitespace and removing empty arguments. If a table isn't passed to the function or the current frame is passed instead, it will automatically use the parent frame's arguments table if it has any entries, otherwise it will default to the current frame's argument table.

fromParent function

The fromParent function will normalize the arguments passed to it, using the norm function. If a table isn't passed to the function, it will automatically use the current frame's arguments table. If this table has the fromParent parameter set and non-empty after trimming, it will automatically use the parent frame's arguments table. This function always returns a table passed through the norm function.

merge function

The merge function will merge two tables together, overwriting duplicate values from the second table with the first table's value, as well as doing the same as the norm function if the norm parameter is true. If the first parameter isn't a table, it is used as the value for the norm parameter, and it will automatically get the current frame's directly passed arguments table and merge it with the current frame's parent arguments table.

See also


local p = {}

local function trim( origArgs, keepEmptyArgs )
	local keepEmptyArgs = keepEmptyArgs or false
	local args = {}
	
	for k, v in pairs( origArgs ) do
		v = mw.text.trim( tostring( v ) )
		if v ~= '' or keepEmptyArgs then
			args[k] = v
		end
	end
	
	return args
end

-- If default is set to true (p.normDefault), the args will default to the current
-- frame's args if the parent frame is nil or its args are empty.
-- If "fromThis" is set, will use this frame's args regardless. 
-- 
-- If default is set to false (p.norm), the parent frame args will always be used
-- or return an empty table if they do not exist.
local function norm( origArgs, default, keepEmptyArgs )
	if type( origArgs ) ~= 'table' or origArgs == mw.getCurrentFrame() then
		local frame = mw.getCurrentFrame()
		local parent = frame:getParent()
		local normedParent = {}
		if parent ~= nil then
			-- A frame's arg table can never be nil since it is a metatable!
			-- trim() also always at least returns an empty table
			normedParent = trim( parent.args, keepEmptyArgs )
		end
		if not default then -- p.norm functionality
			return normedParent
		end
		-- Table is empty if this is nil
		if next( normedParent ) == nil or frame.args.fromThis then
			return trim( frame.args, keepEmptyArgs ) -- normedCurrent
		else
			return normedParent
		end
	end
	
	return trim( origArgs, keepEmptyArgs )
end

function p.norm( origArgs )
	return norm( origArgs, false, false )
end

function p.normDefault( origArgs, keepEmptyArgs )
	return norm( origArgs, true, keepEmptyArgs or false )
end

-- This method uses the current frame's args (from #invoke),
-- unless fromParent is set, then uses the parent frame's args.
-- fromParent should only be set in a template wrapper
function p.fromParent( origArgs )
	local args
	
	if type( origArgs ) ~= 'table' then
		-- Normalize to make sure arg fromParent is non-empty if it exists
		args = trim( mw.getCurrentFrame().args, false )
		if args.fromParent then
			args = trim( mw.getCurrentFrame():getParent().args, false )
		end
	else
		args = trim( origArgs, false )
	end
	
	-- This will always return a table normalized from norm()
	return args
end

function p.merge( origArgs, parentArgs, norm )
	if type( origArgs ) ~= 'table' then
		norm = origArgs
		local f = mw.getCurrentFrame()
		origArgs = f.args
		parentArgs = f:getParent().args
	end
	local args = {}
	
	for k, v in pairs( origArgs ) do
		v = mw.text.trim( tostring( v ) )
		if not norm or norm and v ~= '' then
			args[k] = v
		end
	end
	
	for k, v in pairs( parentArgs ) do
		v = mw.text.trim( v )
		if ( not norm or norm and v ~= '' ) and not args[k] then
			args[k] = v
		end
	end
	
	return args
end

return p