Module:ListUtils: Difference between revisions
Created page with "-- 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 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, '%]%]', '') retu..." |
No edit summary |
||
| Line 54: | Line 54: | ||
return table.concat(out, ', ') | return table.concat(out, ', ') | ||
end | |||
-- Public: for "A, B, [[C|X]]" produce: | |||
-- [[Category:A]] [[HasVertical::A]] [[Category:B]] [[HasVertical::B]] [[Category:C]] [[HasVertical::C]] | |||
function p.verticalCatsAndProps(frame) | |||
local raw = frame.args[1] or '' | |||
raw = trim(raw) | |||
if raw == '' then | |||
return '' | |||
end | |||
local parts = mw.text.split(raw, ',') | |||
local out = {} | |||
local seen = {} | |||
for _, part in ipairs(parts) do | |||
local s = trim(part) | |||
if s ~= '' then | |||
-- Normalize to plain page name | |||
local plain = stripLinks(s) | |||
-- Handle links like [[Foo|Label]] | |||
plain = mw.ustring.match(plain, '([^|]+)') or plain | |||
plain = trim(plain) | |||
if plain ~= '' and not seen[plain] then | |||
table.insert(out, '[[Category:' .. plain .. ']]') | |||
table.insert(out, '[[HasVertical::' .. plain .. ']]') | |||
seen[plain] = true | |||
end | |||
end | |||
end | |||
return table.concat(out, ' ') | |||
end | end | ||
return p | return p | ||