Salta al contegnùo

Modulo:lema

Da Wikisionario

La documentazione per questo modulo può essere creata in Modulo:lema/man

local export = {}

local m_lengua = require("Modulo:łéngua")
local m_links = require("Modulo:lighi")

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 ~= "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 = m_lengua.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_lengua.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_lengua.del_nom(data.lang), "Viccionari").exists then
			translits_formatted = " [[Viccionari:Romanització " .. m_lengua.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("Modulo:categoria gramategałe")
		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 (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 .. " " .. (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)
	
	-- Categorise sign glosses
	if data.lang == 'csc' then
		table.insert(data.categories, "Glose in " .. m_lengua.nom(data.lang))
	end
	
	-- Categorise for palindromes
	if mw.ustring.len(title.subpageText) > 2 and require('Module:palìndromi').is_palindrome(title.subpageText, data.lang, data.sc) then
		table.insert(data.categories, "Palìndromi in " .. m_lengua.nom(data.lang))
	end
	
	-- Format and return all the gathered information
	return
		format_headword(data) ..
		format_genders(data) ..
		format_inflections(data) ..
		require("Modulo:utilitats").format_categories(data.categories, data.lang, data.sort_key)
end

return export
Traesto fora da Wikipèdia - L'ençiclopedia łìbara e cołaboradiva in łéngua Vèneta "https://vec.wiktionary.org/w/index.php?title=Modulo:lema&oldid=37059"