Vai al contenuto

Modulo:Infobox nave

Da Wikipedia, l'enciclopedia libera.


Questo modulo contiene funzioni di appoggio al template {{Infobox nave}}


local p = {}
local getArgs = require('Module:Arguments').getArgs
local error_flag = false

---============================================================
-- Save argument in table
---============================================================
local function dump(t, ...)
	local args = {...}
	for _, s in ipairs(args) do
		table.insert(t, s)
	end
end

---============================================================
-- Round to precision
---============================================================
function round(num, idp)
	local pow = idp or 0
	if idp then
		local mult = 10^pow
		local adder = (num >= 0 and 0.5) or -0.5
		return math.floor(num * mult + adder) / mult
	end
	return math.floor(num + 0.5)
end

---============================================================
-- arrotonda in base all'ordine di grandezza: 11111->11110, 1111->1111, 111->111,1
---============================================================
local function round_auto(num)
	local  base = - math.floor(math.log10(math.abs(num))) + 3 
	if base < -3 then base = -3 end
	return round(num, base)
end

---============================================================
-- format value using non breaking spaces to separate three digit group
--============================================================
local function sep_thousand(value)
	local formatted = value
	while true do
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1&nbsp;%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

---============================================================
-- Convert string to number ("," are considered as ".")
---============================================================
local function get_value(txt)
	local s = txt
	if s then
		s = s:gsub("%.(%d%d%d)", "%1" )
		local try_convert = s:gsub(",", ".")
		return tonumber(try_convert)
	end
end

---============================================================
-- Format a number as string replacin "." with "," and adding
-- no breaking space as separator between thousands 
---============================================================
local function format_value(value)
	if value then
		local s = tostring(value)
		s = s:gsub("%.", ",")
		return sep_thousand(s)
	end
end

---============================================================
-- Return formatted error message and set error_flag to true
---============================================================
local function add_error(ignore_error, msg) 
	if ignore_error then return '' end
	error_flag = true
	return '<sup class="error" style="font-size:small;">' .. msg .. '</sup>'
end

local function format_m(m1_raw, m2_raw, factor, ignore_error)
	if not (m1_raw or m2_raw) then
		return
	end	
	local m1_value = get_value(m1_raw)
	local m2_value = get_value(m2_raw)
	if m1_value and not m2_raw then
		m2_value = round_auto(m1_value * factor)
	end
	if m2_value and not m1_raw then
		m1_value = round_auto(m2_value / factor)
	end
	local m1_formatted, m2_formatted
	if m1_value then
		m1_formatted = format_value(tostring(m1_value))
	elseif m1_raw then
		m1_formatted = m1_raw ..  add_error(ignore_error, "Richiesto un numero")
	end
	if m2_value then
		m2_formatted = format_value(tostring(m2_value))
	elseif m2_raw then
		m2_formatted = m2_raw .. add_error(ignore_error, "Richiesto un numero")
	end
	return m1_formatted, m2_formatted 
end

---============================================================
-- Format speed and convert from knots to km/h (or viceversa)
---============================================================
function p.speed(frame)
	
	local args = getArgs(frame, {frameOnly = true})
	local spd_kn = args[1]
	local spd_kmh = args[2]
	local spd_type = args[3]
	if spd_kmh == nil and spd_kn == nil then return '' end
	kn_formatted, kmh_formatted = format_m(spd_kn, spd_kmh, 1.852, true)
	local out = {}
	if spd_type then dump(out, spd_type, ":&nbsp;") end
	if kn_formatted then dump(out, kn_formatted, "&nbsp;[[Nodo (unità di misura)|nodi]]") end
	if kmh_formatted then
		if kn_formatted then dump(out, " (") end
		dump(out, kmh_formatted, "&nbsp;[[Chilometro orario|km/h]]")
		if kn_formatted then dump(out, ")") end
	end
	return table.concat(out)
end

---============================================================
-- Format range data (knots to km/h and nmi to km, or viceversa)
---============================================================
function p.range(frame)
	local args = getArgs(frame)
	local range_nmi = args[1] 
	local range_kn = args[2] 
	local range_type = args[3]
	local range_km = args[4] 
	local range_kmh = args[5]
	local out = {}
	local nmi_formatted, km_formatted = format_m(range_nmi, range_km, 1.852)
	local kn_formatted, kmh_formatted = format_m(range_kn, range_kmh, 1.852)
	if range_type then dump(out, range_type, ":") end
	if nmi_formatted then
		dump(out, nmi_formatted, "&nbsp;[[Miglio nautico|miglia]]" )
		if kn_formatted then
			dump(out, " a ", kn_formatted, "&nbsp;[[nodo (unità di misura)|nodi]]")
		end
	end 
	if km_formatted then
		if nmi_formatted then dump(out, " (") end
		dump(out, km_formatted, "&nbsp;[[chilometro|km]]" )
		if kmh_formatted then
			dump(out, " a ", kmh_formatted, "&nbsp;[[chilometro orario|km/h]]")
		end
		if nmi_formatted then dump (out, ")") end
	end
	-- add category error if necessary
	if error_flag then
		local current_page = mw.title.getCurrentTitle()
		if current_page.namespace == 0 then
			dump(out, "[[Categoria:Errori di compilazione del template Infobox nave]]")
		end
	end
	return table.concat(out)
end

---============================================================
-- Format a generic list of measure
---============================================================
function p.measure(frame)
	local args = getArgs(frame, {parentOnly = true})
	local parameters = getArgs(frame, {frameOnly = true})
	
	local base_arg = parameters[1]
	local base_type = parameters[2] or ''
	local um = parameters[3]
	um = (um and ('&nbsp;' .. um)) or ''
	if not base_arg then return end
	local elements = {}
	local value_txt = args[base_arg]
	if not value_txt then return end
	local value = get_value(value_txt)
	if value then value_txt = format_value(tostring(value)) end
	local value_type = args[base_type]
	value_type = (value_type and (value_type .. ": ")) or ''
	elements[1] = value_type .. value_txt .. um
	local n = 2
	while true do
		local nchar = tostring(n)
		value_txt = args[base_arg .. nchar]
		if value_txt then
			value = get_value(value_txt)
			if value then value_txt = format_value(tostring(value)) end
			value_type = args[base_type .. nchar]
			value_type = (value_type and (value_type .. ": ")) or ''
			elements[#elements+1] = value_type .. value_txt .. um
			n = n + 1
		else
			break 
		end
	end
	if #elements == 1 then
		return elements[1]
	else
		return '<UL><LI>' .. mw.text.listToText(elements, '</LI>\n<LI>', '</LI>\n<LI>') .. '</LI>\n</UL>'
	end
end
	
return p