Module:IPA

From Omniversalis

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

local gsub = mw.ustring.gsub
local U = mw.ustring.char

local p = {}

local function IPAspan(text)
	return '<span title="Representation in the International Phonetic Alphabet (IPA)" class="IPA">'..text..'</span>'
end

local function isEmpty(text)
	return text == nil or text == ""
end

function p.IPA(frame)
	local args = frame:getParent().args[1] and frame:getParent().args 
		or frame.args
	local namespace = mw.title.getCurrentTitle().nsText
	
	local text, lang = "", ""
	
	if not isEmpty(args[2]) then
		text = args[2]
		lang = args[1]
	elseif not isEmpty(args[1]) then
		text = args[1]
	end
	
	local IPApage = mw.loadData("Module:IPA/data")[lang]
	
	local err = {}
	
	if not IPApage and not isEmpty(lang) then 
		table.insert(err, "[No IPA key for the language code " .. lang.."]")
	end
	
	--[[ if isEmpty(text) then
		if namespace == "Template" or namespace == "Module" then
			text = "[ɪɡˈzɑːmpəl]"
		else
			error("No text provided to Template:IPA")
		end
	end ]]--
	
	if isEmpty(lang) and args[2] then
		table.insert(err, "[language code?]")
	end
	
	if isEmpty(text) then
		table.insert(err, "[IPA symbols?]")
	end
	
	text = gsub(text, U(0x67), U(0x261)) -- Replace regular g with IPA g.
	
	text = IPAspan(text)
	
	local output =  IPApage and "[[" .. IPApage .. "|" .. text .. "]]"
		or text
		or ""
	
	if not err[1] then
		err = ""
	else
		err = "<sup>" .. table.concat(err) .. "</sup>"
	end
	
	-- Makes error messages show only in preview mode.
	if frame:preprocess("{{REVISIONID}}") == "" then
		return output .. err
	else
		return output
	end
end

return p