summaryrefslogtreecommitdiff
path: root/activity.lua
blob: 9a602b5cadddf75f8225a0026683c80c463f8c39 (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 dates = require "activity.dates"
local git = require "activity.git"
local activity = {}


function activity.generate_table (year, lookup, format)
	year = year or dates.this_year()

	local function day_of_year (first_weekday, week, weekday)
		return week * 7 + weekday - first_weekday + 1
	end

	local function level (count)
		if 0 < count and count <= 2 then
			return 1
		end
		if 2 < count and count <= 4 then
			return 2
		end
		if 4 < count and count <= 6 then
			return 3
		end
		if 6 < count then
			return 4
		end
		return 0
	end

	local function day_level (first_weekday, week, weekday)
		return level(lookup(year, day_of_year(first_weekday, week, weekday)))
	end

	local function cell (exists)
		if not exists then
			return format.spot
		end
		return format.cell
	end

	local function row (weekday, index, start_from, first, last)
		first = cell(first)
		last = cell(last)
		local row = format.start_row() .. format.label(weekday)
		row = row .. first(day_level(start_from, 0, index))
		for week=1,51 do
			row = row .. format.cell(day_level(start_from, week, index))
		end
		row = row .. last(day_level(start_from, 52, index)) .. format.end_row()
		return row
	end

	local rows = ""
	local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""}
	local start_from = dates.first_week_day(year)
	local end_at = (start_from + dates.days_in(year) - 1) % 7
	for index, weekday in pairs(weekdays) do
		rows = rows .. row(weekday, index, start_from, index >= start_from, index <= end_at)
	end
	return
		format.start_document"Activity" .. format.start_table() .. rows .. format.end_table() .. format.end_document()
end


return activity