انتقل إلى المحتوى

وحدة:form of

من ويكاموس، القاموس الحر
local m_links = require("Module:links")
local m_data = mw.loadData("Module:form of/data")

local export = {}

local function format_t(args, iargs, text, term, alt, gloss)
	NAMESPACE = mw.title.getCurrentTitle().nsText

	local categories = {}
	local annotations = {}
	
	local lang = iargs["lang"] or args["lang"]; if lang == "" then lang = nil end
	local sc = iargs["sc"] or args["sc"]; if sc == "" then sc = nil end
	local id = iargs["id"] or args["id"]; if id == "" then id = nil end
	annotations.gloss = gloss
	annotations.tr = args["tr"]; if annotations.tr == "" then annotations.tr = nil end
	
	if not lang then
		lang = "en"
		table.insert(categories, "Language code missing/form of")
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	if not term then
		if NAMESPACE == "Template" then
			term = "term"
		else
			error("You have not given a term to link to.")
		end
	end
	
	if not text then
		error("No definition text provided.")
	end
	
	if #categories > 0 then
		local m_utilities = require("Module:utilities")
		categories = m_utilities.format_categories(categories, lang, nil)
	else
		categories = ""
	end
	
	return
		"<span class='form-of-definition'>" .. text .. " " ..
		"<span class='form-of-definition-link'>" .. m_links.full_link(term, alt, lang, sc, "term", id, annotations, false) .. "</span>" ..
		"</span>" .. categories
end


function export.form_of_t(frame)
	local args = frame:getParent().args
	local iargs = frame.args
	
	local term_param = tonumber(iargs["term_param"]) or 1
	local text = iargs[1]; if text == "" then text = nil end
	
	local term = args[term_param]; if term == "" then term = nil end
	local alt = args[term_param + 1]; if alt == "" then alt = nil end
	local gloss = args.gloss or args[term_param + 2]; if gloss == "" then gloss = nil end
	
	local categories = ""
	
	return format_t(args, iargs, text, term, alt, gloss) .. categories
end


function export.inflection_t(frame)
	local args = frame:getParent().args
	local iargs = frame.args
	
	local term = args[1]; if term == "" then term = nil end
	local alt = args[2]; if alt == "" then alt = nil end
	local gloss = args["gloss"]; if gloss == "" then gloss = nil end
	
	local tags = {}
	local i = 3
	local tag = args[i]
	
	while tag do
		if tag ~= "" then
			if m_data.shortcuts[tag] then
				require("Module:debug").track("inflection of/known")
				require("Module:debug").track("inflection of/known/" .. tag)
			elseif m_data.tags[tag] then
				require("Module:debug").track("inflection of/possible")
				require("Module:debug").track("inflection of/possible/" .. tag:gsub("%[", "("):gsub("%]", ")"):gsub("|", "!"))
			else
				require("Module:debug").track("inflection of/unknown")
				require("Module:debug").track("inflection of/unknown/" .. tag:gsub("%[", "("):gsub("%]", ")"):gsub("|", "!"))
			end
			
			tag = m_data.shortcuts[tag] or tag
			local data = m_data.tags[tag]
			
			-- If there is a nonempty glossary index, then show a link to it
			if data and data.glossary then
				table.insert(tags, "[[Appendix:Glossary#" .. mw.uri.anchorEncode(data.glossary) .. "|" .. tag .. "]]")
			else
				table.insert(tags, tag)
			end
		end
		
		i = i + 1
		tag = args[i]
	end
	
	return format_t(args, iargs, table.concat(tags, " ") .. " of", term, alt, gloss)
end


return export