Module:One page calendar

require ('strict');

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

-- sequence 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)

{}, -- table of months that begin on sunday

{}, -- table of months that begin on monday

{}, -- tuesday

{}, -- wednesday

{}, -- thursday

{}, -- friday

{}, -- saturday

}

local subheader_rows_t = { -- will get abbreviated month names for each of the days of the week

{}, -- these are the first three 'subheader' rows at the top of the wikitable; top

{}, -- middle

{}, -- bottom

}

local week_day_names_t = { -- for i18n though Help:Extension:ParserFunctions##time says day names rarely internationalized

['sun_t'] = {}, -- holds calculated weekday names: [1]=, [2]=, [3]=

['mon_t'] = {}, -- if your MediaWiki does not have weekday names for your language, these tables can be filled manually

['tue_t'] = {},

['wed_t'] = {},

['thu_t'] = {},

['fri_t'] = {},

['sat_t'] = {},

}

local row_format_str = '|| %s || %s || %s || %s || %s || %s || %s '; -- format string for the three subheader rows

local weekday_rows_fmt_t = { -- format strings for weekday wikitable rows

'| 1 || 8 || 15 || 22 || 29 || %s || %s || %s || %s || %s || %s || %s',

'| 2 || 9 || 16 || 23 || 30 || %s || %s || %s || %s || %s || %s || %s',

'| 3 || 10 || 17 || 24 || 31 || %s || %s || %s || %s || %s || %s || %s',

'| 4 || 11 || 18 || 25 || || %s || %s || %s || %s || %s || %s || %s',

'| 5 || 12 || 19 || 26 || || %s || %s || %s || %s || %s || %s || %s',

'| 6 || 13 || 20 || 27 || || %s || %s || %s || %s || %s || %s || %s',

'| 7 || 14 || 21 || 28 || || %s || %s || %s || %s || %s || %s || %s',

}

local caption_fmt = 'Calendar for year with 1 %s on a %s'; -- first %s is 'January' in local language; second %s is day name in local language

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

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

supported parameters are:

|year= the year for which this calendar is rendered; defaults to current year

|lang-tag= language in which this calendar is rendered; defaults to wiki's own language

|table-style= css style string suitable for use in an html style="" attribute; no quotes; defaults to empty string

|caption= a caption that replaces the default caption

]]

local function main (frame)

local args_t = getArgs (frame);

args_t['table-style'] = (args_t['table-style'] and ' style="' .. args_t['table-style'] .. '"') or ''; -- if not set, empty string for concatenation; applies to whole table

local lang_object;

if args_t['lang-tag'] then

lang_object = mw.language.new (args_t['lang-tag']); -- get a language object for the language specified by |lang-tag=

else

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

end

if not week_day_names_t.sun_t[1] then -- if empty auto fill; otherwise assume that tables have been filled manually at a local wiki

for i, day in ipairs ({'sun_t', 'mon_t', 'tue_t', 'wed_t', 'thu_t', 'fri_t', 'sat_t'}) do -- sequence values are used as indexes into week_day_names_t

local long = lang_object:formatDate ('l', '2022-05-0' .. i); -- 2022-05-01 is known to be a Sunday; calculate day names for the rest of the week

local abbreviated = lang_object:formatDate ('D', '2022-05-0' .. i); -- 2022-05-01 is known to be a Sun; calculate abbreviated day names for the rest of the week

week_day_names_t[day][1] = abbreviated; -- abbreviated day names: Sun, Mon, etc in local wiki language

week_day_names_t[day][2] = long; -- full day names: Sunday, Monday, etc in local wiki language

week_day_names_t[day][3] = table.concat ({'', abbreviated, ''});

end

end

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 occurs; for day name in table caption

local jan_1_mon = lang_object:formatDate ('F', year .. '-01-01'); -- i18n: get month name for -01-01; for month name in table caption

for m=1, 12 do

local month_abbr = lang_object:formatDate ('M', year .. '-' .. m); -- get abbreviated month name

local day_number = lang_object:formatDate ('w', year .. '-' .. m .. '-01') + 1; -- returns a value 0-6; +1 offset makes 1-7 to index into first_day_t lua sequence

table.insert (first_day_t[day_number], month_abbr); -- add abbreviated month name to appropriate day number sequence

end

for row=1, 3 do -- for each of the three 'month' subheader rows

for day=1, 7 do -- and for each day of the week in that row

if first_day_t[day][row] then -- if first of the month occurs on

table.insert (subheader_rows_t[row], first_day_t[day][row]); -- insert abreviated month name

else

table.insert (subheader_rows_t[row], ''); -- insert empty string else

end

end

end

local out_t = {}; -- components of the output go here

local caption = (args_t.caption and args_t.caption) or string.format (caption_fmt, jan_1_mon, jan_1_day);

table.insert (out_t, '

class="wikitable"' .. args_t['table-style'] .. '\n|+ ' .. caption); -- open wikitable

table.insert (out_t, table.concat ({'! colspan="12" | ', year})); -- common column header holds year value from |year= or current year

for i, row_t in ipairs (subheader_rows_t) do -- for each of the three 'month' subheader 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

-- append static day numbers and weekday rows

table.insert (out_t, string.format (weekday_rows_fmt_t[1],

week_day_names_t.sun_t[3], week_day_names_t.mon_t[3], week_day_names_t.tue_t[3], week_day_names_t.wed_t[3], week_day_names_t.thu_t[3], week_day_names_t.fri_t[3], week_day_names_t.sat_t[3]));

table.insert (out_t, string.format (weekday_rows_fmt_t[2],

week_day_names_t.mon_t[3], week_day_names_t.tue_t[1], week_day_names_t.wed_t[1], week_day_names_t.thu_t[1], week_day_names_t.fri_t[1], week_day_names_t.sat_t[1], week_day_names_t.sun_t[1]));

table.insert (out_t, string.format (weekday_rows_fmt_t[3],

week_day_names_t.tue_t[3], week_day_names_t.wed_t[1], week_day_names_t.thu_t[1], week_day_names_t.fri_t[1], week_day_names_t.sat_t[1], week_day_names_t.sun_t[1], week_day_names_t.mon_t[1]));

table.insert (out_t, string.format (weekday_rows_fmt_t[4],

week_day_names_t.wed_t[3], week_day_names_t.thu_t[1], week_day_names_t.fri_t[1], week_day_names_t.sat_t[1], week_day_names_t.sun_t[1], week_day_names_t.mon_t[1], week_day_names_t.tue_t[1]));

table.insert (out_t, string.format (weekday_rows_fmt_t[5],

week_day_names_t.thu_t[3], week_day_names_t.fri_t[1], week_day_names_t.sat_t[1], week_day_names_t.sun_t[1], week_day_names_t.mon_t[1], week_day_names_t.tue_t[1], week_day_names_t.wed_t[1]));

table.insert (out_t, string.format (weekday_rows_fmt_t[6],

week_day_names_t.fri_t[3], week_day_names_t.sat_t[1], week_day_names_t.sun_t[1], week_day_names_t.mon_t[1], week_day_names_t.tue_t[1], week_day_names_t.wed_t[1], week_day_names_t.thu_t[1]));

table.insert (out_t, string.format (weekday_rows_fmt_t[7],

week_day_names_t.sat_t[3], week_day_names_t.sun_t[1], week_day_names_t.mon_t[1], week_day_names_t.tue_t[1], week_day_names_t.wed_t[1], week_day_names_t.thu_t[1], week_day_names_t.fri_t[1]));

table.insert (out_t, '

'); -- close wikitable

return table.concat (out_t, '\n|-\n'); -- make a big string and done

end

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

]]

return {

main = main

}