Module:auto cat

From Laenkea
Revision as of 10:20, 7 August 2023 by Maria (talk | contribs)
Jump to navigation Jump to search
local export = {}

local submodules = {"languages", "rhymes", "topics"}
local handlers = {}

local function format_categories(categories)
	local cats = ""
	for _, cat in ipairs(categories) do
		cats = cats .. "[[" .. cat .. "]]"
	end
	return cats
end

function export.show(frame)
	local title_obj = mw.title.getCurrentTitle()
	if title_obj.nsText ~= "Category" then
		error("{{auto cat}} can only be used on pages in the Category: namespace")
	end
	
	for _, m in ipairs(submodules) do
		local handler = require(m).handler
		if handler then table.insert(handlers, handler) end
	end
	for _, handler in ipairs(handlers) do
		r_cats, r_match = handler(title_obj)
		if r_match then
			return format_categories(r_cats)
		end
	end
	
	error("{{auto cat}} could not recognise the format of this category name")
end

return export