Module:IPA/templates

Definition from Wiktionary, the free dictionary
Jump to: navigation, 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 U = mw.ustring.char
local syllabic = U(0x0329)

-- Used for [[Template:IPA]].
function export.IPA(frame)
	local params = {
		[1]			= {list = true, allow_holes = true},
		["n"]		= {list = true, allow_holes = true},
		["qual"]	= {list = true, allow_holes = true},
		["lang"]	= {required = false, default = ""},
		["nocount"] = {type = "boolean"},
		["sort"]	= {},
	}
	
	local err = nil
	local args = require("Module:parameters").process(frame.getParent and frame:getParent().args or frame, params)
	local lang = mw.title.getCurrentTitle().nsText == "Template" and "en" or args["lang"]
	lang = require("Module:languages").getByCode(lang)
	
	if not lang then
		if args["lang"] == "" then
			err = "No language code specified.[[Category:Language code missing/IPA]]"
		else
			err = "The language code '" .. args["lang"] .. "' is not valid.[[Category:Language code invalid/IPA]]"
		end
	end
	
	-- [[Special:WhatLinksHere/Template:tracking/IPA/grc]]
	if lang and lang:getCode() == "grc" then
		require("Module:debug").track("IPA/grc")
	end
	
	local items = {}
	
	for i = 1, math.max(args[1].maxindex, args["n"].maxindex) do
		local pron = args[1][i]
		local note = args["n"][i]
		local qual = args["qual"][i]
		
		if lang then
			require("Module:IPA/tracking").run_tracking(pron, lang)
		end
		
		if pron or note or qual then
			table.insert(items, {pron = pron, note = note, qualifiers = {qual}})
		end
	end
	
	return m_IPA.format_IPA_full(lang, items, err, nil, args.sort, args.nocount)
end

-- Used for [[Template:IPAchar]].
function export.IPAchar(frame)
	local params = {
		[1] = {list = true, allow_holes = true},
		["n"] = {list = true, allow_holes = true},
		["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)
	
	local items = {}
	
	for i = 1, math.max(args[1].maxindex, args["n"].maxindex) do
		local pron = args[1][i]
		local note = args["n"][i]
		
		if pron or note then
			table.insert(items, {pron = pron, note = note})
		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 params = {
		[1] = { list = true, allow_holes = true },
		["n"] = { list = true, allow_holes = true },
		["qual"] = { list = true, allow_holes = true },
		["lang"] = { required = true },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	pronunciations, notes, qualifiers, lang = args[1], args["n"], args["qual"], args["lang"]
	
	local output = {}
	table.insert(output, "{{IPA")
	
	for i = 1, math.max(pronunciations.maxindex, notes.maxindex, qualifiers.maxindex) do
		if pronunciations[i] then
			table.insert(output, "|"..m_XSAMPA.XSAMPA_to_IPA(pronunciations[i]))
		end
		if notes[i] then
			table.insert(output, "|n"..i.."="..notes[i])
		end
		if qualifiers[i] then
			table.insert(output, "|qual"..i.."="..qualifiers[i])
		end
	end
	
	if lang then
		table.insert(output, "|lang="..lang.."}}")
	end
	
	return table.concat(output)
end

-- Used by [[Template:X2IPAchar]]
function export.X2IPAchar(frame)
	local params = {
		[1] = { list = true, allow_holes = true },
		["n"] = { list = true, allow_holes = true },
		["lang"] = { },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	pronunciations, notes, lang = args[1], args["n"], args["lang"]
	
	local output = {}
	table.insert(output, "{{IPAchar")
	
	for i = 1, math.max(pronunciations.maxindex, notes.maxindex) do
		if pronunciations[i] then
			table.insert(output, "|"..m_XSAMPA.XSAMPA_to_IPA(pronunciations[i]))
		end
		if notes[i] then
			table.insert(output, "|n"..i.."="..notes[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 params = {
		[1] = { required = true, list = true, allow_holes = true },
		["lang"] = { required = true },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	pronunciations, lang = args[1], args["lang"]
	
	local output =  {}
	table.insert(output, "{{rhymes")
	
	for i = 1, pronunciations.maxindex do
		if pronunciations[i] then
			table.insert(output, "|"..m_XSAMPA.XSAMPA_to_IPA(pronunciations[i]))
		end
	end
	
	if lang then
		table.insert(output, "|lang="..lang)
	end
	
	table.insert(output, "}}")
	
	return table.concat(output)
end

return export