Module:ListUtils
Documentation for this module may be created at Module:ListUtils/doc
-- Module:ListUtils
local p = {}
-- Helper: trim whitespace
local function trim(s)
if not s then return '' end
return (mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1'))
end
-- Helper: is already a wikilink like [[Foo]] or [[Foo|Bar]]
local function isLinked(s)
return mw.ustring.match(s, '%[%[') ~= nil
end
-- Helper: strip existing [[...]] to normalize before relinking
local function stripLinks(s)
s = mw.ustring.gsub(s, '%[%[', '')
s = mw.ustring.gsub(s, '%]%]', '')
return s
end
-- Public: turn "A, B , [[C|X]] , D" into "[[A]], [[B]], [[C|X]], [[D]]"
function p.linkifyCommaList(frame)
local raw = frame.args[1] or ''
raw = trim(raw)
if raw == '' then return '' end
-- Split on commas (allowing messy spacing)
local parts = mw.text.split(raw, ',')
local out = {}
local seen = {}
for _, part in ipairs(parts) do
local s = trim(part)
if s ~= '' then
-- Keep already-linked entries as-is
if isLinked(s) then
if not seen[s] then
table.insert(out, s)
seen[s] = true
end
else
-- Normalize any accidental brackets then relink
local plain = stripLinks(s)
plain = trim(plain)
-- Deduplicate by plain text
if not seen[plain] then
table.insert(out, '[[' .. plain .. ']]')
seen[plain] = true
end
end
end
end
return table.concat(out, ', ')
end
return p