diff options
Diffstat (limited to 'activity/generators.lua')
-rw-r--r-- | activity/generators.lua | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/activity/generators.lua b/activity/generators.lua new file mode 100644 index 0000000..ff7f0a9 --- /dev/null +++ b/activity/generators.lua @@ -0,0 +1,97 @@ +local dates = require "activity.dates" +local git = require "activity.git" +local generators = {} + + +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 generate_rolling (lookup, format, date) + date = date or os.date"*t" + local rows = "" + local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""} + + local function year_and_day (week, weekday) + local days = week * 7 + date.wday - weekday + local at = dates.before(date, days) + return at.year, dates.day_of_year(at) + end + + local head = format.start_document"Activity" .. format.start_table() + for index, weekday in pairs(weekdays) do + rows = rows .. format.start_row() .. format.label(weekday) + for week=0,51 do + rows = rows .. format.cell(level(lookup(year_and_day(52 - week, index)))) + end + if date.wday >= index then + rows = rows .. format.cell(level(lookup(year_and_day(0, index)))) + else + rows = rows .. format.spot() + end + rows = rows .. format.end_row() + end + return head .. rows .. format.end_table() .. format.end_document() +end + + +function generators.generate_table (year, lookup, format, ...) + if year == "rolling" then + return generate_rolling(lookup, format, ...) + end + 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 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 generators |