Module:Calendar widget
Appearance
{{Module rating }}
Usage
{{#invoke:Calendar widget|calendar}}
Lua error in package.lua at line 80: module 'Module:No globals' not found.
Parameters
Calendar parameters
|year=
– specifies the year to be used when creating a monthly or yearly calendar; Gregorian calendar only; minimum 1583 for yearly calendar; minimum October 1582 for stand-alone month calendar; when omitted or out of range, uses current year|month=
– specifies the month to be used when creating an stand-alone month calendar in the year specified by|year=
; accepts a variety values:- numbers
1
to12
– defaults to current month when month number is out of range - month names (
January
,March
, etc) – defaults to current month when month name is not recognized - keywords:
current
– display the current monthlast
– display the month that occurs before the current monthnext
– display the month that occurs after the current month
- numbers
|cols=
– yearly calendars only; number of columnsn
to be used for calendar rendering; default is4
; values ofn
less than 1 or greater than 12 ignored|iso=
– accepts the single valueyes
; calendar renders in ISO week format (Monday through Sunday); not needed if|iso_wk=
set|iso_wk=
– accepts the single valueyes
; calendar renders in ISO week format (Monday through Sunday) with ISO week number in the left column; setting|iso_wk=yes
automatically sets|iso=yes
Styling parameters
|float=
– position the rendered calendar; default position is at the left page margin:center
– middle of the pageright
– at the right page margin
|hide_year=
– accepts the single valueyes
; suppresses display of year in calendar headers; alias|show_year=off
|show_today=
– accepts the single valueyes
; highlights the current date in the current-month calendar|today_color=
– set the highlight color used by|show-today=
; alias:|today_colour=
|title_color=
– set background color for the month title bar; overrides|color=
; alias:|title_colour=
|week_color=
– set background color for the day-abbreviations title bar; overrides|color=
; alias:|week_colour=
|color=
– shorthand for both|title_color=
and|week_color=
;|color=
yields to|title_color=
and|week_color=
; alias:|colour=
|wknum_color=
– set background color for ISO week numbers; alias:|wknum_colour=
Linking parameters
|lk=
– various date component linking options:d
– link days in calendar to calendar day of calendar month –[[June 7]]
m
– link month in calendar header to month article –[[June]] 2019
y
– link year in calendar header to year article –June [[2025]]
dm
– link to days and monthdy
– link to days and yearmy
– link to month and yearyes
– individually link all date componentsm&y
– stand-alone month calendars only; link month and year together as a single composite link –[[June 2025]]
dm&y
– stand-alone month calendars only; link to days and composite month/year
Link prefixes and suffixes
These parameters require |lk=
:
|lk_pref=
– prefix for all day, month, and year links enabled by|lk=
; yields to specific|lk_pref_x=
parameters|lk_suff=
– suffix for all day, month, and year links enabled by|lk=
; yields to specific|lk_suff_x=
parameters
These parameters automatically set |lk=
to the appropriate value; override values assigned to |lk_pref=
and |lk_suff=
:
|lk_pref_d=
– prefix for day links|lk_pref_m=
– prefix for month and composite month/year links|lk_pref_y=
– prefix for year links|lk_suff_d=
– suffix for day links|lk_suff_m=
– suffix for month links and composite month/year links|lk_suff_y=
– suffix for year links
For stand-alone month calendars only, links to previous- and next-month targets:
|prevnext=
– accepts the single valueyes
; adds generic << and >> links to month header linked to the preceding and next month articles; automatically set if any of the following parameters are set:|lk_pref_mprev=
– prefix for previous-month link|lk_suff_mprev=
– suffix for previous-month link|lk_pref_mnext=
– prefix for next-month link|lk_suff_mnext=
– suffix for next-month link
Examples
if the current page and section is [[An example page#May]]
and there is a May calendar there, to offer links to the previous month (April) and next month (June) sections set:
|k_pref_mprev=#
– creates link to[[An example page#April]]
|k_pref_mnext=#
– creates link to[[An example page#June]]
if the current page is a subpage [[An example page/May]]
and there is a May calendar there, to offer links to the previous month and next month subpages set:
|k_pref_mprev=../
– creates link to[[An example page/April]]
|k_pref_mnext=../
– creates link to[[An example page/June]]
- in
../
,..
is the parent ([[An example page]]
) and/
is the required path separator; see Uniform Resource Identifier
- in
--[[
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
--[[--------------------------< W E E K _ D A Y _ H D R >------------------------------------------------------
TODO: get rid of this in favor of a generic repeated-tag function
]]
local function week_day_hdr ()
local headers = {};
for c = 1, 7 do
local header = mw.html.create ('th');
header
:addClass ('mcal')
:attr ('scope', 'col')
:wikitext (dayabbr[c])
:done()
table.insert (headers, tostring (header)); -- make a string of this object
end
return table.concat (headers)
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', ...}
TODO: make this into a more generic repeated-tag function
]]
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 _ D A T E S >----------------------------------------------------
gets a week (row) of calendar dates each in its own <td>...</td>
]]
local function get_row_dates (firstday, mnum, row)
local options = {['class']='mcal'}; -- table of otions for these td tags
local td_items = {}; -- table of <td>...</td> tag strings
for col = 1, 7 do
local dom = 7 * (row-1) + col + 1 - firstday -- calculate day of month for row/col position
if dom < 1 or dom > daysinmonth[mnum] then dom = " " end -- before or after month, blank cell
table.insert (td_items, dom)
end --columns
return make_td_tags (td_items, options)
end
--[[--------------------------< D I S P L A Y M O N T H >------------------------------------------------------
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 '';
local month_cal = mw.html.create ('table');
month_cal
:addClass ('mcal')
:tag ('tr') -- for month name header
:addClass ('mcalhdr')
:tag ('th')
:addClass ('mcal')
:attr ('colspan', '7')
:wikitext (monthname[mnum] .. ' ' .. hdr_year)
:done() -- close <th>
:done() -- close <tr>
:tag ('tr') -- for weekday header
:addClass ('mcalhdr')
:wikitext (week_day_hdr())
:done() -- close <tr>
local numrows = math.ceil ((firstday + daysinmonth[mnum] - 1) / 7); -- calculate number of rows needed for this calendar
for row = 1, numrows do
month_cal
:tag ('tr') -- for this week
:addClass ('mcal')
:wikitext (get_row_dates (firstday, mnum, row)); -- get dates for this week
end
month_cal:done() -- close <table>
--mw.log (tostring (month_cal))
return tostring (month_cal)
-- 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 = " " 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
--[[--------------------------< 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, ' ')
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