Jump to content

Module:IPA

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 03:14, 27 January 2017 (neaten and fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 text = args[2] or args[1] or ""
	local lang
	local IPApage
	local err = ""
	
	local namespace = mw.title.getCurrentTitle().nsText
	
	if args[2] then
		lang = args[1]
	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) then
		err = err .. "[language code?]"
	end
	
	if isEmpty(text) then
		err = err .. "[IPA symbols?]"
	end
	
	if not isEmpty(err) then
		err = "<sup>"..err.."</sup>"
	end
	
	text = gsub(text, U(0x67), U(0x261)) -- Replace regular g with IPA g.
	if not isEmpty(lang) then
		IPApage = mw.loadData("Module:IPA/data")[lang] or error("No IPA key for the language code " .. lang)
	end
	
	text = IPAspan(text)
	
	local output =  IPApage and "[[" .. IPApage .. "|" .. text .. "]]"
		or text
		or ""
	return output .. err
end

return p