Changes

Jump to: navigation, search

Module:Dts

2,018 bytes added, 20:18, 5 July 2015
allow years up to plus/minus one trillion, and add a tracking category for deprecated parameters
local yesno = require('Module:Yesno')
local lang = mw.language.getContentLanguage()
local N_YEAR_DIGITS = 12
local MAX_YEAR = 10^N_YEAR_DIGITS - 1
--------------------------------------------------------------------------------
Dts.monthSearch = Dts._makeMonthSearch(Dts.months)
Dts.monthSearchAbbr = Dts._makeMonthSearch(Dts.monthsAbbr)
Dts.monthSearchAbbr['sept'] = 9 -- Allow "Sept" to match September
Dts.formats = {
elseif args[1] then
self:parseDate(args[1])
else
error('no date parameters detected', 3)
end
if self.year then
if self.year == 0 then
error('years cannot be zero', 30) elseif self.year < -MAX_YEAR then error(string.format( 'years cannot be less than %s', lang:formatNum(-MAX_YEAR) ), 0) elseif self.year > MAX_YEAR then error(string.format( 'years cannot be greater than %s', lang:formatNum(MAX_YEAR) ), 0)
elseif math.floor(self.year) ~= self.year then
error('years must be an integer', 30)
end
end
or math.floor(self.month) ~= self.month
) then
error('months must be an integer between 1 and 12', 30)
end
if self.day and (
or math.floor(self.day) ~= self.day
) then
error('days must be an integer between 1 and 31', 30)
end
"'%s' is not a valid format",
tostring(self.format)
), 30)
end
math.floor(self.addkey) ~= self.addkey
then
error("the 'addkey' parameter must be an integer between 0 and 9999", 30)
end
end
-- Set whether the displayed date is allowed to wrap or not.
self.isWrapping = args.nowrap == 'off' or yesno(args.nowrap) == false
 
-- Check for deprecated parameters.
if args.link then
self.hasDeprecatedParameters = true
end
return self
end
 
function Dts:hasDate()
return (self.year or self.month or self.day) ~= nil
end
end
-- Parses separate parameters for year, month, day, and era.
function Dts:parseDateParts(year, month, day, bc)
if year then
"'%s' is not a valid year",
tostring(year)
), 30)
end
end
"'%s' is not a valid month",
tostring(month)
), 30)
end
end
"'%s' is not a valid day",
tostring(day)
), 30)
end
end
if bc then
local bc bcLower = type(bc) == 'string' and bc:lower() if bc bcLower == 'bc' or bc bcLower == 'bce' then
if self.year and self.year > 0 then
self.year = -self.year
end
elseif bc bcLower ~= 'ad' or bc and bcLower ~= 'ce' then
error(string.format(
"'%s' is not a valid era code (expected 'BC', 'BCE', 'AD' or 'CE')",
tostring(bc)
), 30)
end
end
"'%s' is an invalid date",
date
), 50)
end
-- Deal with year-only dates first, as they can have hyphens in, and later
-- we need to split the string by all non-word characters, including
-- hyphens. Also, we don't need to restrict years to four 3 or 4 digits, as on
-- their own they can't be confused as a day or a month number.
self.year = tonumber(date)
"an unknown error occurred while parsing the date '%s'",
date
), 40)
end
-- Month DD, YYYY
-- YYYY-MM-DD
-- DD-MM-YYYY
self.month = self:parseMonthName(parts[1])
if self.month then
self.day = parseDayOrMonth(parts[1])
if self.day then
-- This is DD Month YYYY.
self.format = 'dmy'
self.month = self:parseMonthName(parts[2])
if self.month then -- This is DD Month YYYY. self.format = 'dmy' self.year = parseYear(parts[3]) if not self.year then dateError() end else -- This is DD-MM-YYYY. self.format = 'dmy' self.month = parseDayOrMonth(parts[2]) self.year = parseYear(parts[3]) if not self.month or not self.year then dateError() end
end
else
function Dts:makeSortKey()
local year, month, day local nYearDigits = N_YEAR_DIGITS if self:hasDate() then year = self.year or os.date("*t").year if year < 0 then year = -MAX_YEAR - 1 - year > 0 and year nYearDigits = nYearDigits + 1 -- For the minus sign end month = self.month or 1 day = self.day or 1 else -10000 - Blank {{dts}} transclusions should sort last. year= MAX_YEAR month = 99 day = 99 end
return string.format(
"'%05d0' .. nYearDigits .. 'd-%02d-%02d-%04d"', year, self.month or 1, self.day or 1, self.addkey or 0
)
end
ret[#ret + 1] = ' '
end
ret[#ret + 1] local displayYear = math.abs(self.year) if displayYear > 9999 then displayYear = lang:formatNum(displayYear) else displayYear = tostring(displayYear) end ret[#ret + 1] = displayYear
if self.year < 0 then
ret[#ret + 1] = '&nbsp;BC'
end
return table.concat(ret)
end
 
function Dts:renderTrackingCategories()
if self.hasDeprecatedParameters then
return '[[Category:Dts templates with deprecated parameters]]'
else
return ''
end
end
-- Display
if self:hasDate() then if self.isWrapping then root:wikitext(self:makeDisplay()) else root:tag('span') :css('white-space', 'nowrap') :wikitext(self:makeDisplay()) end
end
 
-- Tracking categories
root:wikitext(self:renderTrackingCategories())
return tostring(root)
function p._main(args)
local success, ret = pcall(function () local dts = Dts.new(args) return tostring(dts) end) if success then return ret else ret = string.format( '<strong class="error">Error in [[Template:Dts]]: %s</strong>', ret ) if mw.title.getCurrentTitle().namespace == 0 then -- Only categorise in the main namespace ret = ret .. '[[Category:Dts templates with errors]]' end return ret end
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Dts',
removeBlanks = false
})
return p._main(args)
Anonymous user

Navigation menu