Modulo:Coord: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
+minuti e secondi come stringhe vuote |
+paramsEmpty, migliore gestione errori, fix minori |
||
Riga 58:
-------------------------------------------------------------------------------
--
-------------------------------------------------------------------------------
-- Error handler per xpcall, formatta l'errore
-- nel namespace principale aggiunge una categoria di warning▼
if mw.title.getCurrentTitle().namespace == 0 then▼
cat = "[[Categoria:" .. cfg.categorie["warning"] .. "]]\n"
end
"([[Template:Coord|istruzioni]]):\n%s</span>", cat, msg:match(".+%d:%s(.+)$"))
end
-- Raccoglie più messaggi di errore in un'unica table prima di usare error()
local function dumpError(...)
local arg = {...}
Riga 72 ⟶ 85:
end
-- Con le richieste "dm" e "dms" verifica se ci sono
-- parametri lasciati vuoti in modo valido.
▲-- nel namespace principale aggiunge una categoria di warning
-- Ritorna il tipo di richiesta dopo la eventuale semplificazione.
▲local function getErrors()
local function paramsEmpty(reqFormat)
▲ local text = ""
-- {{coord|1|2||N|5|6||E}} valido
▲ if mw.title.getCurrentTitle().namespace == 0 then
table.remove(args, 7)
table.remove(args, 3)
reqFormat = "dm"
-- {{coord|1|2|3|N|5|6||E}} non valido
error("* lat e long hanno diversa precisione")
-- {{coord|1||3|N|5||7|E}} valido
-- {{coord|1|2|3|N|5||7|E}} non valido
error("* lat e long hanno diversa precisione")
end
if reqFormat == "dm" then
▲ text = text .. "<span style=\"color:red;\">Il template {{Coord}} ha riscontrato degli errori ([[Template:Coord|istruzioni]]):\n" ..
-- {{coord|1||N|4||E}} valido
table.concat(errorTable) .. "</span>"▼
table.remove(args, 5)
table.remove(args, 2)
reqFormat = "d"
-- {{coord|1|2|N|4||E}} non valido
error("* lat e long hanno diversa precisione")
end
end▼
return
end
-- Riconosce il tipo di richiesta ("dec", "d", "dm" o "dms") e
-- Ritorna il tipo di richiesta
local function paramsParse()
local reqFormat, prefix, num, str
Riga 104 ⟶ 139:
elseif posArgs == 6 or posArgs == 7 then
reqFormat = "dm"
▲ if args[2] == "" then args[2] = 0 end
▲ if args[5] == "" then args[5] = 0 end
elseif posArgs == 8 or posArgs == 9 then
reqFormat = "dms"
▲ if args[2] == "" then args[2] = 0 end
▲ if args[3] == "" then args[3] = 0 end
▲ if args[6] == "" then args[6] = 0 end
▲ if args[7] == "" then args[7] = 0 end
else
▲ return nil
end
-- valida i parametri che possono essere lasciati vuoti
reqFormat = paramsEmpty(reqFormat)
-- validazione parametri posizionali
Riga 135 ⟶ 164:
dumpError(prefix, " < ", param.min)
elseif num > param.max then
dumpError(prefix, " > ", param.max)
end
else
dumpError(prefix, " non è un numero")
end
-- valida un parametro di tipo stringa
elseif param.type == "string" then
if v ~= param.min and v ~= param.max then
Riga 148 ⟶ 177:
end
end
end
return reqFormat
end
Riga 203 ⟶ 236:
deg = round(self.deg * 60, 0)
min = deg % 60
deg = math.floor((deg - min) / 60)
elseif dmsFormat == "dms" then
deg = round(self.deg * 3600, 0)
Riga 209 ⟶ 242:
deg = math.floor((deg - sec) / 60)
min = deg % 60
deg = math.floor((deg - min) / 60) % 360
end
Riga 343 ⟶ 376:
local function coord()
local decLat, degLong, dmsLat, dmsLong
local reqFormat, dmsFormat, defaultFormat, geohackParams, ret
-- parsifica i parametri
▲ reqFormat = paramsParse()
if not ret
return reqFormat
end
if reqFormat == "dec" then
-- {{coord|
decLat = DecCoord:new(args[1], "N")
decLong = DecCoord:new(args[2], "E")
elseif reqFormat == "d" then
-- {{coord|
decLat = DecCoord:new(args[1], args[2])
decLong = DecCoord:new(args[3], args[4])
elseif reqFormat == "dm" then
-- {{coord|
dmsLat = DmsCoord:new(args[1], args[2], nil, args[3])
dmsLong = DmsCoord:new(args[4], args[5], nil, args[6])
elseif reqFormat == "dms" then
-- {{coord|
dmsLat = DmsCoord:new(args[1], args[2], args[3], args[4])
dmsLong = DmsCoord:new(args[5], args[6], args[7], args[8])
Riga 405 ⟶ 439:
local args = frame.args
-- {{dms2dec|N|
return DmsCoord:new(args[2], args[3], args[4], args[1]):toDec():getDeg()
end
Riga 413 ⟶ 447:
local args = frame.args
-- {{dec2dms|
return DecCoord:new(args[1], tonumber(args[1]) >= 0 and args[2] or args[3]):toDms(args[4])
end
|