[go: up one dir, main page]

Vés al contingut

Mòdul:lema

De Viccionari
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 {{entrada}}, 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 p = {}

local m_languages = require("Module:llengua")
local m_links = require("Module:enllaç")
local m_general = require("Module:ca-general")
local notranslit = {["mul"]=true, ["sh"]=true}
local title = mw.title.getCurrentTitle()

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 = title.subpageText
	
	-- Add links to multi-word page names when appropriate
	if data.lang.code ~= "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
		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 = data.lang.sc
	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 (data.lang.code == "xib" or not (data.sc:find("Latn", nil, true) or data.sc == "Latinx" or data.sc == "None")) then
			translit = m_languages.trans(data.lang.code, m_links.remove_links(head))
			if not translit and not notranslit[data.lang] then
				translit = "<small>manca transliteració</small>"
				table.insert(data.categories, "Termes en " .. data.lang.name .. " sense transliteració")
			end
		end
		
		data.translits[i] = translit
	end
end

-- Format a headword with transliterations
local function formatHeadword(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, lang = data.lang}, false)
		end
		
		-- Add language and script wrapper
		head = m_links.tag_text(head, data.lang.code, 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>)"
		
		local particle_name = m_general.addParticle("del", data.lang.name)
		if mw.title.new("Romanització " .. particle_name, "Viccionari").exists then
			translits_formatted = " [[Viccionari:Romanització " .. particle_name .. "|•]]" .. translits_formatted
		end
	end
	
	return table.concat(data.heads, " ''o'' ") .. translits_formatted
end

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

local function formatInflectionParts(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),
				lang = 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 (not (parts.enable_auto_translit or data.inflections.enable_auto_translit) and "-" or nil),
			}, 
			"negreta", 
			false
			)
		
		local partaccel = part.accel
		if parts.accel or partaccel then
			part = "<span class=\"form-of lang-" .. data.lang.code .. " " .. (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 formatInflections(data)
	if data.inflections and #data.inflections > 0 then
		-- Format each inflection individually
		for key, infl in ipairs(data.inflections) do
			data.inflections[key] = formatInflectionParts(data, infl)
		end
		
		return " &lrm;(" .. table.concat(data.inflections, ", ") .. ")"
	else
		return ""
	end
end

function p.full_headword(data)
	-- Preprocess
	preprocess(data)
	
	-- Categorise sign glosses
	if data.lang.code == 'csc' then
		table.insert(data.categories, "Glosses en " .. data.lang.name)
	end
	
	-- Categorise for palindromes
	if data.lang.type ~= 'annex' and mw.ustring.len(title.subpageText) > 2 and require('Module:palíndroms').is_palindrome(title.subpageText, data.lang.code, data.sc) then
		table.insert(data.categories, "Palíndroms en " .. data.lang.name)
	end
	
	-- Format and return all the gathered information
	return
		formatHeadword(data) ..
		formatGenders(data) ..
		formatInflections(data) ..
		require("Module: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 p.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 p