Module:Template input utils: Difference between revisions

Ganaram inukshuk (talk | contribs)
m move tester last
Ganaram inukshuk (talk | contribs)
mNo edit summary
 
(7 intermediate revisions by the same user not shown)
Line 3: Line 3:


-- TODO: move tip functions here? See [[Module:Template input parse]]
-- 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.
-- Library module for common operations with handling template input.
-- - Numbered <anything> to table extract args and places them into a table.
-- Normalize params to snake case (may change to a different form later);
-- anything close enough to snake case is converted to snake case:
-- TRAIN_CASE, KEBEB-CASE, no dashes (OK!)
-- PascalCase, camelCase (will not convert properly)
-- Function work-in-progress
function p.normalize_params(args)
-- Local function for normalizing strings
-- Spaces and dashes turn into underscores
-- and all capital letters become lowercase
local function normalize(input_string)
local cleaned_string = string.gsub(string.lower(input_string), "[%-%s]", "_")
end
end


-- Extracts numbered args (from frame.args) and stores them into one table.
-- Extracts numbered args (from frame.args) and stores them into one table.
Line 12: Line 33:
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- kv-pairs, etc).
-- kv-pairs, etc).
function p.numbered_args_to_table(args, max_num, key_fmt)
function p.numbered_args_to_table(args, max_num, key_fmt, keep_originals)
local max_num = max_num or 100
local max_num = max_num or 100
local key_fmt = key_fmt or "Entry %d"
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 = {}
local entries = {}
Line 21: Line 43:
local entry = args[key] -- Extract
local entry = args[key] -- Extract
table.insert(entries, entry) -- Insert
table.insert(entries, entry) -- Insert
args[key] = nil -- Remove original
if not keep_originals then
args[key] = nil -- Remove original
end
end
end
Line 34: Line 58:
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- kv-pairs, etc).
-- kv-pairs, etc).
function p.header_data_pairs_to_table(args, max_num, header_fmt, data_fmt, is_strict_pair)
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 max_num    = max_num or 100
local header_fmt = header_fmt or "Header %d"
local header_fmt = header_fmt or "Header %d"
local data_fmt  = data_fmt or "Data %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 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 = {}
local entries = {}
Line 59: Line 84:
-- Remove originals
-- Remove originals
args[header_key] = nil
if not keep_originals then
args[data_key] = nil
args[header_key] = nil
args[data_key] = nil
end
end
end
Line 68: Line 95:
-- Some older infoboxes use a jagged array instead of an assoc-array, so convert
-- 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
-- two-element tables into a header-data pair, and one-element tables into
-- either a headerless data row, or a dataless header row. This may also be
-- either a headerless data row, or a dataless header row.
-- easier to work with.
-- Default is to interpret size-1 arrays as data rows, which corresponds to how
-- TODO: decide on default value
-- 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)
function p.jagged_array_to_header_data_pairs(rows, is_header_row)
local is_header_row = is_header_row or false
local is_header_row = is_header_row or false
Line 97: Line 125:
--test_args["Entries"] = p.numbered_args_to_table(test_args, "Entry %d", 10)
--test_args["Entries"] = p.numbered_args_to_table(test_args, "Entry %d", 10)
test_args["Entries"] = p.header_data_pairs_to_table(test_args, 10, "Header %d", "Entry %d")
test_args["Entries"] = p.numbered_header_data_args_to_table(test_args, 10, "Header %d", "Entry %d")
return test_args
return test_args
end
end


return p
return p