Module:Current events calendar: Difference between revisions
Content added Content deleted
Vivaporius (talk | contribs) m (1 revision imported) |
en>Centrist16 No edit summary |
||
Line 1: | Line 1: | ||
-- This module renders the calendar seen on [[Portal:Current events]]. |
-- This module renders the calendar seen on [[Portal:Current events]]. |
||
--[[ |
|||
Incoming expected variables: |
|||
frame.args.year = Integer value for year |
|||
frame.args.month = Integer value for month, 1 based. |
|||
--]] |
|||
local p = {} |
local p = {} |
||
Line 11: | Line 17: | ||
end |
end |
||
function p.main() |
function p.main(frame) |
||
local |
local argsDate = nil |
||
if (frame and frame.args and frame.args.year and frame.args.month) then |
|||
-- If a date is passed in, assume that the display page is an Archive page. |
|||
-- If no date passed in, assume that the display page is the current Current Events page |
|||
argsDate = frame.args.year .. "-" .. frame.args.month .. "-01" -- Construct a date, YYY-M-DD format. |
|||
⚫ | |||
local dateStuff = p.getDateStuff(argsDate) |
|||
local dayStrings = p.makeDayStrings(dateStuff) |
local dayStrings = p.makeDayStrings(dateStuff) |
||
return p.export(dayStrings, dateStuff) |
return p.export(dayStrings, dateStuff) |
||
end |
end |
||
function p.getDateStuff() |
function p.getDateStuff(argsDate) |
||
--[[ |
|||
Note: This function takes advantage of the formatDate's second argument to |
|||
create data for the archival calendars. If the second arg (argsDate) is nil, |
|||
then formatDate assumes the current date/time. |
|||
--]] |
|||
-- Gets date data. |
-- Gets date data. |
||
local dateStuff = {} |
local dateStuff = {} |
||
local lang = mw.language.getContentLanguage() |
local lang = mw.language.getContentLanguage() |
||
dateStuff.argsDate = argsDate |
|||
--Year |
--Year |
||
local year = lang:formatDate('Y') |
local year = lang:formatDate('Y', argsDate) |
||
year = tonumber(year) |
year = tonumber(year) |
||
dateStuff.year = year |
dateStuff.year = year |
||
-- Month |
-- Month |
||
local month = lang:formatDate('F') |
local month = lang:formatDate('F', argsDate) |
||
dateStuff.month = month |
dateStuff.month = month |
||
-- Month and year |
-- Month and year |
||
local monthAndYear = lang:formatDate('F Y') |
local monthAndYear = lang:formatDate('F Y', argsDate) |
||
local firstOfMonth = lang:formatDate('01-m-Y') |
local firstOfMonth = lang:formatDate('01-m-Y', argsDate) |
||
dateStuff.monthAndYear = monthAndYear |
dateStuff.monthAndYear = monthAndYear |
||
-- Previous month and year |
-- Previous month and year |
||
Line 37: | Line 57: | ||
dateStuff.nextMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' +1 month') |
dateStuff.nextMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' +1 month') |
||
-- Day |
-- Day |
||
local day = lang:formatDate('j') |
local day = lang:formatDate('j', argsDate) |
||
day = tonumber(day) |
day = tonumber(day) |
||
dateStuff.day = day |
dateStuff.day = day |
||
Line 60: | Line 80: | ||
local makeDayLink = p.makeDayLink |
local makeDayLink = p.makeDayLink |
||
for day = 1, dateStuff.daysInMonth do |
for day = 1, dateStuff.daysInMonth do |
||
if isLinkworthy(day, currentDay) then |
if dateStuff.argsDate or isLinkworthy(day, currentDay) then |
||
calStrings[#calStrings + 1] = makeDayLink(day, currentMonth, currentYear) |
calStrings[#calStrings + 1] = makeDayLink(day, currentMonth, currentYear) |
||
else |
else |
||
Line 81: | Line 101: | ||
function p.makeDayLink(day, month, year) |
function p.makeDayLink(day, month, year) |
||
return string.format("'''[[#%d %s %d|%d]]'''", year, month, day, day) |
return string.format("'''[[#%d %s %d| %d ]]'''", year, month, day, day) |
||
end |
end |
||
Line 88: | Line 108: | ||
local monthAndYear = dateStuff.monthAndYear |
local monthAndYear = dateStuff.monthAndYear |
||
local root = mw.html.create('table') |
local root = mw.html.create('table') |
||
-- The next two lines help to make the table-layout-based Archive pages look good. When the |
|||
-- Archives have been converted to a grid-based layout, this logic can be removed, and the |
|||
-- corressponding CSS margin attribute can be simplified. |
|||
local temporaryMarginAdjustment = "auto !important" |
|||
if dateStuff.argsDate then temporaryMarginAdjustment = "8px 0 0 8px" end |
|||
root |
root |
||
:addClass('infobox') |
:addClass('infobox') |
||
:css{ |
|||
:css{width = '250px', ['text-align'] = 'center', ['background-color'] = '#f5faff', border = '1px solid #cedff2'} |
|||
display = 'table', |
|||
⚫ | |||
width = '100%', |
|||
float = 'initial', |
|||
['max-width'] = '350px', |
|||
margin = temporaryMarginAdjustment, |
|||
['text-align'] = 'center', |
|||
['background-color'] = '#f5faff', |
|||
border = '1px solid #cedff2' |
|||
⚫ | |||
-- Headings |
-- Headings |
||
:tag('tr') |
:tag('tr') |
||
:css('background-color', '#cedff2') |
:css('background-color', '#cedff2') |
||
:tag(' |
:tag('th') |
||
:css{[' |
:css{['text-align'] = 'center'} |
||
⚫ | |||
⚫ | |||
:addClass('noprint') |
|||
⚫ | |||
:done() |
|||
:done() |
:done() |
||
:tag(' |
:tag('th') |
||
:attr('colspan', '5') |
:attr('colspan', '5') |
||
:css{ |
:css{['text-align'] = 'center'} |
||
:wikitext(makeWikilink('Portal:Current events/' .. monthAndYear, monthAndYear)) |
:wikitext(makeWikilink('Portal:Current events/' .. monthAndYear, monthAndYear)) |
||
:done() |
:done() |
||
:tag(' |
:tag('th') |
||
:css{[' |
:css{['text-align'] = 'center'} |
||
⚫ | |||
⚫ | |||
:addClass('noprint') |
|||
⚫ | |||
-- Day of week headings |
-- Day of week headings |
||
Line 112: | Line 150: | ||
local weekdays = {'S', 'M', 'T', 'W', 'T', 'F', 'S'} |
local weekdays = {'S', 'M', 'T', 'W', 'T', 'F', 'S'} |
||
for i, weekday in ipairs(weekdays) do |
for i, weekday in ipairs(weekdays) do |
||
dayHeadingRow:tag(' |
dayHeadingRow:tag('th') |
||
:css{['width'] = '14%', ['text-align'] = 'center'} |
|||
:wikitext(weekday) |
:wikitext(weekday) |
||
end |
end |
||
-- Days |
-- Days |
||
local |
local cellCount = 1 - dateStuff.firstWeekday -- Tracks the number of day cells. Negative values used for initial blank cells. |
||
while cellCount < #dayStrings do -- Weekly rows |
|||
local weeklyRow = root:tag('tr') |
|||
⚫ | |||
if colspan > 1 then |
|||
⚫ | |||
⚫ | |||
elseif colspan == 1 then |
|||
⚫ | |||
⚫ | |||
for i = colspan + 1, 7 do -- Finish the first row |
|||
cellCount = cellCount + 1 |
|||
firstDayRow:tag('td') |
|||
:wikitext(dayStrings[cellCount]) |
|||
⚫ | |||
while cellCount < #dayStrings do -- Second day row onwards |
|||
local otherDayRow = root:tag('tr') |
|||
⚫ | |||
cellCount = cellCount + 1 |
cellCount = cellCount + 1 |
||
local dayString = dayStrings[cellCount] |
local dayString = dayStrings[cellCount] or " " -- Use a blank cell if there is no corresponding dateString |
||
⚫ | |||
if not dayString then |
|||
:css{['text-align'] = 'center'} |
|||
break |
|||
end |
|||
⚫ | |||
:wikitext(dayString) |
:wikitext(dayString) |
||
end |
end |
||
Line 145: | Line 169: | ||
-- Footer |
-- Footer |
||
if not dateStuff.argsDate then -- No footer necessary on Archive pages. |
|||
⚫ | |||
:tag(' |
root:tag('tr') |
||
:addClass('noprint') |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return tostring(root) |
return tostring(root) |