Modul:Categories
Utseende
Dokumentasjon for denne modulen kan opprettes på Modul:Categories/dok
--[=[
Simplify the addition of maintenance and tracking categories in modules.
]=]
require("strict") -- strict mode
local p = {} -- export table
local cats = {} -- to hold the categories
--[=[
Function docs
]=]
-- Add a category name to the accumulator table ("cats").
--
-- Takes a plain string argument containing the category name to be added.
--
-- We count the number of duplicates as a by-product of deduplicating them, but
-- so far as I know there's no real use for the count. We could get called
-- multiple times on the same page so there can be duplicate categories that
-- aren't even visible to us.
--
-- TODO: Supporting sortkeys would be good. I can't think of any actual need for
-- that ottomh, but it seems a fairly obvious extension at some point.
function p:addCat(cat)
if cat ~= nil and cat ~= "" then
cats[cat] = (cats.cat or 0) + 1
end
end
-- Remove a category from the accumulator table ("cats").
--
-- Takes a plain string argument containing the category name to be removed.
--
-- We unconditionally set the value to nil to remove it from the table. We could
-- conceivably use increment/decrement semantics but the use for that is not
-- obvious and would best be implemented by dedicated functions.
function p:removeCat(cat)
if cat ~= nil and cat ~= "" then
cats[cat] = nil
end
end
-- Check whether a cat has been added.
--
-- Takes a plain string argument and returns a boolean.
function p:hasCat(cat)
if cats[cat] ~= nil then
return true
else
return false
end
end
-- Output all added categories as wikitext.
--
-- Takes no arguments and returns wikitext.
function p:getCats()
local s = ""
for cat, _ in pairs(cats) do
s = s .. "[[Category:" .. cat .. "]]"
end
return s
end
return p