Module:FantasyCalendar
Documentation for this module may be created at Module:FantasyCalendar/doc
local p = {}
local defaultSeasons = {
"Spring", "Summer", "Autumn", "Winter"
}
local defaultMonths = {}
local defaultDays = {}
for i = 1, 12 do
defaultMonths[i] = "Month " .. i
end
for i = 1, 6 do
defaultDays[i] = "Day " .. i
end
-- Read comma-separated custom names
local function getNames(value, defaults)
local names = {}
local parts = {}
if value and value ~= "" then
for part in mw.text.gsplit(value, ",", true) do
table.insert(parts, mw.text.trim(part))
end
end
for i = 1, #defaults do
names[i] = (parts[i] and parts[i] ~= "")
and parts[i] or defaults[i]
end
return names
end
-- Build a six-day, five-week month
local function drawMonth(parent, number, months, days, markM, markD)
local box = parent:tag("div"):addClass("fc-month")
box:tag("div")
:addClass("fc-month-name")
:wikitext(mw.text.encode(months[number]))
local calendar = box:tag("table"):addClass("fc-grid")
local header = calendar:tag("thead"):tag("tr")
for d = 1, 6 do
header:tag("th")
:attr("scope", "col")
:wikitext(mw.text.encode(days[d]))
end
local body = calendar:tag("tbody")
for week = 0, 4 do
local row = body:tag("tr")
for d = 1, 6 do
local date = week * 6 + d
local cell = row:tag("td")
if number == markM and date == markD then
cell:addClass("fc-marked")
end
cell:wikitext(tostring(date))
end
end
end
-- Draw a season containing three months
local function drawSeason(parent, season, seasons, months, days,
selectedMonth, markM, markD)
local section = parent:tag("section"):addClass("fc-season")
section:tag("h3")
:addClass("fc-season-name")
:wikitext(mw.text.encode(seasons[season]))
local monthGrid = section:tag("div"):addClass("fc-months")
local first = (season - 1) * 3 + 1
local last = first + 2
for m = first, last do
if not selectedMonth or selectedMonth == m then
drawMonth(monthGrid, m, months, days, markM, markD)
end
end
end
function p.main(frame)
local args = frame.args
local seasons = getNames(args.seasons, defaultSeasons)
local months = getNames(args.months, defaultMonths)
local days = getNames(args.days, defaultDays)
local view = mw.text.trim(args.view or "year"):lower()
local year = mw.text.trim(args.year or "")
local month = tonumber(args.month) or 1
local season = tonumber(args.season) or 1
if view ~= "year" and view ~= "season" and view ~= "month" then
return "Calendar error: view must be year, season, or month."
end
if view == "month" and
(month < 1 or month > 12 or month % 1 ~= 0) then
return "Calendar error: month must be 1–12."
end
if view == "season" and
(season < 1 or season > 4 or season % 1 ~= 0) then
return "Calendar error: season must be 1–4."
end
-- Optional highlighted date: mark=5-14
local markM, markD = (args.mark or ""):match("^(%d+)%-(%d+)$")
markM = tonumber(markM)
markD = tonumber(markD)
local root = mw.html.create("div"):addClass("fc-calendar")
if year ~= "" then
root:tag("h2")
:addClass("fc-year-name")
:wikitext("Year " .. mw.text.encode(year))
end
if view == "month" then
local seasonIndex = math.ceil(month / 3)
drawSeason(root, seasonIndex, seasons, months, days,
month, markM, markD)
elseif view == "season" then
drawSeason(root, season, seasons, months, days,
nil, markM, markD)
else
for s = 1, 4 do
drawSeason(root, s, seasons, months, days,
nil, markM, markD)
end
end
return tostring(root)
end
return p