Modulis:headword

No ''Wiktionary''
Jump to navigation Jump to search

Šī moduļa dokumentāciju var izveidot Modulis:headword/doc lapā

local export = {}

function export.full_headword(lang, sc, heads, tr, genders, inflections, categories, sort_key)
	local m_links = require("Modulis:links")
	
	-- Try to detect the script if it was not provided
	if not sc then
		sc = require("Modulis:scripts").findBestScript(heads[1] ~= "" and heads[1] or mw.title.getCurrentTitle().subpageText, lang)
	end
	
	-- Try to generate a transliteration if necessary
	-- Generate it if the script is not Latn or similar, and if no transliteration was provided
	if tr == "-" then
		tr = nil
	elseif not tr and not ((sc:getCode():find("Latn", nil, true)) or sc:getCode() == "Latinx") then
		-- TODO: This should handle transliterations for each headword separately
		tr = lang:transliterate(heads[1] ~= "" and m_links.remove_links(heads[1]) or mw.title.getCurrentTitle().subpageText, sc)
	end
	
	-- Format and return all the gathered information
	return
		export.format_headword(heads, lang, sc) ..
		export.format_transliteration(tr, lang) ..
		export.format_genders(genders, lang) ..
		export.format_inflections(inflections, lang, sc)
end

-- Format a headword
function export.format_headword(heads, lang, sc)
	local m_links = require("Module:links")
	local m_scriptutils = require("Module:script utilities")
	
	if type(heads) ~= "table" then
		heads = {heads}
	end
	
	-- Create a default headword.
	local default_head = mw.title.getCurrentTitle().subpageText
	
	-- Add links to multi-word page names when appropriate
	if default_head:find(" ", nil, false) then
		default_head = mw.text.split(default_head, " ", true)
		default_head = "[[" .. table.concat(default_head, "]] [[") .. "]]"
	end
	
	if lang:getType() == "reconstructed" or (lang:getType() ~= "appendix-constructed" and mw.title.getCurrentTitle().nsText == "Appendix") then
		default_head = "*" .. default_head
	end
	
	-- If the list is empty, fill it with the default headword
	if #heads == 0 then
		heads = {default_head}
	end
	
	-- Try to detect the script if it was not provided
	if not sc then
		sc = require("Module:scripts").findBestScript(heads[1], lang)
	end
	
	for i, head in ipairs(heads) do
		-- If the head is the empty string "", then replace it with the default
		if head == "" then
			head = default_head
		end
		
		-- Apply processing to the headword, for formatting links and such
		if head:find("[[", nil, true) then
			head = m_links.language_link(head, nil, lang)
		end
		
		-- Add language and script wrapper
		head = m_scriptutils.tag_text(head, lang, sc, "head")
		heads[i] = head
	end
	
	return table.concat(heads, " ''or'' ")
end

-- Format transliteration
function export.format_transliteration(tr, lang)
	if tr then
		local ret = " (<span class=\"tr\" lang=\"\">" .. tr .. "</span>)"
		
		if lang and mw.title.new(lang:getCanonicalName() .. " transliteration", "Wiktionary").exists then
			ret = " [[Wiktionary:" .. lang:getCanonicalName() .. " transliteration|•]]" .. ret
		end

		return ret
	else
		return ""
	end
end

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

-- Format the inflections following the headword
function export.format_inflections(inflections, lang, sc)
	if #inflections > 0 then
		-- Format each inflection individually
		for key, infl in ipairs(inflections) do
			-- If this inflection is a table, it contains parts
			-- consisting of alternating label-term pairs. Format them too.
			if type(infl) == "table" then
				inflections[key] = format_parts(infl, lang, sc)
			end
		end

		return " (" .. table.concat(inflections, ", ") .. ")"
	else
		return ""
	end
end

function format_parts(parts, lang, sc)
	local m_links = require("Module:links")
	
	for key, part in ipairs(parts) do
		if type(part) ~= "table" then
			part = {term = part}
		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(not parts.nolink and part.term or nil, part.alt or (parts.nolink and part.term or nil), lang, part.sc or sc, "bold", part.id, {genders = part.genders, tr = "-"}, mw.title.getCurrentTitle().prefixedText)
		
		if parts.accel then
			part = "<span class=\"form-of lang-" .. lang:getCode() .. " " .. parts.accel .. "\">" .. part .. "</span>"
		end
		
		parts[key] = part
	end
	
	return "''" .. parts.label .. "''" .. (#parts > 0 and " " .. table.concat(parts, " ''vai'' ") or "")
end

return export