模块:GithubCalendar
来自永雏塔菲百科
更多操作
此模块的文档可以在模块:GithubCalendar/doc创建
-- Module:GithubCalendar
local p = {}
function p.render(frame)
-- In MediaWiki Scribunto, args tables are metatables.
-- Iterate with pairs() to extract arguments safely.
local args = {}
if frame:getParent() then
for k, v in pairs(frame:getParent().args) do
if v ~= "" then
args[k] = mw.text.trim(v)
end
end
end
for k, v in pairs(frame.args) do
if v ~= "" then
args[k] = mw.text.trim(v)
end
end
-- Default to the current year if not specified
local year = tonumber(args.year) or tonumber(os.date("%Y"))
-- Calculate whether it is a leap year to determine total days
local isLeapYear = (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
local daysInYear = isLeapYear and 366 or 365
-- Get the day of the week for January 1st to offset the starting grid position
local jan1Time = os.time({year = year, month = 1, day = 1})
local jan1Weekday = tonumber(os.date("%w", jan1Time))
-- Adjust to 1 = Sunday, 7 = Saturday to match CSS Grid row flow
jan1Weekday = jan1Weekday + 1
-- Keep the year-wide grid inside the viewport on narrow screens.
-- The inner grid retains its desktop size and becomes horizontally scrollable.
local wrapper = mw.html.create('div')
:addClass('github-calendar-scroll')
:attr('role', 'region')
:attr('aria-label', year .. '年主播3D日历,可横向滚动')
:attr('tabindex', '0')
:css('display', 'block')
:css('box-sizing', 'border-box')
:css('width', '100%')
:css('max-width', '100%')
:css('overflow-x', 'auto')
:css('overflow-y', 'hidden')
-- Add 'plainlinks' class to natively hide MediaWiki's external link icons
local html = wrapper:tag('div')
:addClass('plainlinks')
:addClass('github-calendar-grid')
:css('display', 'grid')
:css('grid-template-rows', 'repeat(7, 15px)')
:css('grid-auto-flow', 'column')
:css('gap', '3px')
:css('width', 'max-content')
:css('padding', '10px')
:css('background', '#fff')
:css('border', '1px solid #d0d7de')
:css('border-radius', '6px')
-- Insert invisible spacer cells to align January 1st with the correct weekday row
for i = 1, jan1Weekday - 1 do
html:tag('div')
:css('width', '15px')
:css('height', '15px')
:css('background-color', 'transparent')
end
-- Loop through every day of the year to generate grid cells
local currentSecs = jan1Time
local daySeconds = 24 * 60 * 60
for i = 1, daysInYear do
local currentDate = os.date("*t", currentSecs)
-- Format month and day to "MM-DD" for dictionary key matching
local monthStr = string.format("%02d", currentDate.month)
local dayStr = string.format("%02d", currentDate.day)
local dateKey = monthStr .. "-" .. dayStr
local fullDateStr = year .. "年" .. currentDate.month .. "月" .. currentDate.day .. "日"
-- Create the individual day cell
-- Set overflow hidden to prevent any text from breaking the grid layout
local cell = html:tag('div')
:css('width', '15px')
:css('height', '15px')
:css('border-radius', '2px')
:css('overflow', 'hidden')
-- Check if the user provided a link for this specific date
local link = args[dateKey]
if link and link ~= "" then
-- Style for active streaming days (green)
cell:css('background-color', '#39d353')
-- Use native MediaWiki external link syntax instead of raw HTML <a> tag.
-- A display:block span fills the 15x15 box to make the whole cell clickable.
local wikitextLink = '[' .. link .. ' <span title="' .. fullDateStr .. ' - 点击观看" style="display:block;width:15px;height:15px;"></span>]'
cell:wikitext(wikitextLink)
else
-- Style for inactive days (gray)
cell:css('background-color', '#ebedf0')
:attr('title', fullDateStr .. ' - 无直播')
end
-- Advance the timestamp by one day
currentSecs = currentSecs + daySeconds
end
return tostring(wrapper)
end
return p