Module:Template input utils

From XenReference

Documentation for this module may be created at Module:Template input utils/doc

-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local p = {}

-- TODO: move tip functions here? See [[Module:Template input parse]]

-- TODO:
-- Add param name normalization: params close enough to snake_case are normal-
-- ized to snake_case

-- Library module for common operations with handling template input.
-- - Numbered <anything> to table extract args and places them into a table.

-- Extracts numbered args (from frame.args) and stores them into one table.
-- Removes original numbered args from args table. Sequence of numbered args
-- may have gaps.
-- Table of entries is returned without being inserted into the original args
-- table, as it may require further processing (EG, parsing to number, ratio, 
-- kv-pairs, etc).
function p.numbered_args_to_table(args, max_num, key_fmt, keep_originals)
	local max_num = max_num or 100	
	local key_fmt = key_fmt or "Entry %d"
	local keep_originals = keep_originals or false		-- Denotes whether to keep originals in table or remove them; default false
	
	local entries = {}
	for i = 1, max_num do
		local key = string.format(key_fmt, i)		-- Make key
		local entry = args[key]						-- Extract
		table.insert(entries, entry)				-- Insert
		if not keep_originals then
			args[key] = nil								-- Remove original
		end
	end
	
	return entries
end

-- Extracts numbered args that correspond to header-data pairs of a two-col
-- table, commonly seen in navboxes and infoboxes. Removes original numbered
-- args from args table. Sequence of numbered args may have gaps, and a pair may
-- have one element be missing/nil (headerless data and dataless header).
-- Table of entries is returned without being inserted into the original args
-- table, as it may require further processing (EG, parsing to number, ratio, 
-- kv-pairs, etc).
function p.numbered_header_data_args_to_table(args, max_num, keep_originals, is_strict_pair, header_fmt, data_fmt)
	local max_num    = max_num or 100
	local header_fmt = header_fmt or "Header %d"	
	local data_fmt   = data_fmt or "Data %d"
	local is_strict_pair = is_strict_pair or false		-- Denotes whether a pair must have both header and data
	local keep_originals = keep_originals or false		-- Denotes whether to keep originals in table or remove them; default false
	
	local entries = {}
	for i = 1, max_num do
		-- Make keys
		local header_key = string.format(header_fmt, i)
		local data_key   = string.format(data_fmt, i)
		
		-- Extract header and data
		local header = args[header_key]
		local data = args[data_key]
		local entry = { ["Header"] = args[header_key], ["Data"] = args[data_key] }
		
		-- Insert
		if is_strict_pair then
			if (header and data) then table.insert(entries, entry) end
		else
			if (header or  data) then table.insert(entries, entry) end
		end
		
		-- Remove originals
		if not keep_originals then
			args[header_key] = nil
			args[data_key] = nil
		end
	end
	
	return entries
end

-- Some older infoboxes use a jagged array instead of an assoc-array, so convert
-- two-element tables into a header-data pair, and one-element tables into
-- either a headerless data row, or a dataless header row.
-- Default is to interpret size-1 arrays as data rows, which corresponds to how
-- the infobox worked before it became template-ified. This old input method may
-- be easier to work with.
function p.jagged_array_to_header_data_pairs(rows, is_header_row)
	local is_header_row = is_header_row or false
	local new_rows = {}
	for i = 1, #rows do
		local row = rows[i]
		if #row == 2 then
			table.insert(new_rows, { ["Header"] = row[1], ["Data"] = row[2] })
		elseif #row == 1 and not is_header_row then
			table.insert(new_rows, { ["Data"  ] = row[1] })
		elseif #row == 1 and is_header_row then
			table.insert(new_rows, { ["Header"] = row[1] })
		end
	end
	return new_rows
end

function p.tester()
	local test_args = {
		["Entry 1"] = "aaa",
		["Entry 2"] = "bbb",
		["Entry 3"] = "ccc",
		["Header 2"] = "BBB",
		["Entry 5"] = "ddd",
	}
	
	--test_args["Entries"] = p.numbered_args_to_table(test_args, "Entry %d", 10)
	test_args["Entries"] = p.numbered_header_data_args_to_table(test_args, 10, "Header %d", "Entry %d")
	return test_args
end

return p