Mòdul:lema/plantilles

De Viccionari
Salta a la navegació Salta a la cerca

La documentació d'ús d'aquest mòdul es pot crear a Mòdul:lema/plantilles/ús

local export = {}

-- Suport per la plantilla:lema
function export.plantilla_lema(frame)
	local args = frame:getParent().args
	
	-- Get language and script information
	local data = {}
	data.lang = args[1] or (mw.title.getCurrentTitle().nsText == "Plantilla" and "ca") or error("Falta el codi de llengua com a primer paràmetre.")
	data.sort_key = args["ordre"]; if data.sort_key == "" then data.sort_key = nil end
	data.sc = args["sc"] or require("Mòdul:llengua").script(data.lang)
	
	-- Gather headwords
	data.heads = {}
	data.translits = {}
	
	local head = args["lema"] or ""
	local translit = args["tr"]; if translit == "" then translit = nil end
	local i = 1
	
	while head do
		if head then
			table.insert(data.heads, head)
			data.translits[#data.heads] = translit
		end
		
		i = i + 1
		head = args["lema" .. i]; if head == "" then head = nil end
		translit = args["tr" .. i]; if translit == "" then translit = nil end
	end
	
	-- Gather gender and number specifications
	-- Iterate over all gn parameters (g2, g3 and so on) until one is empty
	data.genders = {}
	
	local g = args["g"]; if g == "" then g = nil end
	local i = 2
	
	while g do
		table.insert(data.genders, g)
		g = args["g" .. i]; if g == "" then g = nil end
		i = i + 1
	end
	
	-- Part-of-speech category
	data.categories = {}
	
	local langname = require("Mòdul:llengua").nom(data.lang)
	if langname == data.lang then error("El codi de llengua no existeix") end
	local langcat = " en " .. langname
	if langname == "multilingüe" then
		langcat = " multilingües"
	end
	
	local pos = args[2]; if pos == "" then pos = nil end
	local cat = args["cat"]; if cat == "" then cat = nil end
	
	if cat then
		table.insert(data.categories, cat .. langcat)
	elseif pos then
		-- Make the plural form of the part of speech
		local pos_abrev = {["abr"]="Abreviatures", ["adv"]="Adverbis", ["adj"]="Adjectius", ["conj"]="Conjuncions", 
			["interj"]="Interjeccions", ["nom"]="Substantius", ["prep"]="Preposicions", ["pron"]="Pronoms",
			["adv-forma"]="Formes d'adverbis", ["adj-forma"]="Formes d'adjectius",
			["nom-forma"]="Formes de substantius", ["pronom-forma"]="Formes de pronoms", ["verb-forma"]="Formes verbals"}
		if pos_abrev[pos] then
			pos = pos_abrev[pos]
		else
			pos, res = require("Mòdul:ca-lema").forma_plural_nominal(pos, "-")
			pos = string.upper(string.sub(pos, 1, 1)) .. string.sub(pos, 2)
		end
		if string.find(head or mw.title.getCurrentTitle().text, "[^ ] [^ ]") then
			local pos_loc = {["Adverbis"]="adverbials", ["Adjectius"]="adjectivals", ["Conjuncions"]="conjuntives", 
				["Interjeccions"]="interjectives", ["Preposicions"]="prepositives", ["Substantius"]="nominals", ["Verbs"]="verbals"}
			if pos_loc[pos] then
				pos = "Locucions " .. pos_loc[pos]
			end
		end
		
		table.insert(data.categories, pos .. langcat)
	end
	
	-- Additional categories
	if args.cat2 then
		table.insert(data.categories, args.cat2 .. langcat)
	end
	
	if args.cat3 then
		table.insert(data.categories, args.cat3 .. langcat)
	end
	
	-- Inflected forms
	data.inflections = {}
	
	local i = 1
	local label = args[i * 2 + 1]
	local accel = args["f" .. i .. "accel"]; if accel == "" then accel = nil end
	local parts = {label = label, accel = accel}
	
	while label do
		local term = args[i * 2 + 2]; if term == "" then term = nil end
		local alt = args["f" .. i .. "alt"]; if alt == "" then alt = nil end
		local sc = args["f" .. i .. "sc"]; if sc == "" then sc = nil end
		local id = args["f" .. i .. "id"]; if id == "" then id = nil end
		local gender = args["f" .. i .. "g"]; if gender == "" then gender = nil end
		local qualifier = args["f" .. i .. "qual"]; if qualifier == "" then qualifier = nil end
		local nolink = args["f" .. i .. "nolink"]; if nolink == "" then nolink = nil end
		
		if term or alt then
			table.insert(parts, {term = term, alt = alt, sc = sc, id = id, translit = translit, genders = {gender}, qualifiers = {qualifier}, nolink = nolink})
		end
		
		i = i + 1
		label = args[i * 2 + 1]
		accel = args["f" .. i .. "accel"]; if accel == "" then accel = nil end
		
		-- If the next label is not "or" then insert the previous one and create a new one.
		if label ~= "o" then
			-- Only insert if the previous label is not empty.
			if (parts.label or "") ~= "" then
				table.insert(data.inflections, parts)
			end
			
			parts = {label = label, accel = accel}
		end
	end
	
	return require("Module:lema").full_headword(data)
end

return export