Module:labels: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 23: Line 23:
l_id = l_data.aliases[l_id] or l_id
l_id = l_data.aliases[l_id] or l_id
local label = l_data.labels[l_id]
local label = l_data.labels[l_id]
if i > 1 then
ret = ret .. (skip_comma and " " or ", ")
end
if label == null then
if label == null then
ret = ret .. ((skip_comma or i == 1) and " " or ", ") .. l_id
ret = ret .. l_id
skip_comma = false
skip_comma = false
else
else
ret = ret .. ((skip_comma or i == 1) and " " or ", ") .. make_label(l_id, label)
ret = ret .. make_label(l_id, label)
skip_comma = label["omit_comma"] or false
skip_comma = label["omit_comma"] or false
end
end

Revision as of 12:30, 6 August 2023

local export = {}

function export.make_labels(language, labels, nocat)
	local function make_label(label_id, label)
		local display = label["display"]
		local glossary = label["glossary"]
		local wikipedia = label["Wikipedia"]
		if display ~= nil then return display end
		if glossary ~= nil then
			local g_url = "Appendix:Glossary#" .. (type(glossary) == "string" and glossary or label_id)
			return "[[" .. g_url .. "|" .. label_id .. "]]"
		end
		if wikipedia ~= nil then
			return "[[w:" .. (type(wikipedia) == "string" and wikipedia or label_id) .. "]]"
		end
		return label_id
	end
	local l_data = mw.loadData("Module:labels/data")
	local ret = "(''"
	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
			ret = ret .. (skip_comma and " " or ", ")
		end
		if label == null then
			ret = ret .. l_id
			skip_comma = false
		else
			ret = ret .. make_label(l_id, label)
			skip_comma = label["omit_comma"] or false
		end
	end
	ret = ret .. ")"
	return ret .. ((nocat and "") or table.concat(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