Modulo:Coord: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
+minuti e secondi come stringhe vuote
+paramsEmpty, migliore gestione errori, fix minori
Riga 58:
 
-------------------------------------------------------------------------------
-- ValidazioneParsing parametri
-------------------------------------------------------------------------------
 
-- Error handler per xpcall, formatta l'errore
-- Aggiunge un messaggio di errore alla risposta come elenco puntato
-- nel namespace principale aggiunge una categoria di warning
local function getErrorserrhandler(msg)
local textcat = ""
 
if mw.title.getCurrentTitle().namespace == 0 then
cat = "[[Categoria:" .. cfg.categorie["warning"] .. "]]\n"
end
 
text = textreturn string.. format("%s<span style=\"color:red;\">Il template {{Coord}} ha riscontrato degli errori ([[Template:Coord|istruzioni]]):\n" ..
"([[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
-- Ritorna una stringa contenente la lista degli errori,
-- 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 = ""
if reqFormat == "dms" paramsParse()then
 
-- {{coord|1|2||N|5|6||E}} valido
if mw.title.getCurrentTitle().namespace == 0 then
textif args[3] == "[[Categoria:" ..and cfg.categorieargs["warning"7] ..== "]]\n" then
table.remove(args, 7)
table.remove(args, 3)
reqFormat = "dm"
-- {{coord|1|2|3|N|5|6||E}} non valido
ifelseif args[3] == "" thenor args[37] == 0"" endthen
error("* lat e long hanno diversa precisione")
-- {{coord|1||3|N|5||7|E}} valido
ifelseif args[2] == "" thenand args[26] == 0"" endthen
if args[2] == "" then, args[26] = 0, end0
-- {{coord|1|2|3|N|5||7|E}} non valido
ifelseif args[62] == "" thenor args[6] == 0"" endthen
error("* lat e long hanno diversa precisione")
return nilend
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>"
if args[52] == "" thenand args[5] == 0"" endthen
table.remove(args, 5)
table.remove(args, 2)
reqFormat = "d"
-- {{coord|1|2|N|4||E}} non valido
ifelseif args[72] == "" thenor args[75] == 0"" endthen
error("* lat e long hanno diversa precisione")
end
end
 
return textreqFormat
end
 
-- Riconosce il tipo di richiesta ("dec", "d", "dm" o "dms") e validaparsifica i parametri posizionali
-- Ritorna il tipo di richiesta o nil in caso di errore.
local function paramsParse()
local reqFormat, prefix, num, str
Riga 104 ⟶ 139:
elseif posArgs == 6 or posArgs == 7 then
reqFormat = "dm"
-- permette di lasciare il parametro minuti come stringa vuota
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"
-- permette di lasciare i parametri minuti e secondi come stringhe vuote
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
dumpErrorerror("Errato* errato numero di parametri")
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
 
returnif #errorTable ==> 0 and reqFormat or nilthen
error(table.concat(errorTable) .. "</span>")
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 notret, reqFormat then= xpcall(paramsParse, errhandler)
if not ret return getErrors()then
return reqFormat
end
if reqFormat == "dec" then
-- {{coord|431.6500111|-792.3800222}}
decLat = DecCoord:new(args[1], "N")
decLong = DecCoord:new(args[2], "E")
elseif reqFormat == "d" then
-- {{coord|431.651234111|N|793.383333333|W}}
decLat = DecCoord:new(args[1], args[2])
decLong = DecCoord:new(args[3], args[4])
elseif reqFormat == "dm" then
-- {{coord|431|292|N|794|235|W}}
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|431|292|43|N|795|236|07|W}}
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|102|203|354}}
return DmsCoord:new(args[2], args[3], args[4], args[1]):toDec():getDeg()
end
Riga 413 ⟶ 447:
local args = frame.args
-- {{dec2dms|101.391944111|N|S|d}}
return DecCoord:new(args[1], tonumber(args[1]) >= 0 and args[2] or args[3]):toDms(args[4])
end