Module:Limbàgios

Dae Wikipedia, s'entziclopedia lìbera.

Documentation for this module may be created at Module:Limbàgios/doc

local language = {}
local getArgs = require('Module:Arguments').getArgs

-- Documentu de configuratzione cun in intro duas tabellas:
-- lg.alias: pro normalizare sos alias a unu còdighe ùnicu
-- lg.codighes: chi, tentu unu codighe, frunit un'array contente
-- nùmene de sa limba e collegamentu a s'artìculu in sc.wiki dedicadu a sa limba
local lg = mw.loadData( 'Module:Limbàgios/Configuratzione');

-- frunit su nùmene de sa boghe chi currispondet a "code". Si "code" non currispondet a peruna
-- limba insertada in tabella frunit un'istringa nudda
function language.get_boghe(code)
	if code == nil then return '' end
	local code = mw.ustring.lower(code)
	if lg.alias[code] then code = lg.alias[code] end
	limba = lg.codighes[code]
	if limba then
		return limba[2]
	end
	return ''
end

-- frunit beru si "code" currispondet a unu limbàgiu, si nono farsu
function language.esistit(code)
	if code == nil or code=='' then return false end
	if lg.alias[code] then code= lg.alias[code] end
	if lg.codighes[code] then return true end
	return false
end

-- Frunit su nùmene istandard de sa limba chi currispondet a "code" e su còdighe normalizadu
-- a cussa de sa tabella codighes.
-- Si "code" non currispondet a peruna limba insertada in sa tabella, frunit su balore de default
-- o istringa nulla si custu no est indicadu e su còdighe rechertu. Si maiùsculu est beru su --
-- nùmene benit iscritu cun sa prima lìtera maiùscola.
function language.get_numene(code, maiusculu, default)
	local numene = default or ''
	if code ~= nil and code ~= '' then
		code = mw.ustring.lower(code)
		if lg.alias[code] then code = lg.alias[code] end
		-- boga sa segunda parte dae còdighes comente a it-IT
		code = mw.ustring.gsub(code, '(%l+)%-.*', '%1')
		local limba = lg.codighes[code]
		if limba then numene = limba[1] end
	end
	if maiusculu then
		numene = mw.ustring.gsub(numene, '^%l', mw.ustring.upper)
	end
	return numene, code
end

-- funtzione de interfache pro su template:Nùmenelimba/boghe
function language.boghe(frame)
	local code = frame.args[1]
	if code == nil or code == '' then
		return ''
	end
	return language.get_boghe(code)
end

-- funtzione de interfache pro su template:Nùmenelimba/nùmene
function language.numene(frame)
	local maiusculu = false
	local code = frame.args[1] or ''
	local default = (frame.args[2] == 'v' and '') or code
	if frame.args['M']~=nil and frame.args['M']~='' then
		maiusculu = true
	end
	return language.get_numene(code, maiusculu, default), _

end

--funtzione de interfache pro su template:Nùmenelimba
function language.numeneintreu(frame)
	local code = frame.args[1]
	if code == nil or code == '' then
		return ''
	end
	local maiusculu = false
	if frame.args['M']~=nil and frame.args['M']~='' then
		maiusculu = true
	end
	local boghe = language.get_boghe(code)
	local numene = language.get_numene(code, maiusculu, code)
	if numene == "" and boghe ~="" then
		if maiusculu then
			numene = mw.ustring.gsub(boghe, '^%l', mw.ustring.upper)
		else
			numene = boghe
		end
	end
	if boghe == "" then
		return numene
	else
		return table.concat({'[[', boghe, "|", numene, "]]"})
	end
end

--funtzione de interfàtzia pro template:Limbas, si benit coladu fintzas su paràmetru
--imprea_codighe (cale si siat siat su balore suo), tando su testu ammustradu intre parèntesis
--benit normalizadu a cussu istandard de sa limba, imbetzes de èssere cussu
--coladu
--In agiunta podet retzire, si su paràmetru return_error est beru, frunit in casu de
--codighes limba no agatados in tabella una tabella agiuntiva cun sos codighes errados
--return_error benit compidadu petzi si sa funtzione est mutida torra dae unu mòdulu Lua
--si est mutida torra dae unu template est semper farsa
function language.limbas(frame)
	local limbas_list = { }
    -- Si mutida pro mèdiu de  #invoke, impreat sos argumentos colados a su template invocante.
	-- Si nono, cun sa punna de proare, assumet chi sos argumentos siant colados diretamente
	local args
	local return_error = false
	local error_list = {}
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
		return_error = args['return_error'] or false
	end
	local limba, codighe_normalizadu, code_to_show

	local yet_processed = {}
	for _,code in ipairs(args) do
		limba, codighe_normalizadu = language.get_numene(code)
		codighe_normalizadu = mw.ustring.upper(codighe_normalizadu)
		if not yet_processed[ codighe_normalizadu ] then
			if limba ~= "" then
				limbas_list[#limbas_list+1] = '<abbr title="' .. limba .. '">' ..codighe_normalizadu .. "</abbr>"
				yet_processed[ codighe_normalizadu ] = true
			else
				limbas_list[#limbas_list+1] = code
				if return_error then error_list[#error_list+1] = code end
			end
		end
	end
	local reply
	if #limbas_list > 0 then
		reply = '(<span style="font-weight:bolder; font-size:80%">' .. table.concat(limbas_list, ",&nbsp;") .. "</span>)"
	else
		reply = ''
	end
	reply = reply
	if #error_list>0 then
		return reply, error_list
	else
		return reply
	end
end

-- ========================================================
-- Frunit s'istringa txt insertada in unu tag <span> cun indicadu
-- su codighe limbàgiu de su testu (language_code) e su sensu
-- de letura (sinistru o destru).
-- Su paràmetru "corsivu" permitet de indicare si su testu
-- depet èssere postu in corsivu, tenet tres balores:
-- -- d: consulta sas tabellas de configuratzione pro su limbàgiu
-- -- s: fortzat corsivu ativu
-- -- n: fortzat corsivu disativadu (est su balore de default)
-- ========================================================
function language._lang_testu(args)
	local txt = args.txt or args[2]
	if not txt then return '' end
	local language_code = args.lang or args[1]
	local direction = "ltr"
	if language_code then
		language_code = mw.ustring.lower(language_code)
		language_code = lg.alias[language_code] or language_code
		local writing = lg.iscritura[language_code]
		if writing then
			direction = writing[1]
			if #writing > 1 then language_code = writing[2] end
		end
	end
	local set_italic = args.corsivu or "n"
	if set_italic ~= "n" then
		local italic = false
		if set_italic:lower() == 's' then
			italic = true
		elseif language_code and lg.codighes[language_code] then
			italic = not lg.codighes[language_code][3]
		else
			italic = true
		end	  
		if italic then
			if txt[1]=="'" then txt = "<nowiki />" .. txt end
			if txt[#txt] =="'" then txt = txt .. "<nowiwiki />" end
			txt = mw.ustring.format("''%s''", txt)
		end
	end
	local span = mw.html.create('span'):wikitext(txt):addClass(args.class)
	if lg.codighes[language_code] then
		span
			:attr('dir', direction)
			:attr('lang', language_code)
			:attr('xml:lang', language_code)
	end			
	return tostring(span)
end

-- ========================================================
-- Funtzione de interfache pro _lang_testu
-- chi podet èssere torrada a mutire in sos templates
-- ========================================================
function language.lang_testu(frame)
	local args = getArgs(frame)
	return language._lang_testu(args)
end

-- Frunit una tabella cun totu sos còdighes reconnotos dae su mòdulu
function language.tabella(frame)

	-- gènerat una tabella còdighes -> lista alias e una de còdighes pro l'ordinare
	local alias_table = {}
	local codighes_sorted = {}
	for code, _ in pairs(lg.codighes) do
		alias_table[code] = {"'''" .. code .. "'''"}
		codighes_sorted[#codighes_sorted+1] = code
	end
	for alias, code in pairs(lg.alias) do
		if alias_table[code] then table.insert(alias_table[code], alias) end
	end
	table.sort(codighes_sorted)

	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th'):wikitext('codighes'):done()
			:tag('th'):wikitext('[[template:Nùmenelimba/nùmene]]'):done()
			:tag('th'):wikitext('[[template:Nùmenelimba/boghe]]'):done()
			:tag('th'):wikitext('[[template:Limbas]]')

	for _,code in ipairs(codighes_sorted) do
		local code_string = table.concat(alias_table[code], ", ")
		local numene, boghe = language.get_numene(code), language.get_boghe(code)
		if boghe ~= '' then boghe = '[[' .. boghe .. ']]' end
		root
			:tag('tr')
				:tag('td'):wikitext(code_string):done()
				:tag('td'):wikitext(numene):done()
				:tag('td'):wikitext(boghe):done()
				:tag('td'):css('text-align', 'center'):wikitext(language.limbas({code}))
	end
	return tostring(root)
end

-- Frunit una tabella de sos alias in formadu alias;codighe
function language.tabella_alias(frame)

	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th'):wikitext('Alias'):done()
			:tag('th'):wikitext('codighe'):done()
	for alias, code in pairs(lg.alias) do
		root
			:tag('tr')
				:tag('td'):wikitext(alias):done()
				:tag('td'):wikitext(code)
	end
	return tostring(root)
end

-- Frunit una tabella de sos còdighes in formadu codighe;numene;boghe
function language.tabella_codighes(frame)

	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th'):wikitext('Còdighe'):done()
			:tag('th'):wikitext('Nùmene'):done()
			:tag('th'):wikitext('Boghe'):done()
	for code, valore in pairs(lg.codighes) do
		root
			:tag('tr')
				:tag('td'):wikitext(code):done()
				:tag('td'):wikitext(valore[1]):done()
				:tag('td'):wikitext(valore[2])
	end
	return tostring(root)
end

-- Frunit una whitelist de totu sos còdighes reconnotos a impreu bot in python
function language.whitelist(frame)
	local rows = { 'WHITELIST_LIMBAS = set( [' }
	-- gènerat una tabella còdighes -> lista alias e una de còdighes pro l'ordinare
	local codighes = {}
	for code, _ in pairs(lg.codighes) do
		codighes[#codighes+1] = code
	end
	for alias, _ in pairs(lg.alias) do
		codighes[#alias+1] = alias
	end
	table.sort(codighes)
	for _, codighe in ipairs(codighes) do
		rows[#rows+1] = "	'" .. codighe .."',"
	end
	rows[#rows+1] = "])"
	return table.concat(rows, '\n')
end

return language