Module:Age: Difference between revisions
update from sandbox with features discussed at Template talk:Age
m (Changed protection level for "Module:Age": that was weird ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))) |
(update from sandbox with features discussed at Template talk:Age) |
||
Line 312: | Line 312: | ||
return | return | ||
(options.prefix or '') .. | (options.prefix or '') .. | ||
(options.extra or '') .. | |||
sign .. | sign .. | ||
text:join() .. | text:join() .. | ||
Line 401: | Line 402: | ||
prefix = prefix, | prefix = prefix, | ||
suffix = parms.suffix, -- not currently used | suffix = parms.suffix, -- not currently used | ||
extra = parms.extra, | |||
format = parms.format, | format = parms.format, | ||
join = parms.sep or defaultJoin, | join = parms.sep or defaultJoin, | ||
Line 443: | Line 445: | ||
local Date, currentDate = getExports(frame) | local Date, currentDate = getExports(frame) | ||
getopt = getopt or {} | getopt = getopt or {} | ||
local function flagCurrent(text) | |||
-- This allows the calling template to detect if the current date has been used, | |||
-- that is, whether both dates have been entered in a template expecting two. | |||
-- For example, an infobox may want the age when an event occurred, not the current age. | |||
-- Don't bother detecting if wantMixture is used because not needed and it is a poor option. | |||
if not text then | |||
text = 'currentdate' | |||
if getopt.flag == 'usesCurrent' then | |||
getopt.usesCurrent = true | |||
end | |||
end | |||
return text | |||
end | |||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local fields = {} | local fields = {} | ||
Line 465: | Line 480: | ||
if fields[i] then | if fields[i] then | ||
imax = i | imax = i | ||
end | |||
if getopt.omitZero and i % 3 ~= 1 then -- omit zero months and days as unknown values but keep year 0 which is 1 BCE | |||
if tonumber(fields[i]) == 0 then | |||
fields[i] = nil | |||
getopt.partial = true | |||
end | |||
end | end | ||
end | end | ||
Line 495: | Line 516: | ||
dates[i] = Date(fix, partialText, y, m, d) | dates[i] = Date(fix, partialText, y, m, d) | ||
elseif not y and not m and not d and not noDefault then | elseif not y and not m and not d and not noDefault then | ||
dates[i] = Date( | dates[i] = Date(flagCurrent()) | ||
end | end | ||
end | end | ||
Line 501: | Line 522: | ||
elseif not noDefault then | elseif not noDefault then | ||
getopt.textdates = true -- have parsed each date from a single text field | getopt.textdates = true -- have parsed each date from a single text field | ||
dates[1] = Date(fix, partialText, fields[1] | dates[1] = Date(fix, partialText, flagCurrent(fields[1])) | ||
if not getopt.single then | if not getopt.single then | ||
dates[2] = Date(fix, partialText, fields[2] | dates[2] = Date(fix, partialText, flagCurrent(fields[2])) | ||
end | end | ||
end | end | ||
Line 553: | Line 574: | ||
show = 'y', | show = 'y', | ||
abbr = 'abbr_raw', | abbr = 'abbr_raw', | ||
flag = 'usesCurrent', | |||
omitZero = true, | |||
range = 'no', | |||
}, | }, | ||
age_full_years_nts = { -- {{age nts}} | age_full_years_nts = { -- {{age nts}} | ||
Line 650: | Line 674: | ||
local getopt = { | local getopt = { | ||
fix = yes(args.fix), | fix = yes(args.fix), | ||
flag = stripToNil(args.flag) or spec.flag, | |||
omitZero = spec.omitZero, | |||
partial = partial, | partial = partial, | ||
wantMixture = spec.wantMixture, | wantMixture = spec.wantMixture, | ||
Line 671: | Line 697: | ||
abbr = spec.abbr, | abbr = spec.abbr, | ||
disp = spec.disp, | disp = spec.disp, | ||
extra = (getopt.usesCurrent and format ~= 'format_raw') and '<span class="currentage"></span>' or nil, | |||
format = format or spec.format, | format = format or spec.format, | ||
round = yes(args.round), | round = yes(args.round), | ||
Line 726: | Line 753: | ||
df = true, | df = true, | ||
mf = true, | mf = true, | ||
day = true, | day = true, | ||
day1 = true, | day1 = true, |