Feed The Beast Wiki

Follow the Feed The Beast Wiki on Discord or Mastodon!

READ MORE

Feed The Beast Wiki
Advertisement

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

local p = {}
function p.data( f )
	local args = f:getParent().args
	local data = mw.loadData( 'Module:' .. args.data )
	
	local value = data
	local case = args.lcase
	for k, v in ipairs( args ) do
		v = mw.text.trim( v )
		if case then
			v = mw.ustring.lower( v )
		end
		
		value = value[v]
		if not value then
			return args.default or args[1]
		end
	end
	
	if type( value ) == 'table' then
		-- loadData tables don't work with table.concat
		local concatValue = {}
		for _, v in ipairs( value ) do
			table.insert( concatValue, v )
		end
		
		value = table.concat( concatValue, ( args.separator or '' ) .. ' ' )
	end
	
	return value
end

-- Trims all empty arguments, then invokes a module
-- Parameters: 
--   1: the name of the module to invoke
--   2: the function of the module
--   everything else: gets trimmed and passed as arguments for the invoked module
p.trimInvoke = function(frame)
	--local moduleName = frame.args[1];
	--local moduleFunc = frame.args[2];
	--frame.args[1]=nil;
	--frame.args[2]=nil;
	
	local newArgs = {};
	for k, v in pairs( frame.args ) do
		v = mw.text.trim( tostring( v ) );
		if v ~= '' and v ~= nil then
			newArgs[k] = v;
		end
	end
	--frame.args = newArgs;
	--mw.logObject(newArgs);
	
	--return require('Module:'..moduleName)[moduleFunc](frame);
	local val, _ = string.gsub(frame:callParserFunction('#invoke', newArgs ), "%[%[Category:Articles with bad template parameters%]%]", ''); --Hack to remove category
	return val;
end


--[[ 
Example usage:

	In Template:Example ->
	{{#invoke:Data|toList|fmt=Hello $1, $2 of $3}}
	(by default, the separator is a new line, to change, add the 'sep' argument with your separator)
	
	In the page using Template:Example ->
	{{Example|Elizabeth~daughter~William|Thor~son~Odin}}
	
	Returns ->
	Hello Elizabeth, daughter of William
	Hello Thor, son of Odin


]]
p.toList = function(frame)
	local fmt = frame.args.fmt;
	
	local out = {}
	for k, v in pairs( frame:getParent().args ) do
		if (type(k) == "number") then
			local temp = {};
			local i = 1;
			for word in string.gmatch(v, '[^~]+') do
				temp["$"..i] = word;
				i = i+1;
			end
		
			out[#out+1] = string.gsub(fmt, "%$%d+", function(capture)
					return temp[capture];
			end);
		end
		
	end
	if (out == {}) then return fmt end
	return table.concat(out, frame.args.sep or "<br/>");
end

return p
Advertisement