Module:User:CodeCat/pronunciation

ពីWiktionary

Documentation for this module may be created at Module:User:CodeCat/pronunciation/doc

local export = {}

local columns = {
	{code = "variety", display = "variety"},
	{code = "IPA", display = "[[Wiktionary:IPA|IPA]]"},
	{code = "audio", display = "[[Wiktionary:Audio|Audio]]"},
	{code = "homophones", display = "[[homophone|Homophones]]"},
	{code = "hyphenation", display = "[[Wiktionary:Hyphenation|Hyphenation]]"},
}

local lang = require("Module:languages").getLanguageByCode("nl")

function export.show(frame)
	local data = {
		{variety = "NL", IPA = "/ˈrɛizə(n)/", audio = "nl-reizen.ogg", hyphenation = "rei-zen", homophones = "rijzen"},
		{variety = "BE", IPA = "/ˈrɛːzə(n)/", hyphenation = "rei-zen", homophones = "rijzen"},
		}
	
	-- Determine which columns need to be displayed
	local found_columns = {}
	
	for _, row in ipairs(data) do
		for code, _ in pairs(row) do
			found_columns[code] = true
		end
	end
	
	-- Format table
	for key, row in ipairs(data) do
		for _, col in ipairs(columns) do
			if found_columns[col.code] then
				table.insert(row, process_value(col.code, row[col.code]))
			end
		end
		
		data[key] = "<tr><td>" .. table.concat(row, "</td><td>") .. "</td></tr>"
	end
	
	local head_row = {}
	
	for _, col in ipairs(columns) do
		if found_columns[col.code] then
			table.insert(head_row, col.display)
		end
	end
	
	head_row = "<tr><th>" .. table.concat(head_row, "</th><th>") .. "</th></tr>"
	
	return "<table class=\"wikitable\">" .. head_row .. table.concat(data, "") .. "</table>"
end

function process_value(code, val)
	if not val then
		return ""
	elseif code == "variety" then
		local template = mw.title.new("Template:accent:" .. val)
		
		if template.exists then
			return mw.getCurrentFrame():expandTemplate{title = "Template:accent:" .. val, args = {}}
		else
			return val
		end
	elseif code == "IPA" then
		return mw.getCurrentFrame():expandTemplate{title = "IPAchar", args = {val}}
	elseif code == "audio" then
		return mw.getCurrentFrame():expandTemplate{title = "audio", args = {val, lang = lang:getCode()}}
	elseif code == "homophones" then
		return require("Module:links").full_link(val, nil, lang, nil, nil, nil, nil, nil)
	elseif code == "hyphenation" then
		return (val:gsub("-", "‧", nil))
	else
		return val
	end
end

return export