Module:IPA/templates

From Wiktionary
Jump to navigation Jump to search

Documentation for this module may be created at Module:IPA/templates/doc

local m_IPA = require("Module:IPA")

local export = {}

-- Used for [[Template:IPA]].
function export.IPA(frame)
	local params = {
		[1] = {list = true, allow_holes = true},
		["n"] = {list = true, allow_holes = true},
		["lang"] = {required = false, default = ""},
	}
	
	local err = nil
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local lang = 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
	
	-- Temporary test to see which Finnish entries use {{IPA}} rather than {{fi-IPA}}
	if lang and (lang:getCode() == "ca" or lang:getCode() == "fi") then
		require("Module:debug").track("IPA/" .. lang:getCode())
	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]
		
		if pron or note then
			table.insert(items, {pron = pron, note = note})
		end
	end
	
	return m_IPA.format_IPA_full(lang, items, err)
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().args, 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

return export