Module:frp-IPA/utilities

From Wiktionary, the free dictionary
Jump to navigation Jump to search


local m_main = require("Module:frp-IPA")
local data_page = "Module:frp-IPA/data"
local frp = require("Module:languages").getByCode("frp")

local export = {}

local function make_status(res)
	return res and ("failed ❌\n" .. res) or "passed ✔\n"
end

local function check_dupe_key()
	local src = mw.title.new(data_page):getContent()
	local seen = {}
	for key in src:gmatch('%["[^"\n]+"%]') do
		if seen[key] then
			return "** Duplicated code: <code>" .. key .. "</code>\n"
		end
		seen[key] = true
	end
	return false
end

local function check_comma()
	local data = mw.loadData(data_page)
	for _, locs in pairs(data) do
		if type(locs) == "table" then
			for key, _ in pairs(locs) do
				if key:match(",") then
					return "** Comma found in code: <code>" .. key .. "</code>\n"
				end
			end
		end
	end
	return false
end

local function check_double_redir()
	local data = mw.loadData(data_page)
	for _, locs in pairs(data) do
		if type(locs) == "table" then
			for key, val in pairs(locs) do
				if type(val) == "string" and type(locs[val]) == "string" then
					return "** Double redirect found: <code>" .. key
						.. "</code> &rarr; <code>" .. val
						.. "</code> &rarr; <code>" .. locs[val] .. "</code>\n"
				end
			end
		end
	end
	return false
end

-- check the validity of [[Module:frp-IPA/data]]
function export.check_data(frame)
	local dupe_key = check_dupe_key()
	local comma = check_comma()
	local double_redir = check_double_redir()
	local all_pass = (not dupe_key) and (not comma) and (not double_redir)
	local msg = "* Checking for duplicate codes: " .. make_status(dupe_key)
		.. "* Checking for commas in codes: " .. make_status(comma)
		.. "* Checking for double redirects: " .. make_status(double_redir)
	if all_pass then
		return '<div class="vsSwitcher" data-toggle-category="checks">'
			.. '<div style="position:relative;">All test passed! ✔<span class="vsToggleElement" style="position:absolute;"></span></div>'
			.. '<div class="vsHide">\n'
			.. msg
			.. '\n</div></div>'
	else
		return '== Checks ==\n' .. msg
	end
end

-- generate a template for calling [[Template:frp-IPA]]
function export.template(frame)
	local function span(text,class)
		return text
		--return '<span class="'..class..'">'..text..'</span>'
	end
	local res = ""
	res = res .. '<div class="mw-highlight">'
	res = res .. "<pre>{{"..span("frp-IPA","s2").."\n"
	res = res .. "|"..span("ALF","nb").."=\n"
	res = res .. "|"..span("AIS","nb").."=\n"
	for _,code in ipairs(m_main.codes) do
		res = res .. "|"..span(code,"kd").."=\n"
	end
	res = res .. "}}</pre></div>"
	return res
end

local function gather_locs(loc_data)
	-- first pass: gather all the aliases
	local aliases = {}
	for key, val in pairs(loc_data) do
		if type(val) == "string" then
			aliases[val] = key
		end
	end
	-- second pass: gather name, wiki, aliases
	local res = {}
	for key, val in pairs(loc_data) do
		if type(val) ~= "string" then
			local alias = aliases[key]
			alias = alias and "<code>"..alias.."</code>" or ""
			local wiki = (type(val) == "table") and val.wiki or key
			local sortkey = frp:makeSortKey(key)
			table.insert(res, {key, wiki, alias, sortkey})
		end
	end
	table.sort(res, function(a,b) return a[4] < b[4] end)
	return res
end

-- make a table for [[Module:frp-IPA/data]]
function export.make_table(frame)
	local data = mw.loadData(data_page)
	local accents = mw.loadData("Module:labels/data/lang/frp")
	local res = {}
	local function ins(text)
		table.insert(res,text)
	end
	ins('{| style="width:650px"')
	ins("|+ Localities for [[Template:frp-IPA]]")
	ins("|-")
	for _,code in ipairs(m_main.codes) do
		if data[code] then
			ins('{| class="wikitable vsSwitcher" style="width:650px; margin:0px" data-toggle-category="' .. accents[code] .. '"')
			ins('! colspan=3 style="position:relative;" | ' .. accents[code] .. " (" .. code .. ")"
				.. '<span class=vsToggleElement style="position:absolute; right:5px;"></span>')
			ins("|- class=vsHide")
			ins('! style="width:300px" | Name')
			ins('! style="width:300px" | Wiki')
			ins('! style="width:50px" | Number')
			for _,loc in ipairs(gather_locs(data[code])) do
				ins("|- class=vsHide")
				ins("| <code>" .. loc[1] .. "</code>")
				ins("| [[w:" .. loc[2] .. "]]")
				ins("| " .. loc[3])
			end
			ins('|}')
		end
	end
	ins("|}")
	return table.concat(res,"\n")
end

return export