Module:Ancient Greek words with English derivatives

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

A user suggests that this Translingual module be moved, merged or split.
Please see the discussion on Requests for moves, mergers and splits(+) for more information and remove this template after the request has been fulfilled.

local export = {}

local function interpret_character_entities(text)
	return text:gsub(
		"&(#?)(x?)([^;]+);",
		function (numeric, hexadecimal, code)
			if numeric == "#" then
				if hexadecimal == "x" then
					return mw.ustring.char(tonumber(code, 16))
				else
					return mw.ustring.char(tonumber(code))
				end
			end
		end)
end

-- U+0304 COMBINING MACRON, U+0306 COMBINING BREVE
local function remove_macron_breve(text)
	return mw.ustring.toNFD(text):gsub("\204[\132\134]", "")
end

-- U+0300 COMBINING GRAVE ACCENT, U+0301 COMBINING ACUTE ACCENT, U+0342 COMBINING GREEK PERISPOMENI
local function remove_accents(text)
	return mw.ustring.toNFD(interpret_character_entities(text)):gsub("\204[\129\128]", ""):gsub("\205\130", "")
end

local function link(text)
	text = interpret_character_entities(text)
	return '<span class="Polyt" lang="grc">[['
		.. remove_macron_breve(text)
		.. '|' .. text .. ']]</span>'
end

local function tag(text)
	return '<span class="Polyt" lang="grc">' .. text .. '</span>'
end

local function form_English_link(entry, alt)
	return "[[" .. entry .. "#English|" .. (alt ~= "" and alt or entry) .. "]]"
end

local function English_link(text)
	if not text or text == "" then
		return "&mdash;"
	elseif text:find("[[", 1, true) then
		return text:gsub("%[%[([^|%]]+)|?(.-)%]%]", form_English_link)
	else
		return form_English_link(text)
	end
end

local auto_subtable_mt = {
	__index = function(self, key)
		local val = {}
		self[key] = val
		return val
	end,
}

local function auto_subtable()
	return setmetatable(
		{},
		auto_subtable_mt)
end

local function make_row_table(cells, column_count)
	local trim = mw.text.trim
	local rows = auto_subtable()
	
	local column_number = 0
	local row_number = 1
	for i, cell in ipairs(cells) do
		if column_number == column_count then
			column_number = 1
			row_number = row_number + 1
		else
			column_number = column_number + 1
		end
		
		rows[row_number][column_number] = trim(cell)
	end
	
	-- Remove auto-subtabling.
	setmetatable(rows, nil)
	
	return rows
end

local function print_table(rows)
	local output = {}
	for i, row in ipairs(rows) do
		row[1] = interpret_character_entities(row[1])
		output[i] = ("|-\n| %s || %s || %s || %s || %s || %s\n")
			:format(link(row[1]), (require("Module:languages").getByCode("grc"):transliterate(row[1])), tag(remove_accents(row[2])), row[3] or "", row[4] or "", English_link(row[5]))
	end
	
	table.insert(output, 1, '{| class="wikitable"\n! colspan="2" | Citation form || colspan="2" | Root form || Meaning || English derivative\n')
	table.insert(output, "|}")
	
	return table.concat(output)
end

function export.Greek_derivative_table(frame)
	local args = frame:getParent().args
	return print_table(make_row_table(args, 5))
end

return export