Difference between revisions of "Module:Dts"
From Timelines
m (typo fix) |
(remove unnecessary underscores) |
||
Line 14: | Line 14: | ||
function Dts.new(args) | function Dts.new(args) | ||
local self = setmetatable({}, Dts) | local self = setmetatable({}, Dts) | ||
− | self. | + | self.fmt = "mdy" |
− | self. | + | self.abbr = false --default |
if args[1] and (not args[2]) then | if args[1] and (not args[2]) then | ||
for _, val in pairs(mw.text.split(args[1],"[%s/-]")) do | for _, val in pairs(mw.text.split(args[1],"[%s/-]")) do | ||
− | self: | + | self:annonval(val, true) |
end | end | ||
else | else | ||
for key, val in pairs(args) do | for key, val in pairs(args) do | ||
if tonumber(key) then | if tonumber(key) then | ||
− | self: | + | self:annonval(val) |
end | end | ||
end | end | ||
end | end | ||
if args.format then | if args.format then | ||
− | self. | + | self.fmt = args.format |
end | end | ||
if args.abbr then | if args.abbr then | ||
if args.abbr == "on" then | if args.abbr == "on" then | ||
− | self. | + | self.abbr=true |
else | else | ||
− | self. | + | self.abbr=false |
end | end | ||
end | end | ||
− | if (self. | + | if (self.year==0) then --not valid. placeholder for no-year |
− | self. | + | self.year = nil |
end | end | ||
return self | return self | ||
end | end | ||
− | function Dts: | + | function Dts:setmonth(raw) |
if not raw then | if not raw then | ||
− | self. | + | self.month = nil |
return false | return false | ||
end | end | ||
local numbermonth = tonumber(raw) | local numbermonth = tonumber(raw) | ||
if numbermonth and numbermonth > 0 and numbermonth < 13 then | if numbermonth and numbermonth > 0 and numbermonth < 13 then | ||
− | self. | + | self.month = numbermonth |
return true | return true | ||
end | end | ||
for i, mon in pairs(self.monthsSearch) do | for i, mon in pairs(self.monthsSearch) do | ||
if string.find(string.lower(raw),mon) then | if string.find(string.lower(raw),mon) then | ||
− | self. | + | self.month=i |
if string.find(string.lower(raw),string.lower(self.months[i])) then | if string.find(string.lower(raw),string.lower(self.months[i])) then | ||
− | self. | + | self.abbr=false |
else | else | ||
− | self. | + | self.abbr=true |
end | end | ||
return true | return true | ||
Line 67: | Line 67: | ||
end | end | ||
− | function Dts: | + | function Dts:annonval(val, dayfirst) |
local numberval | local numberval | ||
if val then | if val then | ||
Line 77: | Line 77: | ||
if not numberval then | if not numberval then | ||
if mw.text.trim(string.lower(val)) == "bc" then | if mw.text.trim(string.lower(val)) == "bc" then | ||
− | if (not self. | + | if (not self.year) then |
− | self. | + | self.year = self.day |
− | self. | + | self.day = nil |
end | end | ||
− | if self. | + | if self.year then |
− | self. | + | self.year = 0 - self.year |
end | end | ||
else | else | ||
− | if self: | + | if self:setmonth(val,dayfirst) and dayfirst and self.year and (not self.day) and (self.year > 0) and (self.year<31) then |
− | self. | + | self.day = self.year |
− | self. | + | self.year = nil |
− | self. | + | self.fmt = "dmy" |
end | end | ||
end | end | ||
return | return | ||
end | end | ||
− | if self. | + | if self.month and (not self.day) and (numberval < 32) and (numberval > 0) then |
− | self. | + | self.day = numberval |
return | return | ||
end | end | ||
− | if self. | + | if self.year and (not self.month) then |
− | self. | + | self.month = numberval |
return | return | ||
end | end | ||
− | if (not self. | + | if (not self.year) then |
− | self. | + | self.year = numberval |
return | return | ||
end | end | ||
end | end | ||
− | function Dts: | + | function Dts:monthName() |
− | if (not self. | + | if (not self.month) or (self.month < 0) or (self.month > 12) then |
return "" | return "" | ||
end | end | ||
− | if self. | + | if self.abbr then |
− | return self.monthsAbr[self. | + | return self.monthsAbr[self.month] |
else | else | ||
− | return self.months[self. | + | return self.months[self.month] |
end | end | ||
end | end | ||
− | function Dts: | + | function Dts:sortkey() |
local root = mw.html.create() | local root = mw.html.create() | ||
Line 128: | Line 128: | ||
:css('speak', 'none') | :css('speak', 'none') | ||
local current = os.date("*t") | local current = os.date("*t") | ||
− | local year = self. | + | local year = self.year or current.year |
year = year > 0 and year or -10000 - year | year = year > 0 and year or -10000 - year | ||
sortSpan:wikitext(string.format( | sortSpan:wikitext(string.format( | ||
"%05d-%02d-%02d-%02d%02d", | "%05d-%02d-%02d-%02d%02d", | ||
year, | year, | ||
− | self. | + | self.month or 1, |
− | self. | + | self.day or 1, |
0, | 0, | ||
0 | 0 | ||
Line 143: | Line 143: | ||
function Dts:__tostring() | function Dts:__tostring() | ||
− | local root = self: | + | local root = self:sortkey() |
local displaySpan = root:tag('span') | local displaySpan = root:tag('span') | ||
displaySpan:css('white-space', 'nowrap') | displaySpan:css('white-space', 'nowrap') | ||
− | if self. | + | if self.day then |
− | if self. | + | if self.fmt == "mdy" then |
− | displaySpan:wikitext(self: | + | displaySpan:wikitext(self:monthName()) |
displaySpan:wikitext(' ') | displaySpan:wikitext(' ') | ||
− | displaySpan:wikitext(self. | + | displaySpan:wikitext(self.day) |
− | if self. | + | if self.year then |
displaySpan:wikitext(',') | displaySpan:wikitext(',') | ||
end | end | ||
else | else | ||
− | displaySpan:wikitext(self. | + | displaySpan:wikitext(self.day) |
displaySpan:wikitext(' ') | displaySpan:wikitext(' ') | ||
− | displaySpan:wikitext(self: | + | displaySpan:wikitext(self:monthName()) |
end | end | ||
else | else | ||
− | if self. | + | if self.month then |
− | displaySpan:wikitext(self: | + | displaySpan:wikitext(self:monthName()) |
end | end | ||
end | end | ||
− | if self. | + | if self.year then |
− | if self. | + | if self.month then |
displaySpan:wikitext(' ') | displaySpan:wikitext(' ') | ||
end | end | ||
− | displaySpan:wikitext(math.abs(self. | + | displaySpan:wikitext(math.abs(self.year)) |
− | if self. | + | if self.year < 0 then |
displaySpan:wikitext(' BC') | displaySpan:wikitext(' BC') | ||
end | end |
Revision as of 06:33, 1 July 2015
Documentation for this module may be created at Module:Dts/doc
local getArgs = require('Module:Arguments').getArgs -------------------------------------------------------------------------------- -- Dts class -------------------------------------------------------------------------------- local Dts = {} Dts.__index = Dts Dts.monthsSearch = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" } Dts.monthsAbr = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } Dts.months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } function Dts.new(args) local self = setmetatable({}, Dts) self.fmt = "mdy" self.abbr = false --default if args[1] and (not args[2]) then for _, val in pairs(mw.text.split(args[1],"[%s/-]")) do self:annonval(val, true) end else for key, val in pairs(args) do if tonumber(key) then self:annonval(val) end end end if args.format then self.fmt = args.format end if args.abbr then if args.abbr == "on" then self.abbr=true else self.abbr=false end end if (self.year==0) then --not valid. placeholder for no-year self.year = nil end return self end function Dts:setmonth(raw) if not raw then self.month = nil return false end local numbermonth = tonumber(raw) if numbermonth and numbermonth > 0 and numbermonth < 13 then self.month = numbermonth return true end for i, mon in pairs(self.monthsSearch) do if string.find(string.lower(raw),mon) then self.month=i if string.find(string.lower(raw),string.lower(self.months[i])) then self.abbr=false else self.abbr=true end return true end end return false end function Dts:annonval(val, dayfirst) local numberval if val then numberval = tonumber(mw.text.trim(val,"%s%t,")) end if (not val) or (type(val)=="table") or (mw.text.trim(val)=="") then numberval = 0 end if not numberval then if mw.text.trim(string.lower(val)) == "bc" then if (not self.year) then self.year = self.day self.day = nil end if self.year then self.year = 0 - self.year end else if self:setmonth(val,dayfirst) and dayfirst and self.year and (not self.day) and (self.year > 0) and (self.year<31) then self.day = self.year self.year = nil self.fmt = "dmy" end end return end if self.month and (not self.day) and (numberval < 32) and (numberval > 0) then self.day = numberval return end if self.year and (not self.month) then self.month = numberval return end if (not self.year) then self.year = numberval return end end function Dts:monthName() if (not self.month) or (self.month < 0) or (self.month > 12) then return "" end if self.abbr then return self.monthsAbr[self.month] else return self.months[self.month] end end function Dts:sortkey() local root = mw.html.create() -- Sort span local sortSpan = root:tag('span') sortSpan :addClass('sortkey') :css('display', 'none') :css('speak', 'none') local current = os.date("*t") local year = self.year or current.year year = year > 0 and year or -10000 - year sortSpan:wikitext(string.format( "%05d-%02d-%02d-%02d%02d", year, self.month or 1, self.day or 1, 0, 0 )) return root end function Dts:__tostring() local root = self:sortkey() local displaySpan = root:tag('span') displaySpan:css('white-space', 'nowrap') if self.day then if self.fmt == "mdy" then displaySpan:wikitext(self:monthName()) displaySpan:wikitext(' ') displaySpan:wikitext(self.day) if self.year then displaySpan:wikitext(',') end else displaySpan:wikitext(self.day) displaySpan:wikitext(' ') displaySpan:wikitext(self:monthName()) end else if self.month then displaySpan:wikitext(self:monthName()) end end if self.year then if self.month then displaySpan:wikitext(' ') end displaySpan:wikitext(math.abs(self.year)) if self.year < 0 then displaySpan:wikitext(' BC') end end return tostring(root) end -------------------------------------------------------------------------------- -- Exports -------------------------------------------------------------------------------- local p = {} function p._main(args) local dts = Dts.new(args) return tostring(dts) end function p.main(frame) local args = getArgs(frame, { wrappers = 'Template:Dts', removeBlanks = false }) return p._main(args) end return p