summaryrefslogtreecommitdiff
path: root/activity.lua
blob: 1be6e9363d38682f27c5dffd30921ecaf4963e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 [[<td>]]
	end

	local function day (level)
		return ([[<td data-activity-level="%d">]]):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 = ([[<tr><td class="activity-chart-label"><span>%s</span>]]):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 [[<table class="activity-chart">]] .. "\n" .. rows .. "</table>"
end


return activity