Content deleted Content added
update from sandbox: use data-sort-value for sort keys per Module talk:Age#Sorting will use data-sort-value |
update from Module:Age/sandbox for df parameter value check, per talk |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 3:
local mtext = {
-- Message and other text that should be localized.
-- Also need to localize text in table names in function dateDifference.
['mt-bad-param2'] = 'Parameter $1=$2 is invalid',
['mt-bad-show'] = 'Parameter show=$1 is not supported here',
Line 25:
['mt-template-bad-name'] = 'The specified template name is not valid',
['mt-template-x'] = 'The template invoking this must have "|template=x" where x is the wanted operation',
['
['
['txt-affirmative'] = { y = true, yes = true, Y = true, Yes = true, YES = true }, -- valid values for df + mf parameters
['txt-yes'] = { y = true, yes = true, on = true }, -- valid values for parameters introduced with this module
['txt-and'] = ' and ',
['txt-or'] = ' or ',
['txt-category'] = 'Category:Age error',
['txt-comma-and'] = ', and ',
['txt-error'] = 'Error: ',
['txt-
['txt-module-convertnumeric'] = 'Module:ConvertNumeric',
['txt-module-date'] = 'Module:Date',
['txt-sandbox'] = 'sandbox',
['txt-bda'] = '<span style="display:none"> (<span class="bday">$1</span>) </span>$2<span class="noprint ForceAgeToShow"> (age $3)</span>',
['txt-dda'] = '$2<span style="display:none">($1)</span> (aged $3)',
['txt-bda-disp'] = 'disp_raw', -- disp_raw → age is a number only; disp_age → age is a number and unit (normally years but months or days if very young)
['txt-dda-disp'] = 'disp_raw',
['txt-dmy'] = '%-d %B %-Y',
['txt-mdy'] = '%B %-d, %-Y',
}
local isWarning = {
['mt-warn-param1'] = true,
['mt-warn-param2'] = true,
}
-- yes[parameter] is true if parameter should be interpreted as "yes".
-- Do not want to accept mixed upper/lowercase unless done by previously used templates.
-- Need to accept "on" because "round=on" is wanted.
local yes = mtext['txt-yes']
local translate, from_en, to_en, isZero
Line 50 ⟶ 73:
-- Return objects exported from the date module or its sandbox.
if not _Date then
local sandbox = frame:getTitle():find(mtext['txt-sandbox'], 1, true) and ('/' .. mtext['txt-sandbox']) or ''
local datemod = require(mtext['
local realDate = datemod._Date
_currentDate = datemod._current
Line 104 ⟶ 127:
end
local function
-- Return
return mw.message.newRawMessage(text, ...):plain()
end
local function message(msg,
-- Return formatted message text for an error or warning.
local function getText(msg)
return mtext[msg] or error('Bug: message "' .. tostring(msg) .. '" not defined')
end
local categories = {
error = mtext['txt-category'
warning = mtext['txt-category'],
}
local a, b, k, category
local text = substituteParameters(getText(msg), ...)
if isWarning[msg] then
a = '<sup>[<i>'
b = '</i>]</sup>'
k = 'warning'
else
a = '<strong class="error">' .. getText('txt-error')
b = '</strong>'
k = 'error'
end
if mw.title.getCurrentTitle():inNamespaces(0) then
-- Category only in namespaces: 0=article.
category = '[[' .. categories[
end
return
Line 150 ⟶ 161:
b ..
(category or '')
end
local function dateFormat(args)
-- Return
-- nil, f if parameter is valid
-- m, f otherwise
-- where
-- m = string for warning message with category
-- f = string for wanted date format
local problem
local wanted = mtext['txt-format-default']
local other = wanted == 'df' and 'mf' or 'df'
local parm = args[other] or ''
if mtext['txt-affirmative'][parm] then
wanted = other
elseif parm ~= '' then
problem = message('mt-warn-param2', other, parm)
end
return problem, wanted == 'df' and mtext['txt-dmy'] or mtext['txt-mdy']
end
Line 178 ⟶ 208:
-- i == 1 for the first number which can optionally start with an uppercase letter.
number = tostring(number)
return require(mtext['
number,
nil, -- fraction numerator
Line 230 ⟶ 260:
result = '<span data-sort-value="_SORTKEY_♠"></span>'
end
return (result:gsub('_SORTKEY_', sortKey))
end
end
Line 276 ⟶ 306:
hm = { 'H', 'M', id = 'hm' },
hms = { 'H', 'M', 'S', id = 'hms' },
M = { 'M', id = 'M' },
s = { 'S', id = 's' },
d = { 'd', id = 'd' },
dh = { 'd', 'H', id = 'dh' },
Line 312 ⟶ 344:
parms[i] = v
end
if yes
table.insert(parms, 'fix')
end
if yes
table.insert(parms, 'partial')
end
Line 331 ⟶ 363:
date = date + item
if not date then
return message(
end
end
Line 388 ⟶ 420:
local name = names[components[i]]
if name then
if type(name) == 'table' then
end
text:add(vstr .. sep .. name
else
text:add(vstr)
Line 440 ⟶ 471:
-- which have been validated.
local names = {
-- Each name is:
-- * a string if no plural form of the name is used; or
-- * a table of strings, one of which is selected using the rules at
-- https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
abbr_off = {
sep = ' ',
y = {'year', 'years'},
m = {'month', 'months'},
w = {'week', 'weeks'},
d = {'day', 'days'},
H = {'hour', 'hours'},
M = {'minute', 'minutes'},
S = {'second', 'seconds'},
},
abbr_on = {
Line 461 ⟶ 495:
},
abbr_infant = { -- for {{age for infant}}
sep = ' ',
y = {'yr', 'yrs'},
m = {'mo', 'mos'},
w = {'wk', 'wks'},
d = {'day', 'days'},
H = {'hr', 'hrs'},
M = {'min', 'mins'},
S = {'sec', 'secs'},
},
abbr_raw = {},
Line 550 ⟶ 583:
(textOptions.suffix or '')
end
return message(
end
Line 624 ⟶ 657:
for i = 1, nrDates do
local index = i == 1 and 1 or 4
if (m == 2 or m == '2') and (d == 29 or d == '29') then
-- Workaround error with following which attempt to use invalid date 2001-02-29.
-- {{age_ymwd|year1=2001|year2=2004|month2=2|day2=29}}
-- {{age_ymwd|year1=2001|month1=2|year2=2004|month2=1|day2=29}}
-- TODO Get rid of wantMixture because even this ugly code does not handle
-- 'Feb' or 'February' or 'feb' or 'february'.
if not ((y % 4 == 0 and y % 100 ~= 0) or y % 400 == 0) then
d = 28
end
end
dates[i] = Date(y, m, d)
end
else
Line 698 ⟶ 742:
flag = 'usesCurrent',
omitZero = true,
range = '
},
age_full_years_nts = { -- {{age nts}}
Line 722 ⟶ 766:
age_infant = { -- {{age for infant}}
-- Do not set show because special processing is done later.
abbr = yes
disp = 'disp_age',
sep = 'sep_space',
Line 785 ⟶ 829:
-- ("on" is equivalent to "yes", and "off" is equivalent to "no").
-- "|range=OTHER" sets range = nil and rejects partial dates.
range = ({ dash = 'dash', off = 'no', no = 'no', [true] = true })[range] or yes
if range then
partial = true -- accept partial dates with a possible age range for the result
Line 795 ⟶ 839:
end
local getopt = {
fix = yes
flag = stripToNil(args.flag) or spec.flag,
omitZero = spec.omitZero,
Line 814 ⟶ 858:
local parms = {
diff = date2:subtract(date1, { fill = autofill }),
wantDuration = spec.duration or yes
range = range,
wantSc = yes
show = args.show == 'hide' and 'hide' or spec.show,
abbr = spec.abbr,
Line 822 ⟶ 866:
extra = makeExtra(args, getopt.usesCurrent and format ~= 'format_raw'),
format = format or spec.format,
round = yes
sep = spec.sep,
sortable = translateParameters.sortable[args.sortable or spec.sortable],
Line 831 ⟶ 875:
end
return from_en(dateDifference(parms))
end
local function isFake(args)
-- Some templates have TemplateData with an auto value like "{{Birth date and age|YYYY|MM|DD}}".
-- Return true if that appears to be the case so the caller can output nothing rather than an error.
return args[1] == 'YYYY'
end
Line 836 ⟶ 886:
-- Implement [[Template:Birth date and age]].
local args = frame:getParent().args
if isFake(args) then
return ''
end
local options = {
missing1 = 'mt-need-valid-bd',
Line 850 ⟶ 903:
return message('mt-invalid-bd-age')
end
local disp
local show = 'y'
if diff.years < 2 then
disp = 'disp_age'
Line 859 ⟶ 913:
end
end
local problem, format = dateFormat(args)
local result = substituteParameters(
mtext['txt-bda'],
date:text('%-Y-%m-%d'),
from_en(date:text(
from_en(dateDifference({
diff = diff,
show = show,
Line 872 ⟶ 924:
disp = disp,
sep = 'sep_space',
})
) .. (problem or '')
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 902 ⟶ 954:
end
if invalid then
result = result .. message(
end
end
Line 911 ⟶ 963:
-- Implement [[Template:Death date and age]].
local args = frame:getParent().args
if isFake(args) then
return ''
end
local options = {
missing1 = 'mt-need-valid-dd',
Line 940 ⟶ 995:
return message('mt-invalid-dates-age')
end
local fmt_date, fmt_ymd, problem
if date1.day then -- y, m, d known
problem, fmt_date = dateFormat(args)
elseif date1.month then -- y, m known; d unknown
fmt_date = '%B %-Y'
else -- y known; m, d unknown
end
local sortKey
local sortable = translateParameters.sortable[args.sortable]
if sortable then
local value = (date1.partial and date1.partial.first or date1).jdz
sortKey = makeSort(value, sortable)
end
local result = (sortKey or '') .. substituteParameters(
mtext['txt-dda'],
date1:text(fmt_ymd),
from_en(date1:text(fmt_date)),
from_en(dateDifference({
diff = diff,
show = 'y',
abbr = 'abbr_off',
disp = mtext['
range = 'dash',
sep = 'sep_space',
})
) .. (problem or '')
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 989 ⟶ 1,047:
end
if invalid then
result = result .. message(
end
end
Line 1,042 ⟶ 1,100:
local parms = {
extra = makeExtra(args),
wantDuration = yes
range = yes
wantSc = yes
}
local fix = yes
local date1 = Date(fix, 'partial', stripToNil(args[1]) or 'currentdatetime')
if not date1 then
Line 1,061 ⟶ 1,119:
parm = translate[parm]
if parm == nil then -- test for nil because false is a valid setting
return message(
end
parms[argname] = parm
Line 1,072 ⟶ 1,130:
if show then
if show.id ~= round then
return message(
end
else
Line 1,081 ⟶ 1,139:
end
return from_en(dateDifference(parms))
end
local function templateGeneric(frame)
local name = frame.args.template
if not name then
return message('mt-template-x')
end
return ageGeneric(frame:newChild{title = mw.title.new(name, 10), args = frame.args})
end
Line 1,092 ⟶ 1,158:
JULIANDAY = dateToJd, -- Template:JULIANDAY
time_interval = timeInterval, -- Template:Time_interval
[''] = templateGeneric, -- same as age_generic, but can be invoked directly
}
|