Module:Template input utils: Difference between revisions
Jump to navigation
Jump to search
Created page with "-- This module follows User:Ganaram inukshuk/Provisional style guide for Lua local p = {} -- TODO: move tip functions here? See Module:Template input parse -- Library module for common operations with handling template input. -- 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 ori..." |
mNo edit summary |
||
| (9 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. | 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[header_key] = nil | |||
args[data_key] = nil | |||
end | |||
end | end | ||
return entries | 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 | end | ||
| Line 76: | 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. | 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 | ||
Latest revision as of 10:22, 4 February 2026
- This module primarily serves as a library for other modules and has no corresponding template.
Module:Template input utils is a library module meant to help with pre-processing template input.
| Introspection summary for Module:Template input utils | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
-- 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.
-- 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.
-- 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