Mòdul:lema

De Viccionari
Dreceres ràpides: navegació, cerca
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Mòdul de suport a la plantilla {{lema}}, vegeu la seva documentació.

Aquest mòdul utilitza Mòdul:llengua per obtenir el nom de llengua i l'script, i Mòdul:ca-general, Mòdul:es-general o Mòdul:fr-general per obtenir la sil·labificació i la clau d'ordenació de categories.

local export = {}

local m_llengua = require("Mòdul:llengua")
local m_links = require("Mòdul:enllaç")

local function preprocess(data)
	if type(data.heads) ~= "table" then
		data.heads = {data.heads}
	end
	
	if type(data.translits) ~= "table" then
		data.translits = {data.translits}
	end
	
	if not data.heads or #data.heads == 0 then
		data.heads = {""}
	end
	
	-- Create a default headword.
	local default_head = mw.title.getCurrentTitle().subpageText
	
	-- Add links to multi-word page names when appropriate
	if data.lang ~= "zh" then
		local spacingPunctuation = "([%s%p]+)"
		--[[ 	Variable containing anything that is
				not a punctuation character found inside of words.
				Used to exclude characters from the above regex. ]]
		local notWordPunc = "([^-־׳״'.·*]+)"
		local contains_words = false
		
		for possibleWordBreak in mw.ustring.gmatch(default_head, spacingPunctuation) do
			if mw.ustring.find(possibleWordBreak, notWordPunc) then
				contains_words = true
				break
			end
		end
		
		if contains_words then
			local function workaround_to_exclude_chars(s)
				return mw.ustring.gsub(s, notWordPunc, "]]%1[[")
			end
			
			default_head = "[["
				.. mw.ustring.gsub(
					default_head,
					spacingPunctuation,
					workaround_to_exclude_chars
					)
				.. "]]"
			--[=[
			use this when workaround is no longer needed:
			default_head = "[["
				.. mw.ustring.gsub(default_head, WORDBREAKCHARS, "]]%1[[")
				.. "]]"
			
			Remove any empty links, which could have been created above
			at the beginning or end of the string.
			]=]
			default_head = mw.ustring.gsub(default_head, "%[%[%]%]", "")
			if mw.ustring.match(default_head, "%[%[[dl]'") then
				default_head = mw.ustring.gsub(default_head, "(%[%[[dl]')", "%1]][[")
			end
		elseif (data.lang == "ca" or data.lang == "es" or data.lang == "it") then
			default_head = require("Mòdul:" .. data.lang .. "-general").sil(default_head)
		end
	end
	
	-- If a head is the empty string "", then replace it with the default
	for i, head in ipairs(data.heads) do
		if head == "" then
			head = default_head
		end
		
		data.heads[i] = head
	end
	
	-- Try to detect the script if it was not provided
	if not data.sc then
		data.sc = m_llengua.script(data.lang)
	end
	
	-- Make transliterations
	for i, head in ipairs(data.heads) do
		local translit = data.translits[i]
		
		-- Try to generate a transliteration if necessary
		if translit == "-" then
			translit = nil
		elseif not translit
			and (not (
				data.sc:find("Latn", nil, true)
				or data.sc == "Latinx"
				or data.sc == "None"
				)
				or data.lang == "xib")
		then
			local automated_tr
			automated_tr = m_llengua.trans(data.lang, m_links.remove_links(head))
			if automated_tr then
				--if translit and data.lang == "ru" then -- only for checking by language
				--	table.insert(data.categories, "Romanitzacions a revisar")
				--end
				translit = automated_tr
			end
		end
		
		data.translits[i] = translit
	end
end

-- Format a headword with transliterations
local function format_headword(data)
	
	-- Are there non-empty transliterations?
	-- Need to do it this way because translit[1] might be nil while translit[2] is not
	local has_translits = false
	
	-- Format the headwords
	for i, head in ipairs(data.heads) do
		if data.translits[i] then
			has_translits = true
		end
		
		-- Apply processing to the headword, for formatting links and such
		if head:find("[[", nil, true) then
			head = m_links.language_link({term = head, langcode = data.lang}, false)
		end
		
		-- Add language and script wrapper
		head = m_links.tag_text(head, data.lang, data.sc, "lema")
		
		data.heads[i] = head
	end
	
	local translits_formatted = ""
	
	if has_translits then
		-- Format the transliterations
		for i, head in ipairs(data.heads) do
			local translit = data.translits[i]
			
			if not translit then
				translit = "?"
			end
			
			translit = '<span class="tr" lang="" dir="ltr">' .. translit .. "</span>"
			
			data.translits[i] = translit
		end
		
		translits_formatted = " &lrm;(<span class=\"tr\" lang=\"\">" .. table.concat(data.translits, " ''o'' ") .. "</span>)"
		
		if mw.title.new("Romanització " .. m_llengua.del_nom(data.lang), "Viccionari").exists then
			translits_formatted = " [[Viccionari:Romanització " .. m_llengua.del_nom(data.lang) .. "|•]]" .. translits_formatted
		end
	end
	
	return table.concat(data.heads, " ''o'' ") .. translits_formatted
end

local function format_genders(data)
	if data.genders and #data.genders > 0 then
		local gen = require("Module:categoria gramatical")
		return "&nbsp;" .. gen.format_list(data.genders, data.lang)
	else
		return ""
	end
end

local function format_inflection_parts(data, parts)

	for key, part in ipairs(parts) do
		if type(part) ~= "table" then
			part = {term = part}
		end
		
		local qualifiers = ""
		if part.qualifiers and #part.qualifiers > 0 then
			qualifiers = " [" .. table.concat(part.qualifiers, ", ") .. "]"
		end
		
		-- Convert the term into a full link
		-- Don't show a transliteration here, the consensus seems to be not to
		-- show them in headword lines to avoid clutter.
		part = m_links.full_link(
			{
				term = not part.nolink and part.term or nil,
				alt = part.alt or (part.nolink and part.term or nil),
				langcode = part.lang or data.lang,
				sc = part.sc or parts.sc or (not part.lang and data.sc),
				id = part.id,
				genders = part.genders,
				tr = part.translit or (data.lang ~= "ar" and "-" or nil)
			}, 
			"negreta", 
			false
			)
		
		local partaccel = part.accel
		if parts.accel or partaccel then
			part = "<span class=\"form-of lang-" .. data.lang .. " " .. (parts.accel or "") .. " " .. (partaccel or "") .. "\">" .. part .. "</span>"
		end
		
		parts[key] = part .. qualifiers
	end
	
	local parts_output = ""
	
	if #parts > 0 then
		parts_output = " " .. table.concat(parts, " ''o'' ")
	end
	
	return "''" .. parts.label .. "''" .. parts_output
end

-- Format the inflections following the headword
local function format_inflections(data)
	if data.inflections and #data.inflections > 0 then
		-- Format each inflection individually
		for key, infl in ipairs(data.inflections) do
			data.inflections[key] = format_inflection_parts(data, infl)
		end
		
		return " &lrm;(" .. table.concat(data.inflections, ", ") .. ")"
	else
		return ""
	end
end

function export.full_headword(data)
	-- Preprocess
	preprocess(data)
	
	-- Format and return all the gathered information
	return
		format_headword(data) ..
		format_genders(data) ..
		format_inflections(data) ..
		require("Mòdul:utilitats").format_categories(data.categories, data.lang, data.sort_key)
end

-- Treu una llista de formes per poder interpretar fàcilment, funció usada per VriuBot via mòduls de lema per llengua.
function export.make_bot_list(forms)
	local ret = ""
	
	for key, form in pairs(forms) do
		if type(form) == "table" then
			for key2, subform in ipairs(form) do
				if type(subform) ~= "table" then
					subform = {term = subform}
				end
				ret = ret .. "* " .. form.label .. "=" .. subform.term .. "\n"
			end
		else
			ret = ret .. "* " .. key .. "=" .. form .. "\n"
		end
	end
	
	return ret
end

return export