Module:form of

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

Not to be invoked directly, but from Module:form of/templates instead. Used by many templates.


local m_links = require("Module:links")
local m_data = mw.loadData("Module:form of/data")

local export = {}


function export.format_t(text, term, alt, lang, sc, id, annotations)
	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>"
end


function export.form_of_t(frame)
	local iargs = frame.args
	
	local term_param = tonumber(iargs["term_param"]) or 1
	local text = iargs[1]; if text == "" then text = nil end
	local lang = iargs["lang"]
	local sc = iargs["sc"]
	local id = iargs["id"]
	
	if not text then
		error("No definition text provided.")
	end
	
	local params = {
		[term_param] = {required = true},
		[term_param + 1] = {},
		[term_param + 2] = {alias_of = "gloss"},
		
		["cap"] = {},
		["dot"] = {},
		["from"] = {},
		["from2"] = {},
		["from3"] = {},
		["from4"] = {},
		["from5"] = {},
		["from6"] = {},
		["gloss"] = {},
		["id"] = {},
		["is lemma"] = {type = "boolean"},
		["lang"] = {required = lang == nil},
		["nocap"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["nodot"] = {type = "boolean"},
		["POS"] = {},
		["pos"] = {},
		["sc"] = {},
		["sort"] = {},
		["tr"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local term = args[term_param] or "term"
	local alt = args[term_param + 1]
	
	lang = lang or args["lang"] or "und"
	sc = sc or args["sc"]
	id = id or args["id"]
	
	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)
	
	return export.format_t(text, term, alt, lang, sc, id, {gloss = args["gloss"], tr = args["tr"]})
end

function export.tagged_inflections(tags, term, alt, lang, sc, id, annotations)
	local cur_infl = {}
	local inflections = {}
	
	for i, tag in ipairs(tags) do
		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
		
		if tag == ";" then
			if #cur_infl > 0 then
				table.insert(inflections, table.concat(cur_infl, " "))
			end
			
			cur_infl = {}
		else
			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
				tag = "[[Appendix:Glossary#" .. mw.uri.anchorEncode(data.glossary) .. "|" .. tag .. "]]"
			end
			
			table.insert(cur_infl, tag)
		end
	end
	
	if #cur_infl > 0 then
		table.insert(inflections, table.concat(cur_infl, " "))
	end
	
	if #inflections == 1 then
		return export.format_t(inflections[1] .. " of", term, alt, lang, sc, id, annotations)
	else
		return
			"<span class='form-of-definition'>inflections of " ..
			"<span class='form-of-definition-link'>" .. m_links.full_link(term, alt, lang, sc, "term", id, annotations, false) .. "</span>" ..
			":</span>\n## <span class='form-of-definition'>" .. table.concat(inflections, "</span>\n## <span class='form-of-definition'>") .. "</span>"
	end
end


return export