Modul:etymology/templates/doublet

A modult a Modul:etymology/templates/doublet/doc lapon tudod dokumentálni

local export = {}

local m_internal = require("Module:etymology/templates/internal")

local rsplit = mw.text.split
local rsubn = mw.ustring.gsub

-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end


local function get_parsed_part(template, lang, args, terms, i)
	local term = terms[i]
	local alt = args["alt"][i]
	local id = args["id"][i]
	local sc = m_internal.fetch_script(args["sc"][i])

	local tr = args["tr"][i]
	local ts = args["ts"][i]
	local gloss = args["t"][i]
	local pos = args["pos"][i]
	local lit = args["lit"][i]
	local g = args["g"][i]

	if not (term or alt or tr or ts) then
		require("Module:debug/track")(template .. "/no term or alt or tr")
		return nil
	else
		return require("Module:links").full_link(
			{ term = term, alt = alt, id = id, lang = lang, sc = sc, tr = tr,
			ts = ts, gloss = gloss, pos = pos, lit = lit,
			genders = g and rsplit(g, ",") or {}
		}, "term", true)
	end
end


local function get_parsed_parts(template, lang, args, terms)
	local parts = {}

	-- Find the maximum index among any of the list parameters.
	local maxmaxindex = 0
	for k, v in pairs(args) do
		if type(v) == "table" and v.maxindex and v.maxindex > maxmaxindex then
			maxmaxindex = v.maxindex
		end
	end

	for index = 1, maxmaxindex do
		table.insert(parts, get_parsed_part(template, lang, args, terms, index))
	end
	
	return parts
end

-- Implementation of miscellaneous templates such as {{doublet}} that can take
-- multiple terms. Doesn't handle {{blend}} or {{univerbation}}, which display
-- + signs between elements and use compound_like in [[Module:compound/templates]].
function export.misc_variant_multiple_terms(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {list = true, allow_holes = true},

		["t"] = {list = true, allow_holes = true, require_index = true},
		["gloss"] = {list = true, allow_holes = true, require_index = true, alias_of = "t"},
		["tr"] = {list = true, allow_holes = true, require_index = true},
		["ts"] = {list = true, allow_holes = true, require_index = true},
		["g"] = {list = true, allow_holes = true, require_index = true},
		["id"] = {list = true, allow_holes = true, require_index = true},
		["alt"] = {list = true, allow_holes = true, require_index = true},
		["lit"] = {list = true, allow_holes = true, require_index = true},
		["pos"] = {list = true, allow_holes = true, require_index = true},
		["sc"] = {list = true, allow_holes = true, require_index = true},

		["nocap"] = {type = "boolean"}, -- should be processed in the template itself
		["notext"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["sort"] = {},
	}

	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = m_internal.fetch_lang(args[1], 1)

	local parts = {}
	if not args["notext"] then
		table.insert(parts, frame.args["text"])
	end
	if #args[2] > 0 or #args["alt"] > 0 then
		if not args["notext"] then
			table.insert(parts, " ")
			table.insert(parts, frame.args["oftext"] or "of")
			table.insert(parts, " ")
		end
		local formatted_terms = get_parsed_parts(mw.ustring.lower(
			-- Remove link and convert uppercase to lowercase to get an
			-- approximation of the original template name.
			rsub(rsub(frame.args["text"], "^%[%[.*|", ""), "%]%]$", "")),
			lang, args, args[2])
		table.insert(parts, require("Module:table").serialCommaJoin(formatted_terms))
	end
	if not args["nocat"] and frame.args["cat"] then
		local categories = {}
		table.insert(categories, lang:getCanonicalName() .. " " .. frame.args["cat"])
		table.insert(parts, require("Module:utilities/format_categories")(categories, lang, args["sort"]))
	end

	return table.concat(parts)
end

return export