Questa pagina è protetta dallo spostamento
Questa pagina è protetta

Modulo:RaDec

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca

Modulo che implementa i template {{RA}} e {{DEC}}.

Contiene inoltre funzioni di parsing del loro output per i moduli (parseRA e parseDEC), in modo da tenere allineato il formato nella generazione e nel parsing.


--[[
* Modulo che implementa i template RA e DEC, con inoltre funzioni di parsing del loro output,
* in modo da tenere allineato il formato nella generazione e nel parsing.
]]--

require('strict')

local getArgs = require('Modulo:Arguments').getArgs
local p = {}

-- Funzione di utilità per altri moduli:
-- parsifica il testo generato dal Template:RA (ascensione retta) e 
-- restituisce una table con chiavi 'h', 'm' e 's' di tipo number.
function p.parseRA(text)
	local h, m, s

	text = mw.text.trim(mw.text.unstrip(text)):gsub('−', '-'):gsub(',', '.')
	h, m, s = text:match('^(%d+)<sup>h</sup>&nbsp;(%d+)<sup>m</sup>&nbsp;([%d%.]+)<sup>s</sup>$')
	if not h then
		h, m = text:match('^(%d+)<sup>h</sup>&nbsp;(%d+)<sup>m</sup>&nbsp;:$')
		s = 0
	end
	if not h then
		h = text:match('^(%d+)<sup>h</sup>&nbsp;:$')
		m, s = 0, 0
	end

	h, m, s = tonumber(h), tonumber(m), tonumber(s)

	return (h and m and s) and { h = h, m = m, s = s } or nil
end

-- Funzione di utilità per altri moduli:
-- parsifica il testo generato dal Template:DEC (declinazione) e
-- restituisce una table con chiavi 'sign', 'd', 'm' e 's' di tipo number.
function p.parseDEC(text)
	local sign, d, m, s

	text = mw.text.trim(mw.text.unstrip(text)):gsub('&#43;', '+'):gsub('&#45;', '-'):gsub('−', '-'):gsub(',', '.')
	d, m, s = text:match('^([+-]?%d+)&deg;&nbsp;(%d+)&prime;&nbsp;([%d%.]+)&Prime;$')
	if not d then
		d, m = text:match('^([+-]?%d+)&deg;&nbsp;(%d+)&prime;&nbsp;:$')
		s = 0
	end
	if not d then
		d = text:match('^([+-]?%d+)&deg;&nbsp;:$')
		m, s = 0, 0
	end

	d, m, s = tonumber(d), tonumber(m), tonumber(s)
	if d then
		sign = (d < 0 or tostring(d) == '-0') and -1 or 1
		d = math.abs(d)
	end

	return (sign and d and m and s) and { sign = sign, d = d, m = m, s = s } or nil
end

-- Entry-point per il template {{RA}}
function p.RA(frame)
	local args = getArgs(frame, { parentOnly = true })
	local h, m, s = args[1] and (args[1] .. '<sup>h</sup>&nbsp;') or '', args[2] or ':', args[3] or ':'
	m = m .. (m == ':' and '' or '<sup>m</sup>&nbsp;')
	s = s .. (s == ':' and '' or '<sup>s</sup>')
	return h .. m .. (m == ':' and '' or s)
end

-- Entry-point per il template {{DEC}}
function p.DEC(frame)
	local args = getArgs(frame, { parentOnly = true })
	local d, m, s = args[1] and (args[1] .. '&deg;&nbsp;') or '', args[2] or ':', args[3] or ':'
	d = mw.text.encode(d, '+-')
	m = m .. (m == ':' and '' or '&prime;&nbsp;')
	s = s .. (s == ':' and '' or '&Prime;')
	return d .. m .. (m == ':' and '' or s)
end

return p