Module:Cats
Appearance
Documentation for this module may be created at Module:Cats/doc
local p = {}
-- Utility to add a value to a table if not empty
local function addIfValid(tbl, val)
if val and val ~= "" then
table.insert(tbl, val)
end
end
-- Parse a "*"-style hierarchy string into a flat list of all nodes
local function parseTaxonomyFlat(taxonomy)
local flat = {}
for stars, label in taxonomy:gmatch("(%*+)([^%*]+)") do
label = mw.text.trim(label)
addIfValid(flat, label)
end
return flat
end
-- Main function: generate categories from fields + taxonomy
function p.addCats(frame)
local categories = {}
-- 1. Collect from fields
for k, v in pairs(frame.args) do
if k:match("^has.*Type$") or k:match("^hasCategory$") or k:match("^hasClass$") then
for item in mw.text.gsplit(v, ",", true) do
addIfValid(categories, mw.text.trim(item))
end
end
end
-- 2. Collect from taxonomy (hasTaxonomy)
local taxonomy_str = frame.args.hasTaxonomy or ""
if taxonomy_str ~= "" then
local nodes = parseTaxonomyFlat(taxonomy_str)
for _, node in ipairs(nodes) do
addIfValid(categories, node)
end
end
-- 3. Deduplicate
local seen = {}
local output = {}
for _, cat in ipairs(categories) do
if not seen[cat] then
seen[cat] = true
table.insert(output, "[[Category:" .. cat .. "]]")
end
end
return table.concat(output, "\n")
end
return p