Module:Time

From Wikispooks
Revision as of 19:27, 1 February 2019 by Robin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Time/doc

local export = {}


local language = mw.getContentLanguage()
local function format(code, timestamp)
	return language:formatDate(code, timestamp)
end

local sub = mw.ustring.sub
local gsub = mw.ustring.gsub


local abbrs = {
	["a."] = { anchor = "a.", full = "ante", },
	["c."] = { anchor = "c.", full = "circa", },
	["p."] = { anchor = "p.", full = "post", },
}


--[[
	"1 January, 2015", with day of the month before month name
	and a comma, is not a valid date format; formatDate will
	ignore the year "2015" and replace it with the current year.
	The comma therefore has to be removed.
]]
local function fix_date(date)
	if tonumber(date) ~= nil then
		error("|date= parameter should contain a full date: year, month, day of month. "
			.. "Use |year= parameter for year.")
	end
	if date then
		return gsub(date, "(%d+ %a+),", "%1")
	end
end


function export.quote(frame)
	local params = {
		["year"] = {},
		["month"] = {},
		["date"] = {},
		["start_date"] = {},
		["start_year"] = {},
		["nodate"] = { type = boolean, default = false },
		["accessdate"] = {},
		["origdate"] = {},
		["origyear"] = {},
		["origmonth"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params, true)
	
	local output = {}
	
	local namespace = mw.title.getCurrentTitle().nsText
	
	local start_date, date = fix_date(args.start_date), fix_date(args.date)
	
	local function insert(text)
		table.insert(output, text)
	end
	
	if args.year then
		local prefix = sub(args.year, 1, 2)
		local abbr = abbrs[prefix]
		
		if abbr then
			insert('\'\'[[Appendix:Glossary#' .. abbr.anchor .. '|<abbr title="' .. abbr.full .. '">' .. abbr.anchor .. '</abbr>]]\'\' ')
			-- [[Special:WhatLinksHere/Template:tracking/quote/abbr]]
			require("Module:debug").track("quote/abbr")
			
			-- Remove lowercase letter, period, and space from beginning of year parameter.
			args.year = gsub(args.year, "%l.%s*", "")
		end
		
		if start_date then
			if format("Y", start_date) == args.year then
				if format("F", start_date) == args.month then
					insert(
						format("'''Y''' F j", start_date)
							.. "–" .. date
					)
				else
					insert(
						format("'''Y''' F j",
							start_date)
							.. " – " .. args.month .. " " .. date
					)
				end
			end
		else
			if args.month then
				if args.start_year then
					insert(
						"'''" .. args.start_year
						.. "'''&nbsp;– "
					)
				end
				
				insert(
					"'''" .. args.year .. "''' "
					.. args.month
				)
				
				if date then
					insert(" " .. date)
				end
			else
				if args.start_year then
					insert("'''" .. args.start_year .. "'''–")
				end
				
				insert("'''" .. args.year .. "'''")
			end
		end
	else
		if date then
			if start_date then
				if format("Y", start_date) == format("Y", date) then
					if format("n", start_date) == format("n", date) then
						insert(
							format("'''Y''' F j", start_date)
								.. "–" .. format("j", date)
						)
					else
						insert(
							format("'''Y''' F j", start_date)
								.. "–" .. format("F j", date)
						)
					end
				else
					insert(
						format("'''Y''' F j", start_date)
							.. "–" .. format("'''Y''' F j", date)
					)
				end
			else
				insert(
					format("'''Y''' F j", date)
				)
			end
		else
			if not args.nodate then
				if args.accessdate then
					insert(
						format("'''Y''' F j", args.accessdate)
						.. " (last accessed)"
					)
				else
					if namespace ~= "Template" then
						insert(
							frame:expandTemplate{ title = "maintenance line", args = {"Can we [[:Category:Requests for date|date]] this quote?"} } ..
							"[[Category:Requests for date]]"
						)
					end
				end
			end
		end
	end

	return table.concat(output)
end

return export