Open main menu

This is a private module sandbox of ZxxZxxZ, for his own experimentation. Items in this module may be added and removed at ZxxZxxZ's discretion; do not rely on this module's stability.


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 = "[[Wiktionary:IPA|IPA]]"},
	{code = "audio", display = "[[Wiktionary:Audio|Audio]]" },
}

local columns_uncollapsed = {
	{code = "variety", display = "variety"},
	{code = "IPA", display = "[[Wiktionary:IPA|IPA]]"},
	{code = "rhymes", display = "[[Rhymes:" .. lang:getCanonicalName() .. "|Rhymes]]"},
	{code = "audio", display = "[[Wiktionary:Audio|Audio]]" },
	{code = "homophones", display = "[[homophone|Homophones]]"},
	{code = "hyphenation", display = "[[Wiktionary:Hyphenation|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] = "<tr><td"
				if subrow == 1 and subrows > 1 then
					data2[key][subrow] = data2[key][subrow] .. " rowspan=\"" .. subrows .."\""
				end
				data2[key][subrow] = data2[key][subrow] .. ">"
				.. table.concat(row2[subrow], "</td><td>") .. "</td></tr>"
			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 = "<tr><th>" .. table.concat(head_row, "</th><th>") .. "</th></tr>"

		return "<table class=\"wikitable\">" .. head_row .. data .. "</table>"
	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] = "<li>" .. table.concat(data[key], ", &nbsp; ") .. "</li>"
		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 "<ul>" .. data .. "</ul>"
	end



	return [=[<div class="vsSwitcher vsClass-pronunciations">
<span class="putButtonHere" style="float:right">&nbsp;</span>
<div class="showme" style="display:none">]=] .. format_table_collapsed(data) .. [=[</div>
<div class="hideme">]=] .. format_table(data) .. [=[</div>
</div>]=]
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 .. "#English|" .. val .. "]]"
		else
			return val
		end
	elseif code == "IPA" then
		return m_IPA.format_IPA(lang, val)
	elseif code == "rhymes" and not val:find("[[", nil, true) then
		local formatted_IPA, cat = m_IPA.format_IPA(lang, val, true)
		return ("[[Rhymes:" .. lang:getCanonicalName() .. ":-" .. val
		 .."|-" .. formatted_IPA .. "]]" .. cat)
	elseif code == "audio" then
		if collapsed then
			return '<div class="audiofile" style="vertical-align:top; display:inline-block; list-style:none; line-height:1em;">[[File:' .. val .. '|noicon|68px]]</div>'
		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