Module:ExplodeAndList
Documentation for this module may be created at Module:ExplodeAndList/doc
-- Module:ExplodeAndList
-- This module takes two parameters, a list of, for example, titles and a list of, for example, names separated by '<br />', '<br/>' or '<br/ >' tags,
-- and formats them into a list with each title and name pair displayed on separate lines.
--
-- Usage:
-- {{#invoke:ExplodeAndList|explodeAndList|<list1>|<list2>}}
--
-- Parameters:
-- 1. <list1>: A string containing, for example, titles separated by '<br />', '<br/>' or '<br/ >' tags.
-- 2. <list2>: A string containing, for example, names separated by '<br />', '<br/>' or '<br/ >' tags.
--
-- Example:
-- {{#invoke:ExplodeAndList|explodeAndList|
-- | King<br />Governor<br />Prime minister
-- | Willem-Alexander<br />Lucille George-Wout<br />Gilmar Pisas
-- }}
--
-- Output:
-- - King Willem-Alexander<br />
-- - Governor Lucille George-Wout<br />
-- - Prime minister Gilmar Pisas
--
-- This module is typically used in templates to format lists of titles and names.
local p = {}
function p.explodeAndList(frame)
local titulonan = frame.args[1] or ""
local nombernan = frame.args[2] or ""
-- Function to trim whitespace from a string
local function trim(s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
-- Remove leading and trailing whitespace from inputs
titulonan = trim(titulonan)
nombernan = trim(nombernan)
-- Remove leading <br /> or <br/> or <br/ > tags
titulonan = titulonan:gsub("^<br ?/?>", "")
nombernan = nombernan:gsub("^<br ?/?>", "")
-- Replace all variations of <br /> with a standard separator
titulonan = titulonan:gsub("<br%s*/?%s*>", "||")
nombernan = nombernan:gsub("<br%s*/?%s*>", "||")
-- Split the inputs by the standard separator
local titulonan_list = mw.text.split(titulonan, "||")
local nombernan_list = mw.text.split(nombernan, "||")
-- Initialize the result table
local result = {}
-- Ensure the lists are of equal length
local max_length = math.max(#titulonan_list, #nombernan_list)
-- Iterate through each pair of titles and names
for i = 1, max_length do
local title = trim(titulonan_list[i] or "") -- Trim title or use empty string if nil
local name = trim(nombernan_list[i] or "") -- Trim name or use empty string if nil
-- Append the combined title and name to the result with between them
table.insert(result, title .. ' ' .. name)
end
-- Concatenate the result into a single string separated by <br /> tags
return table.concat(result, '<br />')
end
return p