summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-09-12 22:32:21 +0200
committerAki <please@ignore.pl>2023-09-12 22:32:21 +0200
commitab1cb08298363d429bc811ba2d3375a3fa647065 (patch)
tree8b501e12215d6fe4d201e426fda096478160ae12
parent86e7fa2004c12db0bda8e8fd7697fd8d64b2e4b6 (diff)
downloadactivity-ab1cb08298363d429bc811ba2d3375a3fa647065.zip
activity-ab1cb08298363d429bc811ba2d3375a3fa647065.tar.gz
activity-ab1cb08298363d429bc811ba2d3375a3fa647065.tar.bz2
Extracted output format to own module for potential later expansion
-rw-r--r--activity.lua23
-rw-r--r--activity/formats/html5.lua82
-rw-r--r--generate.lua8
-rw-r--r--style.css58
4 files changed, 92 insertions, 79 deletions
diff --git a/activity.lua b/activity.lua
index 5aefbdc..8bec2a0 100644
--- a/activity.lua
+++ b/activity.lua
@@ -3,7 +3,7 @@ local git = require "activity.git"
local activity = {}
-function activity.generate_table (year, repositories)
+function activity.generate_table (year, repositories, format)
year = year or dates.this_year()
local lookup = git.lookup(repositories)
@@ -24,30 +24,22 @@ function activity.generate_table (year, repositories)
return 0
end
- 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
+ return format.spot
end
- return day
+ return format.cell
end
local function row (weekday, index, offset, first, last)
first = cell(first)
last = cell(last)
- local row = ([[<tr><td class="activity-chart-label"><span>%s</span>]]):format(weekday)
+ local row = format.start_row() .. format.label(weekday)
row = row .. first(count(index, offset, 0))
for week=2,52 do
- row = row .. day(count(index, offset, week))
+ row = row .. format.cell(count(index, offset, week))
end
- row = row .. last(count(index, offset, 53)) .. "\n"
+ row = row .. last(count(index, offset, 53)) .. format.end_row()
return row
end
@@ -58,7 +50,8 @@ function activity.generate_table (year, repositories)
for index, weekday in pairs(weekdays) do
rows = rows .. row(weekday, index, 8 - start_from, index >= start_from, index <= end_at)
end
- return [[<table class="activity-chart">]] .. "\n" .. rows .. "</table>"
+ return
+ format.start_document"Activity" .. format.start_table() .. rows .. format.end_table() .. format.end_document()
end
diff --git a/activity/formats/html5.lua b/activity/formats/html5.lua
new file mode 100644
index 0000000..8e6e4fd
--- /dev/null
+++ b/activity/formats/html5.lua
@@ -0,0 +1,82 @@
+local stringx = require "pl.stringx"
+local html5 = {}
+
+
+function html5.start_document (title)
+ return stringx.dedent[[
+ <!doctype html>
+ <html lang="en">
+ <meta charset="utf-8">
+ <style>
+ table.activity-chart { border-spacing: 4px; }
+ table.activity-chart tr { height: 11px; }
+ td.activity-chart-label {
+ position: relative;
+ font-size: 12px;
+ min-width: 2em;
+ }
+ td.activity-chart-label span {
+ position: absolute;
+ top: -3px;
+ }
+ td[data-activity-level] {
+ box-sizing: border-box;
+ outline-width: 1px;
+ outline-style: solid;
+ outline-offset: -1px;
+ outline-color: rgba(27, 31, 35, 0.06);
+ border-radius: 2px;
+ height: 11px;
+ min-width: 11px;
+ }
+ td[data-activity-level="0"] { background-color: #ccc; }
+ td[data-activity-level="1"] { background-color: #9be9a8; }
+ td[data-activity-level="2"] { background-color: #40c463; }
+ td[data-activity-level="3"] { background-color: #30a14e; }
+ td[data-activity-level="4"] { background-color: #216e39; }
+ </style>
+ <title>%s</title>]]:format(title) -- dedent appends trailing newline.
+end
+
+
+function html5.end_document ()
+ return "\n"
+end
+
+
+function html5.start_table ()
+ return [[<table class="activity-chart">]] .. "\n"
+end
+
+
+function html5.end_table ()
+ return "</table>"
+end
+
+
+function html5.start_row ()
+ return "<tr>"
+end
+
+
+function html5.end_row ()
+ return "\n"
+end
+
+
+function html5.label (name)
+ return ([[<td class="activity-chart-label"><span>%s</span>]]):format(name)
+end
+
+
+function html5.spot ()
+ return "<td>"
+end
+
+
+function html5.cell (level)
+ return ([[<td data-activity-level="%d">]]):format(level)
+end
+
+
+return html5
diff --git a/generate.lua b/generate.lua
index 0486f7e..c9a21bf 100644
--- a/generate.lua
+++ b/generate.lua
@@ -1,9 +1,5 @@
local activity = require "activity"
+local format = require "activity.formats.html5"
local args = {...}
local year = tonumber(table.remove(args, 1))
-print([[<!doctype html>]])
-print([[<html lang="en">]])
-print([[<meta charset="utf-8">]])
-print([[<link rel="stylesheet" type="text/css" href="style.css">]])
-print([[<title>Activity</title>]])
-print(activity.generate_table(year, args))
+print(activity.generate_table(year, args, format))
diff --git a/style.css b/style.css
deleted file mode 100644
index e5ca66c..0000000
--- a/style.css
+++ /dev/null
@@ -1,58 +0,0 @@
-table.activity-chart {
- border-spacing: 4px;
-}
-
-
-table.activity-chart tr {
- height: 11px;
-}
-
-
-td.activity-chart-label {
- position: relative;
- font-size: 12px;
- min-width: 2em;
-}
-
-
-td.activity-chart-label span {
- position: absolute;
- top: -3px;
-}
-
-
-td[data-activity-level] {
- box-sizing: border-box;
- outline-width: 1px;
- outline-style: solid;
- outline-offset: -1px;
- outline-color: rgba(27, 31, 35, 0.06);
- border-radius: 2px;
- height: 11px;
- min-width: 11px;
-}
-
-
-td[data-activity-level="0"] {
- background-color: #ccc;
-}
-
-
-td[data-activity-level="1"] {
- background-color: #9be9a8;
-}
-
-
-td[data-activity-level="2"] {
- background-color: #40c463;
-}
-
-
-td[data-activity-level="3"] {
- background-color: #30a14e;
-}
-
-
-td[data-activity-level="4"] {
- background-color: #216e39;
-}