summaryrefslogtreecommitdiff
path: root/generate-index.lua
blob: 0b94821f3432a975f9cea4297d4328bbdf028678 (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
65
66
67
#!/usr/bin/env lua
local pretty = require"pl.pretty"
local dir = require"pl.dir"
local gumbo = require"gumbo"


local function parse (stamp)
	local year, month, day, hour, min, sec = stamp:match"(%d+)-(%d?%d)-(%d?%d)T(%d?%d):(%d?%d):(%d?%d)"
	return {
		year = tonumber(year),
		month = tonumber(month),
		day = tonumber(day),
		hour = tonumber(hour),
		min = tonumber(min),
		sec = tonumber(sec),
	}
end


local function describe (date)
	-- strftime(3) still does not support printing day of month without leading zero or space.
	return string.format("%d %s", date.day, os.date("%B", os.time(date)))
end


local pages = dir.getfiles(".", "*.html")
local takeaways = {
	["published-on"] = "published",
	["last-modified-on"] = "modified",
}
local excludes = {
	["index.html"] = true,
	["graveyard_of_the_drawings.html"] = true,
	["plop.html"] = true,
}
local posts = {}
for _, filename in pairs(pages) do
	local document = gumbo.parseFile(filename)
	local metas = document:getElementsByTagName"meta"
	local properties = {title=document.title, filename=filename:sub(3), published=""}
	for _, meta in ipairs(metas) do
		local take = nil
		for _, attribute in ipairs(meta.attributes) do
			if attribute.name == "name" then
				take = takeaways[attribute.value]
			end
			if take and attribute.name == "content" then
				properties[take] = attribute.value
			end
		end
	end
	if properties.filename:match"^[^_]" and not excludes[properties.filename] then
		table.insert(posts, properties)
	end
end
table.sort(posts, function (lhs, rhs) return lhs.published > rhs.published end)
print[[<table class="index">]]
local year = nil
for _, page in pairs(posts) do
	local date = parse(page.published)
	if date and year ~= date.year then
		year = date.year
		print(string.format("<tr><td><b>%d</b><td><td>", year))
	end
	print(string.format([[<tr><td>%s<td class="sep">•<td><a href="%s">%s</a>]], describe(date), page.filename, page.title))
end
print"</table>"