मॉड्यूल:Weather box
दिखावट
इस मॉड्यूल हेतु प्रलेख मॉड्यूल:Weather box/doc पर बनाया जा सकता है
-- Implement [[Template:Weather box]].
local _precision = require('Module:Math')._precision
local function precision(text)
-- Input like 'Jan precipitation inch = trace' calls this with text = 'trace'
-- which would cause _precision to throw an error since it is not numeric.
-- Workaround: Return 0 as the precision if an error occurs.
local success, result = pcall(_precision, text)
if success then
return result
end
return 0
end
local function stripToNil(text)
-- If text is a non-empty string, return its trimmed content.
-- Otherwise, return nothing (text is an empty string or is not a string).
if type(text) == 'string' then
return text:match('(%S.-)%s*$')
end
end
local function isAny(args, suffix)
local months = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' }
for _, month in ipairs(months) do
if stripToNil(args[month .. suffix]) then
return true
end
end
end
local function makeLabel(args, options, is_first, base, what)
local first
if isAny(args, ' ' .. what .. ' cm') then
first = 'सेमी'
elseif isAny(args, ' ' .. what .. ' mm') then
first = 'मिमी'
else
first = (what:sub(1, 4) == 'snow' or
precision(args['Jan ' .. what .. ' inch'] or '0') < 1)
and 'सेमी'
or 'मिमी'
end
local second = 'इंच'
if not stripToNil(args['metric first']) then
first, second = second, first
end
if is_first then
if options.wantSingleLine then
first = first .. ' (' .. second .. ')'
end
else
first = second
end
return base .. ' ' .. first
end
local function makeSources(frame, args)
local source1 = stripToNil(args.source) or stripToNil(args['source 1'])
local source2 = stripToNil(args['source 2']) or stripToNil(args['source2'])
local result = '|-\n|colspan="14" style="text-align:center;font-size:95%;"|'
local text
if source1 or source2 then
if source1 and source2 then
text = 'स्रोत १: ' .. source1 .. '\n' .. result .. 'स्रोत २: ' .. source2
else
text = 'स्रोत: ' .. (source1 and source1 or source2)
end
else
text = frame:expandTemplate({ title = 'citation needed', args = {date = stripToNil(args.date)} })
end
return result .. text .. '\n|}'
end
local function wantSingle(parm)
parm = (parm or ''):lower()
return not (parm == 'no' or parm == 'n' or parm == '')
end
local function getDefinitions(frame, args, options)
local function _if(parm, a, b)
return stripToNil(args[parm]) and a or b or ''
end
local function _ifset(parm, a)
return stripToNil(args[parm]) and args[parm] or a
end
local function _ifany(suffix)
return isAny(args, suffix)
end
local function _ifSingle(a, b)
return options.wantSingleLine and a or b or ''
end
local function _colorscheme(what, default)
return stripToNil(args[what .. ' colour']) or stripToNil(args[what .. ' color']) or default
end
local function _margin()
if stripToNil(args.margin) then
return 'margin:' .. args.margin .. ';'
end
return ''
end
local location = _if('location', args.location, '{{{location}}}')
local navbarText
if not stripToNil(args.open) then
if stripToNil(args.name) then
local navbar = require('Module:Navbar')._navbar
navbarText = navbar({location .. ' के जलवायु आँकड़ा', args.name, collapsible=1})
end
end
return {
----------- HEADER ----------
'{| class="wikitable ' .. _if('open', '', 'mw-collapsible' .. _if('collapsed', ' mw-collapsed')) ..
'" style="width:' .. _ifset('width', 'auto') ..
'; text-align:center; line-height:1.2em;' ..
_margin() .. '"' ..
_if('open',
'\n|+' .. location .. ' के जलवायु आँकड़ा',
'\n|-' ..
'\n!colspan="14" | ' .. _if('name', navbarText, location .. ' के जलवायु आँकड़ा')
) ..
[=[
|-
!scope="row" |महीना
!scope="col" |जन
!scope="col" |फर
!scope="col" |मार्च
!scope="col" |अप्रै
!scope="col" |मई
!scope="col" |जून
!scope="col" |जुला
!scope="col" |अग
!scope="col" |सित
!scope="col" |अक्टू
!scope="col" |नवं
!scope="col" |दिस
!scope="col" style="border-left-width:medium" |साल
]=],
{---------- FIRST LINE RECORD HIGH TEMPERATURES ----------
WANTROW = _ifany(' record high C') or _ifany(' record high F'),
mode = 'temperature',
group_name = 'record high',
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'अधिकतम रिकार्ड °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'max',
},
{---------- FIRST LINE MEAN DAILY MAXIMUM TEMPERATURES ----------
WANTROW = _ifany(' high C') or _ifany(' high F'),
mode = 'temperature',
group_name = 'high',
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'औसत दैनिक अधिकतम °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- FIRST LINE DAILY MEAN TEMPERATURES ----------
WANTROW = _ifany(' mean C') or _ifany(' mean F'),
mode = 'temperature',
group_name = 'mean',
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'दैनिक औसत °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- FIRST LINE MEAN DAILY MINIMUM TEMPERATURES ----------
WANTROW = _ifany(' low C') or _ifany(' low F'),
mode = 'temperature',
group_name = 'low',
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'औसत दैनिक न्यूनतम °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- FIRST LINE RECORD LOW TEMPERATURES ----------
WANTROW = _ifany(' record low C') or _ifany(' record low F'),
mode = 'temperature',
group_name = 'record low',
color_scheme = _colorscheme('temperature', 't'),
scale_factor = '1',
label = 'न्यूनतम रिकार्ड °' .. _if('metric first', 'C', 'F') .. _ifSingle(' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'min',
},
{---------- FIRST LINE TOTAL PRECIPITATION ----------
WANTROW = _ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch'),
mode = 'precipitation',
group_name = 'precipitation',
color_scheme = _colorscheme('precipitation', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1,
label = makeLabel(args, options, true, 'औसत वर्षण', 'precipitation'),
annual_mode = 'sum',
},
{---------- SECOND LINE TOTAL PRECIPITATION ----------
WANTROW = not options.wantSingleLine and (_ifany(' precipitation cm') or _ifany(' precipitation mm') or _ifany(' precipitation inch')),
mode = 'precipitation',
group_name = 'precipitation',
second_line = true,
color_scheme = _colorscheme('precipitation', 'p'),
date_mode = true,
scale_factor = '1',
prefer_cm = precision(_ifset('Jan precipitation inch', '0')) < 1,
label = makeLabel(args, options, false, 'औसत वर्षण', 'precipitation'),
annual_mode = 'sum',
},
{---------- PRECIPITATION DAYS ----------
WANTROW = _ifany(' precipitation days'),
mode = 'basic',
group_name = 'precipitation days',
color_scheme = _colorscheme('precip days', 'd'),
date_mode = true,
scale_factor = '1',
label = 'औसत वर्षण दिन' .. _if('unit precipitation days', ' <span style="font-size:90%;" class="nowrap">(≥ ' .. _ifset('unit precipitation days', '') .. ')</span>'),
annual_mode = 'sum',
},
----------- SOURCES ----------
makeSources(frame, args),
}
end
local function main(frame)
local sandbox = frame:getTitle():find('sandbox', 1, true) and '/sandbox' or ''
local buildRow = require('Module:Weather box/row' .. sandbox)._buildRow
local args = frame:getParent().args
local options = {
wantSingleLine = wantSingle(args['single line']),
sandbox = sandbox,
}
local results = {}
for i, definition in ipairs(getDefinitions(frame, args, options)) do
local row
if type(definition) == 'string' then
row = definition
elseif definition.WANTROW then
row = buildRow(definition, args, options)
else
row = ''
end
results[i] = row
end
return '<div>\n'..table.concat(results)..'\n</div>'
end
return {
main = main,
}