Module:IPA/templates

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module is used by {{IPA}}, {{IPAchar}}, {{x2i}}, {{x2ipa}}, {{x2ipachar}}, {{x2rhymes}}.

Because {{IPA}} always includes a language code, you can track when certain symbols or sequences of symbols are used in a particular language's IPA transcriptions. This allows you to find and correct incorrect transcriptions. The tracking function is found in Module:IPA/tracking.


local export = {}

local m_IPA = require("Module:IPA")
local parameter_utilities_module = "Module:parameter utilities"
local parse_utilities_module = "Module:parse utilities"
local pron_qualifier_module = "Module:pron qualifier"
local references_module = "Module:references"

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

-- Used for [[Template:IPA]].
function export.IPA(frame)
	local parent_args = frame:getParent().args
	-- Track uses of n so they can be converted to ref.
	-- Track uses of qual so they can be converted to q.
	for k, v in pairs(parent_args) do
		if type(k) == "string" and k:find("^n[0-9]*$") then
			track("n")
		end
		if type(k) == "string" and k:find("^qual[0-9]*$") then
			track("q")
		end
	end
	local include_langname = frame.args.include_langname
	local compat = parent_args.lang
	local offset = compat and 0 or 1

	local params = {
		[compat and "lang" or 1] = {required = true, type = "language", etym_lang = true, default = "en"},
		[1 + offset] = {list = true, disallow_holes = true},
		-- Deprecated; don't use in new code. Came before 'ref' but too obscure.
		["n"] = {list = true, allow_holes = true, alias_of = "ref"},
		-- Deprecated; don't use in new code.
		["qual"] = {list = true, allow_holes = true, separate_no_index = true, alias_of = "q"},
		["nocount"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["sort"] = {},
	}

	local param_mods = {}

	local m_param_utils = require(parameter_utilities_module)

	m_param_utils.augment_param_mods_with_pron_qualifiers(param_mods)
	m_param_utils.augment_params_with_modifiers(params, param_mods)

	local args = require("Module:parameters").process(parent_args, params)

	local lang = args[compat and "lang" or 1]

	local items = m_param_utils.process_list_arguments {
		args = args,
		param_mods = param_mods,
		termarg = 1 + offset,
		term_dest = "pron",
		track_module = "IPA",
	}
	for _, item in ipairs(items) do
		require("Module:IPA/tracking").run_tracking(item.pron, lang)
	end

	local data = {
		lang = lang,
		items = items,
		no_count = args.nocount,
		nocat = args.nocat,
		sort_key = args.sort,
		include_langname = include_langname,
		separator = "",
	}
	require(pron_qualifier_module).parse_qualifiers {
		store_obj = data,
		q = args.q.default,
		qq = args.qq.default,
		a = args.a.default,
		aa = args.aa.default,
	}

	return m_IPA.format_IPA_full(data)
end

-- Used for [[Template:IPAchar]].
function export.IPAchar(frame)
	local params = {
		[1] = {list = true, allow_holes = true},
		["ref"] = {list = true, allow_holes = true},
		-- Came before 'ref' but too obscure
		["n"] = {list = true, allow_holes = true, alias_of = "ref"},
		["q"] = {list = true, allow_holes = true, require_index = true},
		["qq"] = {list = true, allow_holes = true, require_index = true},
		["qual"] = {list = true, allow_holes = true},
		-- FIXME, remove this.
		["lang"] = {}, -- This parameter is not used and does nothing, but is allowed for futureproofing.
	}

	local args = require("Module:parameters").process(frame.getParent and frame:getParent().args or frame, params)
	
	-- [[Special:WhatLinksHere/Wiktionary:Tracking/IPAchar/lang]]
	if args.lang then
		require("Module:debug/track")("IPAchar/lang")
	end

	local items = {}
	
	for i = 1, math.max(args[1].maxindex, args.ref.maxindex, args.q.maxindex, args.qq.maxindex, args.qual.maxindex) do
		local pron = args[1][i]
		local refs = args["ref"][i]
		if refs then
			refs = require("Module:references").parse_references(refs)
		end
		local q = args.q[i] or args.qual[i]
		local qq = args.qq[i]

		if pron or refs or qual then
			table.insert(items, {pron = pron, refs = refs, q = {q}, qq = {qq}})
		end
	end

	-- Format
	return m_IPA.format_IPA_multiple(nil, items)
end

function export.XSAMPA(frame)
	local params = {
		[1] = { required = true },
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	return m_IPA.XSAMPA_to_IPA(args[1] or "[Eg'zA:mp5=]")
end

-- Used by [[Template:X2IPA]]
function export.X2IPAtemplate(frame)
	local parent_args = frame.getParent and frame:getParent().args or frame
	local compat = parent_args["lang"]
	local offset = compat and 0 or 1

	local params = {
		[compat and "lang" or 1] = {required = true, default = "und"},
		[1 + offset] = {list = true, allow_holes = true},
		["ref"] = {list = true, allow_holes = true},
		-- Came before 'ref' but too obscure
		["n"] = {list = true, allow_holes = true, alias_of = "ref"},
		["a"] = {list = true, allow_holes = true, separate_no_index = true},
		["aa"] = {list = true, allow_holes = true, separate_no_index = true},
		["q"] = {list = true, allow_holes = true, separate_no_index = true},
		["qq"] = {list = true, allow_holes = true, separate_no_index = true},
		["qual"] = {list = true, allow_holes = true},
		["nocount"] = {type = "boolean"},
		["sort"] = {},
	}
	
	local args = require("Module:parameters").process(parent_args, params)
	
	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	local pronunciations, refs, a, aa, q, qq, qual, lang =
		args[1 + offset], args.ref, args.a, args.aa, args.q, args.qq, args.qual, args[compat and "lang" or 1]
	
	local output = {}
	table.insert(output, "{{IPA")
	
	table.insert(output, "|" .. lang)

	if a.default then
		table.insert(output, "|a=" .. a.default)
	end
	if q.default then
		table.insert(output, "|q=" .. q.default)
	end
	for i = 1, math.max(pronunciations.maxindex, refs.maxindex, a.maxindex, aa.maxindex, q.maxindex, qq.maxindex,
		qual.maxindex) do
		if pronunciations[i] then
			table.insert(output, "|" .. m_XSAMPA.XSAMPA_to_IPA(pronunciations[i]))
		end
		if a[i] then
			table.insert(output, "|a" .. i .. "=" .. a[i])
		end
		if aa[i] then
			table.insert(output, "|aa" .. i .. "=" .. aa[i])
		end
		if q[i] then
			table.insert(output, "|q" .. i .. "=" .. q[i])
		end
		if qq[i] then
			table.insert(output, "|qq" .. i .. "=" .. qq[i])
		end
		if refs[i] then
			table.insert(output, "|ref" .. i .. "=" .. refs[i])
		end
		if qual[i] then
			table.insert(output, "|qual" .. i .. "=" .. qual[i])
		end
	end
	if aa.default then
		table.insert(output, "|aa=" .. aa.default)
	end
	if qq.default then
		table.insert(output, "|qq=" .. qq.default)
	end
	if args.nocount then
		table.insert(output, "|nocount=1")
	end
	if args.sort then
		table.insert(output, "|sort=" .. args.sort)
	end
	
	table.insert(output, "}}")

	return table.concat(output)
end

-- Used by [[Template:X2IPAchar]]
function export.X2IPAchar(frame)
	local params = {
		[1] = { list = true, allow_holes = true },
		["ref"] = {list = true, allow_holes = true},
		-- Came before 'ref' but too obscure
		["n"] = {list = true, allow_holes = true, alias_of = "ref"},
		["q"] = {list = true, allow_holes = true, require_index = true},
		["qq"] = {list = true, allow_holes = true, require_index = true},
		["qual"] = { list = true, allow_holes = true },
		-- FIXME, remove this.
		["lang"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	-- [[Special:WhatLinksHere/Wiktionary:Tracking/X2IPAchar/lang]]
	if args.lang then
		require("Module:debug/track")("X2IPAchar/lang")
	end

	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	local pronunciations, refs, q, qq, qual, lang = args[1], args.ref, args.q, args.qq, args.qual, args.lang
	
	local output = {}
	table.insert(output, "{{IPAchar")
	
	for i = 1, math.max(pronunciations.maxindex, refs.maxindex, q.maxindex, qq.maxindex, qual.maxindex) do
		if pronunciations[i] then
			table.insert(output, "|" .. m_XSAMPA.XSAMPA_to_IPA(pronunciations[i]))
		end
		if q[i] then
			table.insert(output, "|q" .. i .. "=" .. q[i])
		end
		if qq[i] then
			table.insert(output, "|qq" .. i .. "=" .. qq[i])
		end
		if qual[i] then
			table.insert(output, "|qual" .. i .. "=" .. qual[i])
		end
		if refs[i] then
			table.insert(output, "|ref" .. i .. "=" .. refs[i])
		end
	end

	if lang then
		table.insert(output, "|lang=" .. lang)
	end
	
	table.insert(output, "}}")
	
	return table.concat(output)
end

-- Used by [[Template:x2rhymes]]
function export.X2rhymes(frame)
	local parent_args = frame.getParent and frame:getParent().args or frame
	local compat = parent_args["lang"]
	local offset = compat and 0 or 1

	local params = {
		[compat and "lang" or 1] = {required = true, default = "und"},
		[1 + offset] = {required = true, list = true, allow_holes = true},
	}
	
	local args = require("Module:parameters").process(parent_args, params)
	
	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	pronunciations, lang = args[1 + offset], args[compat and "lang" or 1]
	
	local output =  {}
	table.insert(output, "{{rhymes")
	
	table.insert(output, "|" .. lang)

	for i = 1, pronunciations.maxindex do
		if pronunciations[i] then
			table.insert(output, "|" .. m_XSAMPA.XSAMPA_to_IPA(pronunciations[i]))
		end
	end
	
	table.insert(output, "}}")
	
	return table.concat(output)
end

-- Used for [[Template:enPR]].
function export.enPR(frame)
	local parent_args = frame:getParent().args

	local params = {
		[1] = {list = true, disallow_holes = true},
	}

	local param_mods = {}

	local m_param_utils = require(parameter_utilities_module)

	m_param_utils.augment_param_mods_with_pron_qualifiers(param_mods)
	m_param_utils.augment_params_with_modifiers(params, param_mods)

	local args = require("Module:parameters").process(parent_args, params)

	local items = m_param_utils.process_list_arguments {
		args = args,
		param_mods = param_mods,
		termarg = 1,
		term_dest = "pron",
		track_module = "IPA",
	}

	local data = {
		items = items,
	}
	require(pron_qualifier_module).parse_qualifiers {
		store_obj = data,
		q = args.q.default,
		qq = args.qq.default,
		a = args.a.default,
		aa = args.aa.default,
	}

	return m_IPA.format_enPR_full(data)
end

return export