Modulo:Aeropuerte

Da Uicchipèdie, 'a 'ngeclopedije lìbbere.

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

--[[
* Modulo a supporto del template Aeropuerte.
]]--

require('strict')

local getArgs = require('Modulo:Arguments').getArgs
local mWikidata = require('Modulo:Wikidata')

-- Legge le piste dagli argomenti passati al modulo e le restituisce come sequence Lua.
--
-- @param {table} args
-- @return {table}
local function getPiste(args)
	local params = { 'Orientamento(%d+)', 'Lunghezza(%d+)_m', 'Lunghezza(%d+)', 'Superficie(%d+)', 'Note(%d+)' }
	local piste = {}

	for k, v in pairs(args) do
		if type(k) == 'string' then
			for _, param in ipairs(params) do
				local num = k:match('^' .. param .. '$')
				if num then
					num = tonumber(num)
					piste[num] = piste[num] or {}
					piste[num][param:gsub('%(%%d%+%)', '')] = v
					break
				end
			end
		end
	end

	return piste
end

-- Ricerca le piste da Wikidata in P529 e le restituisce come sequence Lua.
--
-- @return {table}
local function getPisteFromWikidata()
	local piste = {}
	local claims

	claims = mWikidata._getClaims('P529') or {}
	for _, claim in ipairs(claims) do
		piste[#piste + 1] = {
			Orientamento = mWikidata._formatStatement(claim),
			Lunghezza_m = mWikidata._formatQualifiers(claim, 'P2043', { unit = 'metro', formatnum = true }),
			Larghezza = mWikidata._formatQualifiers(claim, 'P2049', { unit = 'metro', formatnum = true }),
			Superficie = mWikidata._formatQualifiers(claim, 'P186')
		}
	end

	return piste
end

-- Restituisce una tabella HTML contenente le piste.
-- Il colore dell'intestazione è ottenuto dal parametro "Struttura" tramite il sottotemplate Colore.
--
-- @param {table} piste
-- @param {string} struttura
-- @return {string}
local function formatPiste(piste, struttura)
	local tableStyle = {
		['text-align'] = 'center',
		['line-height'] = '1.2em'
	}
	local thcolor = mw.getCurrentFrame():expandTemplate {
		title = 'Aeropuerte/Colore',
		args = { struttura }
	}
	local tdcolor = '#d3d3d3'
	local tableNode = mw.html.create('table')

	tableNode
		:addClass('sinottico_annidata')
		:css(tableStyle)
		:tag('tr')
			:tag('th')
				:css('width', '25%')
				:css('text-align', 'center')
				:css('background-color', thcolor)
				:wikitext('Orientamento ([[QFU]])')
				:done()
			:tag('th')
				:css('width', '40%')
				:css('text-align', 'center')
				:css('background-color', thcolor)
				:wikitext('Lunghezza')
				:done()
			:tag('th')
				:css('width', '35%')
				:css('text-align', 'center')
				:css('background-color', thcolor)
				:wikitext('Superficie')
				:done()

	for _, pista in ipairs(piste) do
		if pista.Orientamento then
			local lunghezza
			if pista.Lunghezza then
				lunghezza = pista.Lunghezza
			elseif pista.Lunghezza_m then
				local larghezza = pista.Larghezza and (' x ' .. pista.Larghezza) or ''
				lunghezza = pista.Lunghezza_m .. larghezza .. ' [[metro|m]]'
			end
			tableNode:tag('tr')
				:tag('td')
					:css('background-color', tdcolor)
					:wikitext(pista.Orientamento)
					:done()
				:tag('td')
					:css('background-color', tdcolor)
					:wikitext(lunghezza)
					:done()
				:tag('td')
					:css('background-color', tdcolor)
					:wikitext(pista.Superficie)
					:done()
			if pista.Note then
				tableNode:tag('tr')
					:tag('td')
						:attr('align', 'right')
						:attr('colspan', '3')
						:wikitext(pista.Note)
						:done()
			end
		end
	end

	return tostring(tableNode)
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = {}

-- Funzione per {{#invoke:Aeropuerte|piste}}
function p.piste(frame)
	local args = getArgs(frame, { parentOnly = true })
	local piste = args.Orientamento1 and getPiste(args) or {}
	piste = #piste > 0 and piste or getPisteFromWikidata()
	return #piste > 0 and formatPiste(piste, args.Struttura) or ''
end

return p