Mô đun:Weather box

-- Implement Template:Weather box.

local precision = require('Module:Math')._precision

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, is_first, base, what)
local first
if isAny(args, ' ' .. what .. ' cm') then
first = 'cm'
else
if isAny(args, ' ' .. what .. ' mm') then
first = 'mm'
else
first = (what == 'snow' or
precision(args['Jan ' .. what .. ' inch'] or '0') (≥ ' .. _ifset('unit precipitation days', ) .. ')'),
annual_mode = 'sum',
},
{---------- RAINY DAYS ----------
WANTROW = _ifany(' rain days'),
mode = 'basic',
group_name = 'rain days',
color_scheme = _ifset('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Số ngày mưa trung bình' .. _if('unit rain days', ' (≥ ' .. _ifset('unit rain days', ) .. ')'),
annual_mode = 'sum',
},
{---------- SNOWY DAYS ----------
WANTROW = _ifany(' snow days'),
mode = 'basic',
group_name = 'snow days',
color_scheme = _ifset('precip days colour', 'd'),
date_mode = true,
scale_factor = '1',
label = 'Số ngày tuyết rơi trung bình' .. _if('unit snow days', ' (≥ ' .. _ifset('unit snow days', ) .. ')'),
annual_mode = 'sum',
},
{---------- PERCENT RELATIVE HUMIDITY ----------
WANTROW = _ifany(' humidity'),
mode = 'basic',
group_name = 'humidity',
color_scheme = _ifset('humidity colour', 'h'),
scale_factor = '1',
label = 'Độ ẩm tương đối trung bình (%)' ..
_if('time day', ' (at ' .. _ifset('time day', ) .. ')') ..
_if('daily', ' (daily average)'),
annual_mode = 'avg',
},
{---------- AFTERNOON PERCENT RELATIVE HUMIDITY ----------
WANTROW = _ifany(' afthumidity'),
mode = 'basic',
group_name = 'afthumidity',
color_scheme = _ifset('humidity colour', 'h'),
scale_factor = '1',
label = 'Độ ẩm tương đối trung bình buổi chiều (%)' ..
_if('time day', ' (at ' .. _ifset('time day', ) .. ')') ..
_if('daily', ' (daily average)'),
annual_mode = 'avg',
},
{---------- FIRST LINE AVERAGE DEW POINT ----------
WANTROW = _ifany(' dew point C') or _ifany(' dew point F'),
mode = 'temperature',
group_name = 'dew point',
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Điểm sương trung bình °' .. _if('metric first', 'C', 'F') .. _if('single line', ' (°' .. _if('metric first', 'F', 'C') .. ')'),
annual_mode = 'avg',
},
{---------- SECOND LINE AVERAGE DEW POINT----------
WANTROW = not _ifset('single line') and (_ifany(' dew point C') or _ifany(' dew point F')),
mode = 'temperature',
group_name = 'dew point',
second_line = true,
color_scheme = _ifset('temperature colour', 't'),
scale_factor = '1',
label = 'Điểm sương trung bình °' .. _if('metric first', 'F', 'C'),
show = _if('metric first', '2', '1'),
annual_mode = 'avg',
},
{---------- MONTHLY SUNSHINE HOURS ----------
WANTROW = _ifany(' sun'),
mode = 'basic',
group_name = 'sun',
color_scheme = _ifset('sun colour', 's'),
date_mode = true,
scale_factor = '1',
label = 'Số giờ nắng trung bình tháng',
annual_mode = 'sum',
},
{---------- DAILY SUNSHINE HOURS ----------
WANTROW = _ifany('d sun'),
mode = 'basic',
group_name = 'd sun',
color_scheme = _ifset('sun colour', 's'),
include_space = false,
scale_factor = '30.44',
label = 'Số giờ nắng trung bình ngày',
annual_mode = 'avg',
},
{---------- DAILY DAYLIGHT HOURS ----------
WANTROW = _ifany(' light'),
mode = 'basic',
group_name = ' light',
color_scheme = _ifset('sun colour', 's'),
include_space = false,
scale_factor = '30.44',
label = 'Số giờ ban ngày trung bình hàng ngày',
annual_mode = 'avg',
},
{---------- PERCENT SUNSHINE ----------
WANTROW = _ifany(' percentsun'),
mode = 'basic',
group_name = 'percentsun',
color_scheme = _ifset('sun colour', 's'),
scale_factor = '7.2',
label = 'Phần trăm nắng có thể',
annual_mode = 'avg',
},
{---------- ULTRAVIOLET INDEX ----------
WANTROW = _ifany(' uv'),
mode = 'basic',
group_name = 'uv',
color_scheme = _ifset('uv colour', 'u'),
scale_factor = '1',
label = 'Chỉ số tia cực tím trung bình',
annual_mode = 'avg',
},
----------- SOURCES ----------
makeSources(frame, args),
}
end

local function makeFrame(self_args, parent_args)
-- Kludge to pass arguments for a single row to buildRow.
-- Later: Refactor buildRow so this is not needed.
return {
args = self_args,
getParent = function (self) return makeFrame(parent_args, nil) end,
}
end

local function main(frame)
local buildRow = require('Module:Weather box/row').buildRow
local args = frame:getParent().args
local results = {}
for i, def in ipairs(getDefinitions(frame, args)) do
local row
if type(def) == 'string' then
row = def
elseif def.WANTROW then
row = buildRow(makeFrame(def, args))
else
row =
end
results[i] = row
end
return '\n'..table.concat(results)..'\n' -- prevent Scribunto from inserting a blank line before the table
end

return {
main = main,
}