Jump to content

Module:Calendar widget

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 19:50, 1 July 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
Module to create Calendar widget

--]]
require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;

local lang_obj = mw.language.getContentLanguage();

local p = {}

local daysinmonth = {
	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}
local dayname = {
	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
}
local dayabbr = {}
for i, v in ipairs(dayname) do
	dayabbr[i] = v:sub(1, 2)
end

local monthname = {}
local monthabbr = {}
if 0 == #monthname then
	for m = 1, 12 do
		monthname[m] = lang_obj:formatDate ("F", '2019-' .. m);					-- table of long month names
		monthabbr[m] = lang_obj:formatDate ("M", '2019-' .. m);					-- table of abbreviated month names
	end
end

local function isleap(year)
	return '1' == lang_obj:formatDate ('L', tostring(year));
end

--[[
returns 1 to 7; 1 == Sunday; TODO: error check inputs
--]]
local function dayofweek(year, month, day)
	return lang_obj:formatDate ('w', year .. '-' .. month .. '-' .. day) + 1;
end

-- year and month both numeric:
local function monthstart(year, month)
	return dayofweek(year, month, 1)
end

-- generate the html to display a month calendar
local function displaymonth(props, mnum)
	local year = props.year
	if isleap(year) then daysinmonth[2] = 29 end
	local firstday = dayofweek (year, mnum, 1);									-- get first day number of the first day of the month; 1 == Sunday
	local monthcal = {}
	local hdr_year = props.show_year and year or '';
	table.insert(monthcal, '<table class="mcal">')
	table.insert(monthcal, '<tr class="mcalhdr">')
	table.insert(monthcal, '<th class="mcal" colspan="7">' .. monthname[mnum] .. ' ' .. hdr_year .. '</th>')
	table.insert(monthcal, '</tr>')
	table.insert(monthcal, '<tr class="mcalhdr">')
	for c = 1, 7 do
		table.insert(monthcal, '<th class="mcal" scope="col">' .. dayabbr[c] .. '</th>')
	end
	table.insert(monthcal, '</tr>')
	local numrows = math.ceil( (firstday + daysinmonth[mnum] - 1) / 7 )
	for r = 1, numrows do
--		table.insert(monthcal, '</tr>')
		table.insert(monthcal, '<tr class="mcal">')
		for c = 1, 7 do
			local dom = 7 * (r-1) + c + 1 - firstday
			if dom < 1 or dom > daysinmonth[mnum] then dom = "&nbsp;" end
			table.insert(monthcal, '<td class="mcal">' .. dom .. '</td>')
		end --columns
--		table.insert(monthcal, '<tr class="mcal">')
		table.insert(monthcal, '</tr>')
	end --rows
	table.insert(monthcal, '</table>')
	return table.concat(monthcal, '\n')
end


--[[--------------------------< M A K E _ T D _ T A G S >------------------------------------------------------

create <td> ... </td>... string to be included into <tr>...</tr> as :wikitext(...)

items is a table of items, each of which will be wrapped in <td>...</td>
options is a table of optional class, css, and attribute settings for these td tags
	options.attr is a table of attribute / value pairs: {['attribute'] = 'value', ...}
	options.css is a table of attribute / value pairs: {['attribute'] = 'value', ...}

]]

local function make_td_tags (items, options)
	local td_tags = {};															-- table of <td>...</td> tag strings
	local opt_attr = options.attr or {};										-- if options not supplied, use empty table
	local opt_css = options.css or {};
	
	for i, item in ipairs (items) do
		local td_tag = mw.html.create ('td');									-- new td object
		td_tag
			:addClass (options.class)
			:attr (opt_attr)
			:css (opt_css)
			:wikitext (item)													-- get a calendar for month number mnum
			:done()																-- close <td>
		table.insert (td_tags, tostring (td_tag));								-- make a string of this object
	end
	return table.concat (td_tags);												-- concatenate them all together
end


--[[--------------------------< G E T _ R O W _ C A L E N D A R S >--------------------------------------------

create <td> ... </td>... string to be included into <tr>...</tr> as :wikitext(...)

]]

local function get_row_calendars (cols, row_num, props)
	local mnum;																	-- month number
	local options = {['class']='ycal'};											-- table of otions for these td tags
	local td_items = {};														-- table of <td>...</td> tag strings
	
	for col_num = 1, cols do
		mnum = cols * (row_num - 1) + col_num									-- calculate month number from row and column values
		if mnum < 13 then														-- some sort of error return if ever 13+?
			table.insert (td_items, displaymonth (props, mnum))					-- get a calendar for month number mnum
		end
	end
	return make_td_tags (td_items, options)
end


--[[--------------------------< D I S P L A Y Y E A R >--------------------------------------------------------

]]

local function displayyear(props)
	local year = props.year
	local rows = props.rows
	local cols = props.cols or 4
	local mnum;
	
	if rows then
		cols = math.ceil(12 / rows)
	else
		rows = math.ceil(12 / cols)
	end

	local year_cal = mw.html.create('table');
	year_cal
		:addClass('ycal')
		:tag('tr')
			:addClass('ycalhdr')
				:tag('th')
					:addClass('ycal')
					:attr('colspan', cols)
					:wikitext(year)
					:done()														-- close <th>
			:done()																-- close <tr>

	for row_num = 1, rows do
		year_cal
			:tag('tr')
			:addClass ('ycal')
			:wikitext(get_row_calendars (cols, row_num, props))					-- get calendars for this row each wrapped in <td>...</td> tags as wikitext for this <tr>...</tr>
	end
	year_cal:done()																-- close <table>
mw.log (tostring (year_cal))
	return tostring (year_cal)



--[[
	local yearcal = {}
	table.insert(yearcal, '<table class="ycal">')
	table.insert(yearcal, '<tr class="ycalhdr">')
	table.insert(yearcal, '<th class="ycal" colspan="' .. cols .. '>' .. year .. '</th>')
	for r = 1, rows do
		table.insert(yearcal, '<tr class="ycal">')
		for c = 1, cols do
			mnum = cols * (r - 1) + c
			if mnum < 13 then
				table.insert(yearcal, '<td class="ycal">' .. displaymonth(props, mnum) .. '</td>')
			else
				table.insert(yearcal, '&nbsp;')
			end
		end --cols
		table.insert(yearcal, '</tr>')
	end
	table.insert(yearcal, '</table>')
mw.log (table.concat(yearcal, '\n'))
	return table.concat(yearcal, '\n')
]]
end

local function calendar(props)
	if props.month then
		props.show_year = true;													-- show year in individual month calendars
		return displaymonth(props, props.month);
	else
		return displayyear(props)
	end
end

--------------------------------------------------
--[[
Sakamoto's method: ISO date
returns day name or nil if bad arguments
--]]
function p.dayofweek(frame)
	local isodate = mw.text.trim(frame.args[1] or "")
	local y, m, d = isodate:match("(%d+)%p(%d+)%p(%d+)")
	local dow = dayofweek(y, m, d)
	if not dow then return "" end
	return dayname[dow]
end

--[[
isleap returns "leap" if passed a leap year
otherwise returns nothing
]]
function p.isleap(frame)
	if isleap(frame.args[1]) then return "leap" end
	return ""
end

--[[
Main entry point for widget
--]]
function p.calendar(frame)
	local args=getArgs (frame);
	local cal_props = {};														-- separate calendar properties table to preserve arguments as originally provided
	local this_year_num = tonumber (lang_obj:formatDate ('Y'));
	local this_month_num = tonumber (lang_obj:formatDate ('n'));

	cal_props.year = args.year and tonumber(args.year) or this_year_num;

	if args.month then
		local mnum = tonumber(args.month)
		if not mnum then														-- month provided as some sort of text string
			if args.month == "current" then
				cal_props.month = this_month_num
				cal_props.year = this_year_num
			elseif args.month == "last" then
				mnum = this_month_num - 1
				if mnum == 0 then
					cal_props.month = 12										-- december last year
					cal_props.year = this_year_num - 1								-- last year
				else
					cal_props.month = mnum;										-- previous month
				end
			elseif args.month == "next" then
				mnum = this_month_num + 1
				if mnum == 13 then
					cal_props.month = 1											-- january next year
					cal_props.year = this_year_num + 1								-- next year
				else
					cal_props.month = mnum;										-- next month
				end
			else
				local good
				good, cal_props.month = pcall (lang_obj.formatDate, lang_obj, 'n', args.month);
				
				if not good then
					cal_props.month = this_month_num
				else
					cal_props.month = tonumber (cal_props.month)
				end
			end
		else
			cal_props.month =  (13 > mnum and 0 < mnum) and mnum or this_month_num;	-- month provided as a number
		end
	end
-- TODO: add all other args{} from template or invoke to cal_props{} modified as appropriate
	return calendar(cal_props)
end


return p