Module:tzm-utilities


local export = {}
local sc = require("Module:scripts").getByCode("Tfng")
local lang = require("Module:languages").getByCode("tzm")
local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
local m_headword = require("Module:headword")

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

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

	local params = {
		[1] = {},
		["nocat"] = {type = "boolean"},
		["table"] = {type = "boolean"},
		["t"] = {},
		["gloss"] = {alias_of = "t"},
		["notext"] = {type = "boolean"},
		["nolink"] = {type = "boolean"},
		["sort"] = {},
		["face"] = {}
	}

	local args = require("Module:parameters").process(frame:getParent().args, params)

	local root

	if not args[1] and namespace == "Template" then
		root = "ⵎⵖⵓⵔ"
	elseif args[1] then
		root = args[1]
	else
		root = fulltitle
	end

	if fulltitle == root then
		table.insert(output, m_headword.full_headword(
				{
					lang = lang,
					sc = sc,
					pos_category = "roots",
					categories = {},
					heads = {root}
				}))

		table.insert(categories, "[[Category:Central Atlas Tamazight roots|" .. (args["sort"] or root) .. "]]")

		if args["nocat"] then
			return table.concat(output)
		else
			return table.concat(output) .. table.concat(categories)
		end

	else
		local link_text

		if args["nolink"] then
			link_text = link(nil, args["gloss"], args["face"], root)
		else
			link_text = link(root, args["gloss"], args["face"])
		end

		table.insert(output, link_text)

		table.insert(categories, m_utilities.format_categories(
				{"Central Atlas Tamazight terms belonging to the root " .. root},
				lang))

		if args["nocat"] then
			return table.concat(output)
		elseif args["table"] then
			return "<table class=\"wikitable\" style=\"float: right; clear: right; text-align: center;\"><tr><th>[[w:Semitic root|Root]]</th></tr><tr><td>" .. link_text .. "</td></tr></table>" .. table.concat(categories)
		else
			return table.concat(output) .. table.concat(categories)
		end
	end

end

return export