Jump to content

Module:sem-arb-utilities

From Wiktionary, the free dictionary


local export = {}

local lang = require("Module:languages").getByCode("ar")
local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
local m_headword = require("Module:headword")
local rsplit = mw.text.split

local function link(term, alt, id)
	if word == "" or word == "—" then
		return word
	else
		return m_links.full_link({
			term = term,
			alt = alt,
			lang = lang,
			id = id,
		}, face)
	end
end

local function validateRoot(rootTable, joined_root)
	if type(rootTable) ~= "table" then
		error("rootTable is not a table", 2)
	end

	for i, letter in ipairs(rootTable) do
		if mw.ustring.len(letter) > 1 then
			error("'" .. letter .. "', the " .. ordinal[i] ..
				" letter in the root '" .. joined_root ..
				"' should be a single letter.")
		end
	end
end

function export.root(frame)
	local output = {}
	local categories = {}
	local title = mw.title.getCurrentTitle()
	local fulltitle = title.fullText
	local namespace = title.nsText

	if frame.args["lang"] then
		lang = require("Module:languages").getByCode(frame.args["lang"])
	end

	local params = {
		[1] = { list = true },
		["nocat"] = { type = "boolean" },
		["plain"] = { type = "boolean" },
		["notext"] = { type = "boolean" },
		["sense"] = {}
	}

	local args = require("Module:parameters").process(frame:getParent().args, params)
	local rootLetters = {}
	local roots = args[1]

	local separator = " "
	if frame.args["lang"] ~= "acy" then
		separator = " "
	else
		separator = "-"
	end

	local roots_len = #roots

	if #roots == 0 and namespace == "Template" then
		if frame.args["lang"] ~= "acy" then rootLetters = { "ك", "ت", "ب" } else rootLetters = { "k", "t", "p" } end
	elseif roots then
		for _, root in ipairs(roots) do
			table.insert(rootLetters, rsplit(root, separator))
		end
	else
		table.insert(rootLetters, rsplit(fulltitle, separator))
	end

	local joined_roots = {}
	for i, rootLetter in ipairs(rootLetters) do
		table.insert(joined_roots, table.concat(rootLetter, separator))
		validateRoot(rootLetter, joined_roots[i])
	end

	local sense = args["sense"]
	local sense_formatted = ""
	if sense ~= nil then
		sense_formatted = " (" .. sense .. ") "
	end

	if fulltitle == joined_roots[1] then
		if roots_len > 1 then
			error("There should be only one root.")
		end

		table.insert(output,
			m_headword.full_headword({ lang = lang, pos_category = "roots", categories = {}, heads = { fulltitle }, nomultiwordcat = true }))

		if args["nocat"] then
			return table.concat(output)
		else
			return table.concat(output) .. table.concat(categories)
		end
	else
		local link_texts = {}
		local term_counts = {}

		for i, joined_root in ipairs(joined_roots) do
			table.insert(link_texts,
				link(joined_root, joined_root .. sense_formatted, sense))
			table.insert(
				categories,
				m_utilities.format_categories(
					{ lang:getCanonicalName() .. " terms belonging to the root " .. joined_root .. sense_formatted },
					lang)
			)
			table.insert(term_counts,
				mw.site.stats.pagesInCategory(
					lang:getCanonicalName() .. " terms belonging to the root " .. joined_root .. sense_formatted, "pages")
			)
		end

		if args["nocat"] or args["plain"] then
			return table.concat(link_texts, ", ")
		else
			local link_text_output = ""
			for i, link_text in ipairs(link_texts) do
				link_text_output = link_text_output .. '<tr><td>' ..
					link_text ..
					"</td></tr><tr><td>[[:Category:" ..
					lang:getCanonicalName() ..
					" terms belonging to the root " ..
					joined_roots[i] ..
					sense_formatted ..
					"|" ..
					term_counts[i] ..
					" term" .. (term_counts[i] == 1 and "" or "s") .. "]]</td></tr>"
			end
			return
				'<table class="wikitable" style="float: right; clear: right; text-align: center;"><tr><th>[[w:Semitic root|Root' ..
				(#term_counts == 1 and "" or "s") .. ']]</th></tr>' .. link_text_output ..
				'</table>' .. table.concat(categories)
		end
	end
end

return export