From 8ac1faa7950da39f8f10b110abdf7b10bdeaf66a Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 7 May 2023 17:47:02 +0200 Subject: Activity table is now generated by Lua script --- activity.lua | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 activity.lua (limited to 'activity.lua') diff --git a/activity.lua b/activity.lua new file mode 100644 index 0000000..1be6e93 --- /dev/null +++ b/activity.lua @@ -0,0 +1,64 @@ +local activity = {} + + +function activity.days (year) + if year % 4 ~= 0 or year % 100 == 0 and year % 400 ~= 0 then + return 365 + end + return 366 +end + + +function activity.this_year () + return os.date"%Y" +end + + +function activity.first_week_day (year) + year = year or activity.this_year() + return os.date("*t", os.time{year=year, month=1, day=1}).wday +end + + +function activity.generate_table (year) + year = year or activity.this_year() + + local function spot () + return [[]] + end + + local function day (level) + return ([[]]):format(level) + end + + local function cell (exists) + if not exists then + return spot + end + return day + end + + local function row (weekday, first, last) + first = cell(first) + last = cell(last) + local row = ([[%s]]):format(weekday) + row = row .. first(math.random(0, 4)) + for _=1,51 do + row = row .. day(math.random(0, 4)) + end + row = row .. last(math.random(0, 4)) .. "\n" + return row + end + + local rows = "" + local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""} + local start_from = activity.first_week_day(year) + local end_at = (start_from + activity.days(year) - 1) % 7 + for index, weekday in pairs(weekdays) do + rows = rows .. row(weekday, index >= start_from, index <= end_at) + end + return [[]] .. "\n" .. rows .. "
" +end + + +return activity -- cgit v1.1