Content deleted Content added
convert to camelCase per dominant style |
validate parameters if warnings=1 in template; simplify message and generalize category; accept range=no with generic templates to help emulate inconsistent {{age in years nts}} |
||
Line 55:
end
local function message(msg,
-- Return formatted message text for an error or warning.
local categories = {
warning = '[[Category:Age warning]]',
local category▼
}
if not nocat and mw.title.getCurrentTitle():inNamespaces(0, 10) then▼
▲ local a, b, category
-- Category only in namespaces: 0=article, 10=template.▼
if id == 'warning' then
▲ category = '[[Category:Age error]]'
a = '<sup>[<i>'
b = '</i>]</sup>'
else
b = '</strong>'
end
return anchor ..▼
▲ '<strong class="error">Error: ' ..
category = categories[id or 'error']
end
a ..
mw.text.nowiki(msg) ..
(category or '')
end
local function
-- Return the given number formatted with commas as group separators,
-- given that the number is an integer.
Line 236 ⟶ 243:
if i == 1 and options.format == 'format_commas' then
-- Numbers after the first should be small and not need formatting.
fmt =
else
fmt = tostring
Line 407 ⟶ 414:
getopt = getopt or {}
local fix = getopt.fix and 'fix' or ''
local partial = getopt.
local args = frame:getParent().args
local fields = {}
Line 522 ⟶ 529:
abbr = 'abbr_raw',
negative = 'error',
partial = true,
range = 'dash',
},
Line 528 ⟶ 536:
abbr = 'abbr_raw',
negative = 'error',
partial = true,
range = 'dash',
format = 'format_commas',
Line 567 ⟶ 576:
age_ymd = { -- {{age in years, months and days}}
show = 'ymd',
partial = true,
range = true,
},
Line 587 ⟶ 597:
end
end
local range =
if range then
-- Suppose partial dates are used and age could be 11 or 12 years.
-- "|range=" has no effect (spec is used).
-- "|range=yes" sets range = true (gives "11 or 12")
-- "|range=dash" sets range = 'dash' (gives "11–12").
-- "|range=no" (or anything else) sets range = nil (gives "12").
range = yes(range) or (range == 'dash' and 'dash' or nil)
else
range = spec.range
end
local getopt = {
fix = yes(args.fix),
range = range,
partial = spec.partial or spec.range,
want_mixture = spec.want_mixture,
}
Line 625 ⟶ 646:
-- Implement [[Template:Birth date and age]].
local args = frame:getParent().args
local
local date = getDates(frame, options)
if type(date) == 'string' then
return date -- error text
end
local Date = getExports(frame)
local
if diff.isnegative or diff.years > 150 then
return message('Invalid birth date for calculating age')
Line 646 ⟶ 667:
local df = stripToNil(args.df) -- day first (dmy); default is month first (mdy)
local result = df and
'
'
result = '(<span class="bday">%-Y-%m-%d</span>) </span>' .. result
result = '<span style="display:none"> ' ..
date:text(result) ..
Line 660 ⟶ 682:
}) ..
')</span>'
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
local good = {
df = true,
mf = true,
template = true,
day = true,
day1 = true,
month = true,
month1 = true,
year = true,
year1 = true,
}
local invalid
local imax = options.textdates and 1 or 3
for k, _ in pairs(args) do
if type(k) == 'number' then
if k > imax then
invalid = tostring(k)
break
end
else
if not good[k] then
invalid = k
break
end
end
end
if invalid then
result = result .. message('invalid parameter ' .. invalid, 'warning')
end
end
return result
end
|