Module:User:ZxxZxxZ/pronunciation

local export = {}

local m_IPA = require("Module:IPA") local lang = require("Module:languages").getByCode("nl")

-- TODO: Lua-ize Template:IPAchar

local columns_collapsed = { {code = "variety", display = "variety"}, {code = "IPA", display = "IPA"}, {code = "audio", display = "Audio" }, }

local columns_uncollapsed = { {code = "variety", display = "variety"}, {code = "IPA", display = "IPA"}, {code = "rhymes", display = "Rhymes"}, {code = "audio", display = "Audio" }, {code = "homophones", display = "Homophones"}, {code = "hyphenation", display = "Hyphenation"}, }

local columns_to_show_collapsed, columns_to_show_uncollapsed = {}, {}

function export.show(frame) local data = { {variety = {"NL"}, IPA = {"/ˈrɛizə(n)/"}, rhymes = {"ə(n)"},   audio = {"nl-reizen.ogg"},     homophones = {"rijzen"},              hyphenation = {"rei-zen"}, optional_column={"VALUE", display="Optional column", order="4"}}, {variety = {"BE"}, IPA = {"/ˈrɛːzn/ [ˈrɛːzn̩]", "/ˈrɛːzən/", "/ˈrɛːzə/"}, audio = {"", "nl-reizen.ogg"}, homophones = {"", "rijzen", "rijzn"}, hyphenation = {"rei-zen"}}, {variety = {"Sandwich Islands"}, IPA = {"/gumbalagumba/"}}, }

-- Determine which columns need to be displayed local columns_collapsed_codes, columns_uncollapsed_codes = {}, {} for _, row in ipairs(columns_collapsed) do columns_collapsed_codes[row.code] = true end for _, row in ipairs(columns_uncollapsed) do columns_uncollapsed_codes[row.code] = true end

for _, row in ipairs(data) do		for code, value in pairs(row) do			if columns_collapsed_codes[code] then columns_to_show_collapsed[code] = true end

columns_to_show_uncollapsed[code] = true

if not columns_uncollapsed_codes[code] then value.code = code table.insert(columns_uncollapsed, tonumber(value.order), value) end end end

local format_table = function(data, collapsed) local data2 = {} local row2 = {} local columns = columns_collapsed local columns_to_show = columns_to_show_collapsed if not collapsed then columns = columns_uncollapsed columns_to_show = columns_to_show_uncollapsed end

for key, row in ipairs(data) do			-- Find number of sub-rows local subrows = 1 for _, col in ipairs(columns) do				if row[col.code] and #row[col.code] > subrows then subrows = #row[col.code] end end

for subrow = 1, subrows do				row2[subrow] = {} for _, col in ipairs(columns) do					if columns_to_show[col.code] then if row[col.code] and row[col.code][subrow] then table.insert(row2[subrow], process_value(col.code, row[col.code][subrow])) elseif col.code ~= "variety" then table.insert(row2[subrow], "") end end end

if not data2[key] then data2[key] = {} end data2[key][subrow] = "  1 then data2[key][subrow] = data2[key][subrow] .. " rowspan=\"" .. subrows .."\"" end data2[key][subrow] = data2[key][subrow] .. ">"				.. table.concat(row2[subrow], " ") .. " "			end end

data = data2 for key, _ in ipairs(data) do			data[key] = table.concat(data[key], "") end data = table.concat(data, "")

local head_row = {}

for _, col in ipairs(columns) do			if columns_to_show[col.code] then table.insert(head_row, col.display) end end

head_row = " " .. table.concat(head_row, " ") .. " "

return " " end

-- TODO: merge format_table with format_table_collapsed

local format_table_collapsed = function(data) local data2 = {} local row2 = {} local columns = columns_collapsed local columns_to_show = columns_to_show_collapsed

for key, row in ipairs(data) do			-- Find number of sub-rows local subrows = 1 for _, col in ipairs(columns) do				if row[col.code] and #row[col.code] > subrows then subrows = #row[col.code] end end

for subrow = 1, subrows do				row2[subrow] = {} for _, col in ipairs(columns) do					if columns_to_show[col.code] then if row[col.code] and row[col.code][subrow] then table.insert(row2[subrow], process_value(col.code, row[col.code][subrow], true)) elseif col.code ~= "variety" then table.insert(row2[subrow], "") end end end

if not data2[key] then data2[key] = {} end data2[key][subrow] = table.concat(row2[subrow], " ") end end

data = data2 for key, _ in ipairs(data) do data[key] = "" .. table.concat(data[key], ",  ") .. "" end data = table.concat(data, "")

local head_row = {}

for _, col in ipairs(columns) do			if columns_to_show[col.code] then table.insert(head_row, col.display) end end

return "" .. data .. "" end

return [=[ ]=] .. format_table_collapsed(data) .. [=[ ]=] .. format_table(data) .. [=[ ]=] end

function process_value(code, val, collapsed) if not val or val == "" then return "" elseif code == "variety" then if mw.title.new("Template:accent:" .. val).exists then return mw.getCurrentFrame:expandTemplate{title = "Template:accent:" .. val, args = {}} elseif mw.title.new(val).exists then return "" .. val .. "" else return val end elseif code == "IPA" then return m_IPA.format_IPA(lang, val) elseif code == "rhymes" and not val:find("-" .. formatted_IPA .. "" .. cat)	elseif code == "audio" then		if collapsed then			return '' .. val .. ' '		else			return mw.getCurrentFrame:expandTemplate{title = "audio", args = {val, lang = lang:getCode}}		end	elseif code == "homophones" then		return require("Module:links").full_link{term = val, lang = lang}	elseif code == "hyphenation" then		return (val:gsub("-", "‧", nil))	else		return val	end end

return export