Difference between revisions of "Module:Dts"

From Timelines
Jump to: navigation, search
(make a new object on each run so that we don't have stale data lying around)
m (rm stray comment)
Line 138: Line 138:
 
0
 
0
 
))
 
))
-- retval = retval .. '</span><span style="white-space:nowrap;">'
+
 
 
return root
 
return root
 
end
 
end

Revision as of 23:37, 30 June 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", "Septembre", "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