Module:accent qualifier: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
all internal callers of show() converted to format_qualifiers(); throw an error on internal uses
track uses of format_qualifiers() with old calling convention
Line 21: Line 21:
function export.format_qualifiers(lang, qualifiers)
function export.format_qualifiers(lang, qualifiers)
if type(lang) == "table" and not lang.getCode then
if type(lang) == "table" and not lang.getCode then
track("format-qualifiers-old")
-- old calling convention: (qualifiers) insteads of (lang, qualifiers). Note that lang=true is a special hack used
-- old calling convention: (qualifiers) insteads of (lang, qualifiers). Note that lang=true is a special hack used
-- by show() below to avoid the tracking.
-- by show() below to avoid the tracking.

Revision as of 02:38, 20 May 2024

This module implements the {{accent}} or {{a}} template. To add more regional accent presets, edit Module:accent qualifier/data.

Detailed documentation

export.format_qualifiers

function export.format_qualifiers(lang, qualifiers)

Format accent qualifiers. Implements the {{a}} (shortcut for {{accent}}) template. A cross between {{q}} (for qualifiers), and {{lb}} (for labels). In time, it will require a language code in |1= and have the recognized accent qualifiers merged with labels; at that point, the main difference between {{a}} and [Term?] will be that the latter categorizes.

Currently, lang is accepted and should be supplied, but is ignored (and callers not supplying lang are tracked). In time, lang will be come mandatory. The old calling convention of format_qualifiers(qualifiers) is stil allowed, but deprecated.

export.show

function export.show(frame)

External entry point that implements {{accent}} and {{a}}.


local export = {}

local accent_qualifier_data_module = "Module:accent qualifier/data"
local labels_qualifiers_module = "Module:labels/data/qualifiers"

local function track(page)
	require("Module:debug/track")("accent qualifier/" .. page)
	return true
end

--[==[
Format accent qualifiers. Implements the {{tl|a}} (shortcut for {{tl|accent}}) template. A cross between {{tl|q}} (for
qualifiers), and {{tl|lb}} (for labels). In time, it will require a language code in {{para|1}} and have the 
recognized accent qualifiers merged with labels; at that point, the main difference between {{tl|a}} and {{t|lb}} will
be that the latter categorizes.

Currently, `lang` is accepted and should be supplied, but is ignored (and callers not supplying `lang` are tracked).
In time, `lang` will be come mandatory. The old calling convention of {format_qualifiers(qualifiers)} is stil allowed,
but deprecated.
]==]
function export.format_qualifiers(lang, qualifiers)
	if type(lang) == "table" and not lang.getCode then
		track("format-qualifiers-old")
		-- old calling convention: (qualifiers) insteads of (lang, qualifiers). Note that lang=true is a special hack used
		-- by show() below to avoid the tracking.
		qualifiers = lang
		lang = nil
	end
	if not lang then
		track("nolang")
	end
	local m_data = mw.loadData(accent_qualifier_data_module)
	local m_labels_qualifiers
	
	if type(qualifiers) ~= "table" then
		qualifiers = { qualifiers }
	end
	
	-- local categories = {}

	local omit_preComma = false
	local omit_postComma = true
	local omit_preSpace = false
	local omit_postSpace = true

	local formatted_qualifiers = {}

	for i, accent in ipairs(qualifiers) do
		omit_preComma = omit_postComma
		omit_postComma = false
		omit_preSpace = omit_postSpace
		omit_postSpace = false

		local data
		local to_insert
		
		-- Replace an alias with the label that has a data table.
		if m_data.aliases[accent] then
			accent = m_data.aliases[accent]
		end
		
		-- Retrieve the label's data table.
		if m_data.labels[accent] then
			data = m_data.labels[accent]
		end
		
		-- Use the link and displayed text in the data table, if they exist.
		if data then
			if data.link then
				to_insert = "[[w:" .. data.link .. "|" ..  (data.display or data.link) .. "]]"
			elseif data.display then
				to_insert = data.display
			end
			
			--[[
			if data[accent] then
				if data[accent].type == "sound change" then
					table.insert(categories, lang:getCanonicalName() .. " terms with pronunciations exhibiting " .. accent)
				end
			end
			]]
		elseif #qualifiers > 1 then
			-- Only check label qualifiers if there's more than one accent given, as an optimization.
			m_labels_qualifiers = m_labels_qualifiers or mw.loadData(labels_qualifiers_module)

			local labdata = m_labels_qualifiers[accent]
			if labdata and (type(labdata) == "string" or labdata.alias_of) then
				accent = labdata.alias_of or labdata
				labdata = m_labels_qualifiers[accent]
			end
			if labdata then
				omit_preComma = omit_preComma or labdata.omit_preComma
				omit_postComma = labdata.omit_postComma
				omit_preSpace = omit_preSpace or labdata.omit_preSpace
				omit_postSpace = labdata.omit_postSpace
				to_insert = labdata.display or accent
			else
				to_insert = accent
			end
		else
			to_insert = accent
		end

		if to_insert then
			if to_insert ~= "" then
				to_insert =
					(omit_preComma and "" or '<span class="ib-comma">,</span>') ..
					(omit_preSpace and "" or "&#32;") ..
					to_insert
			end
			table.insert(formatted_qualifiers, to_insert)
		else
			-- FIXME: Does this happen?
		end
	end
	
	return
		"<span class=\"ib-brac\">(</span><span class=\"ib-content\">" ..
		table.concat(formatted_qualifiers, "") ..
		"</span><span class=\"ib-brac\">)</span>"
end

--[==[
External entry point that implements {{tl|accent}} and {{tl|a}}.
]==]
function export.show(frame)
	if not frame.getParent then
		error("When calling [[Module:accent qualifier]] internally, use format_qualifiers() not show()")
	end
	local parent_args = frame:getParent().args

	if (not parent_args[1] or parent_args[1] == "") and mw.title.getCurrentTitle().nsText == "Template" then
		return export.format_qualifiers("{{{1}}}")
	end
	
	local params = {
		[1] = {required = true, list = true}
	}
	local args = require("Module:parameters").process(parent_args, params)
	
	return export.format_qualifiers(true, args[1])
end

return export