Module:labels

Revision as of 05:16, 10 December 2023 by TheNightAvl (talk | contribs)
local export = {}

function export.make_labels(language, labels, nocat)
	local function make_label(label_id, label, language_name)
		local categories = label["categories"]
		local cat_out = ""
		if categories ~= nil then
			for _, cat in ipairs(categories) do
				cat_out = cat_out .. "[[Category:" .. language_name .. " " .. cat .. "]]"
			end
		end
		local display = label["display"]
		local glossary = label["glossary"]
		local wikipedia = label["Wikipedia"]
		if display ~= nil then return display, cat_out end
		if glossary ~= nil then
			local g_url = "Appendix:Glossary#" .. (type(glossary) == "string" and glossary or label_id)
			return "[[" .. g_url .. "|" .. label_id .. "]]", cat_out
		end
		if wikipedia ~= nil then
			return "[[w:" .. (type(wikipedia) == "string" and wikipedia or label_id) .. "|" .. label_id .. "]]", cat_out
		end
		return label_id, cat_out
	end
	local l_data = mw.loadData("Module:labels/data")
	local out = "(<i>"
	local cats = ""
	local skip_comma = false
	for i, l_id in ipairs(labels) do
		l_id = l_data.aliases[l_id] or l_id
		local label = l_data.labels[l_id]
		if i > 1 then
			local conjunctions = {["with"] = true, ["and"] = true, ["or"] = true, ["/"] = true}
			if conjunctions[l_id] or conjunctions[labels[i-1]] then
				skip_comma = true
			end
			out = out .. (skip_comma and "" or ",")
			if not (l_id == "/" or labels[i-1] == "/") then
				out = out .. " "
			end
		end
		if label == null then
			if l_id == "/" then
				out = out .. "</i>/<i>"
			elseif mw.ustring.find(l_id, "^\"[^%[%]]+\"$") then
				local l_id_link = mw.ustring.match(l_id, "\"([^%[%]]+)\"")
				out = out .. "</i>[[" .. l_id_link .. "#" .. language.name .. "|" .. l_id_link .. "]]<i>"
			else
				out = out .. l_id
			end
			skip_comma = false
		else
			l_out, l_cats = make_label(l_id, label, language.name)
			out = out .. l_out
			cats = cats .. l_cats
			skip_comma = label["omit_comma"] or false
		end
	end
	out = out .. "</i>)"
	return out .. (nocat and "" or cats)
end

function export.show(frame)
	local params = {
		[1] = {required = true},
		[2] = {required = true, list = true},
		["nocat"] = {type = "boolean"}
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local language = require("Module:languages").get_by_code(args[1])
	return export.make_labels(language, args[2], args["nocat"])
end

return export