Jump to content

Module:One page calendar

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 20:53, 6 November 2022 (Created page with 'require ('strict'); local getArgs = require ('Module:Arguments').getArgs local lang_object = mw.language.getContentLanguage(); -- get a language object -- table to hold abbreviated month names where first-of-month is on day local first_day_t = { -- lang_object:formatDate ('L', 'YYYY-01-01') + 1 returns 1 (Sunday) to 7 (Saturday) [1] = {}, -- table of months that begin on sunday [2] = {},...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

require ('strict');
local getArgs = require ('Module:Arguments').getArgs

local lang_object = mw.language.getContentLanguage();							-- get a language object

																				-- table to hold abbreviated month names where first-of-month is on day
local first_day_t = {															-- lang_object:formatDate ('L', 'YYYY-01-01') + 1 returns 1 (Sunday) to 7 (Saturday)
	[1] = {},																	-- table of months that begin on sunday
	[2] = {},																	-- table of months that begin on monday
	[3] = {},																	-- tuesday
	[4] = {},																	-- wednesday
	[5] = {},																	-- thursday
	[6] = {},																	-- friday
	[7] = {},																	-- saturday
}


--[[--------------------------< M A I N >----------------------------------------------------------------------

	{{#invoke:Sandbox/trappist the monk/calendar|main|year=}}

]]

local function main (frame)
	local args_t = getArgs (frame)
	local year = (args_t.year and args_t.year) or lang_object:formatDate ('Y');	-- get year from |year=; current year else	TODO: get year from article title?
	local jan_1_day = lang_object:formatDate ('l', year .. '-01-01');			-- get day name for day on which 1 January <year> occurs

	for m=1, 12 do
		local month_name = lang_object:formatDate ('M', year .. '-' .. m);		-- get abbreviated month name
		local day_number = lang_object:formatDate ('w', year .. '-' .. m .. '-01') + 1;	-- +1 ofset for lua tables
		table.insert (first_day_t[day_number], month_name);						-- add abbreviated month name to appropriate day number sequence
	end

	local rows_t = {															-- will get abbreviated month names for each of the days of the week
		{},
		{},
		{},
		}

	local row_str = '';
	for row=1, 3 do																-- for each of the three 'month' sub-header rows
		for day=1, 7 do															-- and for each day of the wee in that row
			if first_day_t[day][row] then										-- if first of the month occurs on that day
				table.insert (rows_t[row], first_day_t[day][row]);				-- insert abreviated month name
			else
				table.insert (rows_t[row], '');									-- insert empty string
			end
		end
	end

	local row_format_str = '|| %s || %s || %s || %s || %s || %s || %s ';		-- for string.format()
	local out_t = {};

	for i, row_t in ipairs (rows_t) do											-- for each of the three 'month' sub-header rows
		if 1 == i then															-- first of these rows has rowspan and colspan styling
			table.insert (out_t, string.format ('| colspan="5" rowspan="3" |Date ' .. row_format_str, row_t[1], row_t[2], row_t[3], row_t[4], row_t[5], row_t[6], row_t[7]));
		else																	-- the others have no styling
			table.insert (out_t, string.format (row_format_str, row_t[1], row_t[2], row_t[3], row_t[4], row_t[5], row_t[6], row_t[7]));
		end
	end

	table.insert (out_t, 1, '{| class="wikitable"\n|+ Calendar for year with 1 January on a ' .. jan_1_day);	-- open wikitable
	table.insert (out_t, 2, table.concat ({'! colspan="12" | ', year}));		-- common column header holds year valye from |year=
																				-- append static day numbers and weekday rows
	table.insert (out_t, '| 1 ||  8 || 15 || 22 || 29 || {{abbr|Sun|Sunday}} || {{abbr|Mon|Monday}} || {{abbr|Tue|Tuesday}} || {{abbr|Wed|Wednesday}} || {{abbr|Thu|Thursday}} || {{abbr|Fri|Friday}} || {{abbr|Sat|Saturday}}')
	table.insert (out_t, '| 2 ||  9 || 16 || 23 || 30 || {{abbr|Mon|Monday}} || Tue || Wed || Thu || Fri || Sat || Sun')
	table.insert (out_t, '| 3 || 10 || 17 || 24 || 31 || {{abbr|Tue|Tuesday}} || Wed || Thu || Fri || Sat || Sun || Mon')
	table.insert (out_t, '| 4 || 11 || 18 || 25 ||    || {{abbr|Wed|Wednesday}} || Thu || Fri || Sat || Sun || Mon || Tue')
	table.insert (out_t, '| 5 || 12 || 19 || 26 ||    || {{abbr|Thu|Thursday}} || Fri || Sat || Sun || Mon || Tue || Wed')
	table.insert (out_t, '| 6 || 13 || 20 || 27 ||    || {{abbr|Fri|Friday}} || Sat || Sun || Mon || Tue || Wed || Thu')
	table.insert (out_t, '| 7 || 14 || 21 || 28 ||    || {{abbr|Sat|Saturday}} || Sun || Mon || Tue || Wed || Thu || Fri')

	table.insert (out_t, '|}');													-- close wikitable
	
	return frame:preprocess (table.concat (out_t, '\n|-\n'));					-- make a big string and preprocess (for {{abbr}}) and done
end


--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]

return	{
	main = main
	}