Jump to content

Mòideal:labels

O Wiktionary

This module is used to support {{Context}}

and {{Label}}

. See Module:labels/data for a list of defined labels, and for labels that are aliases (or "redirects") for other labels.

Many implementation details of this module are subject to change in the near future, so it should be considered under construction. This only affects the internals of the module and of the labels themselves, not the way it is used from within entries.

The module takes a list of labels and will process them as follows:

  1. If the label is listed in Module:labels/data as an alias of another label, replace its name with the name it redirects to, and continue with the next steps.
  2. If the label is defined in Module:labels/data as a label, use that.
  3. Otherwise, just show the label's name unaltered.

local m_labeldata = mw.loadData("Module:labels/data")
local m_utilities = require("Module:utilities")

local export = {}

local function show_categories(data, lang, script, sort_key, script2, sort_key2, term_mode)
	local categories = {}
	local categories2 = {}
	
	for i, cat in ipairs(data.topical_categories or {}) do
		table.insert(categories, lang:getCode() .. ":" .. cat)
		
		if script then
			table.insert(categories, lang:getCode() .. ":" .. cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, lang:getCode() .. ":" .. cat .. " in " .. script2 .. " script")
		end
	end
	
	for i, cat in ipairs(data.sense_categories or {}) do
		cat = (term_mode and (cat .. " terms") or (" terms with " .. cat .. " senses"))
		table.insert(categories, lang:getCanonicalName() .. " " .. cat)
		
		if script then
			table.insert(categories, lang:getCanonicalName() .. " " .. cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, lang:getCanonicalName() .. " " .. cat .. " in " .. script2 .. " script")
		end
	end

	for i, cat in ipairs(data.pos_categories or {}) do
		table.insert(categories, lang:getCanonicalName() .. " " .. cat)
		
		if script then
			table.insert(categories, lang:getCanonicalName() .. " " .. cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, lang:getCanonicalName() .. " " .. cat .. " in " .. script2 .. " script")
		end
	end
	
	for i, cat in ipairs(data.regional_categories or {}) do
		table.insert(categories, cat .. " " .. lang:getCanonicalName())
		
		if script then
			table.insert(categories, cat .. " " .. lang:getCanonicalName() .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, cat .. " " .. lang:getCanonicalName() .. " in " .. script2 .. " script")
		end
	end
	
	for i, cat in ipairs(data.plain_categories or {}) do
		table.insert(categories, cat)
		
		if script then
			table.insert(categories, cat .. " in " .. script .. " script")
		end
		
		if script2 then
			table.insert(categories2, cat .. " in " .. script2 .. " script")
		end
	end
	
	return m_utilities.format_categories(categories, lang, sort_key) .. m_utilities.format_categories(categories2, lang, sort_key2)
end

function export.show_labels(labels, lang, script, script2, sort_key, sort_key2, nocat, term_mode)
	if #labels < 1 then
		if mw.title.getCurrentTitle().nsText == "Template" then
			labels = {"example"}
		else
			error("You must specify at least one label.")
		end
	end
	
	-- Show the labels
	local omit_preComma = false
	local omit_postComma = false
	
	for i, label in ipairs(labels) do
		if omit_postComma then
			omit_preComma = true
			omit_postComma = false
		end
		
		local deprecated = false
		if m_labeldata.deprecated[label] then
			deprecated = true
		end
		if m_labeldata.aliases[label] then
			label = m_labeldata.aliases[label]
		end
		if m_labeldata.deprecated[label] then
			deprecated = true
		end
			
		local data = m_labeldata.labels[label] or {}
			
		label = data.display or label
		
		local omit_comma = omit_preComma or data.omit_preComma
		omit_preComma = false
		omit_postComma = data.omit_postComma
		
		if deprecated then
			label = '<span class="deprecated-label">' .. label .. '</span>'
			if not nocat then
				label = label .. m_utilities.format_categories({ "Entries with deprecated labels" }, lang)
			end
		end
		
		if i > 1 then
			label = (not omit_comma and "<span class=\"ib-comma\">,</span>" or "") .. "&#32;" .. label
		end
		
		labels[i] = label .. (nocat and "" or show_categories(data, lang, script, sort_key, script2, sort_key2, term_mode))
	end
	
	return
		"<span class=\"ib-brac\">(</span><span class=\"ib-content\">" ..
		table.concat(labels, "") ..
		"</span><span class=\"ib-brac\">)</span>"
end

return export