summaryrefslogtreecommitdiffstats
path: root/_scripts/generate_index.lua
blob: ec56f194d0807dbd0275b7f38100fd8ba590f605 (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
#!/usr/bin/lua

markdown = require('discount')
lyaml = require('lyaml')
lfs = require('lfs')

function shell_escape(args)
	local ret = {}
	for _,a in pairs(args) do
		s = tostring(a)
		if s:match("[^A-Za-z0-9_/:=-]") then
			s = "'"..s:gsub("'", "'\\''").."'"
		end
		table.insert(ret,s)
	end
	return table.concat(ret, " ")
end

function run(args)
	local h = io.popen(shell_escape(args))
	local outstr = h:read("*a")
	return h:close(), outstr
end

function read_meta(file)
	local f = assert(io.open(file))
	local header, body = f:read("*a"):match("^(%-%-%-.-%-%-%-)(.*)$")
	f:close()
	local m = lyaml.load(header)
	m.pagename = file:gsub("%.md$", ""):gsub("^.*/", "")
	m.md = file
	m.html = m.pagename..".html"
	-- timestamps in Atom must conform to RFC 3339
	m.updated= os.date('!%Y-%m-%dT%H:%M:%SZ', lfs.attributes(file).modification)
	return m
end

a = {}
j=1
for i=1, #arg do
	local m = read_meta(arg[i])
	if m.date then
		if not m.author then
			local rc, author = run{'git', 'log', '-n', '1',
						'--pretty=format:%an',
						'--', arg[i]}
			if rc then
				m.author = author
			else
				m.author = "unknown"
			end
		end

		a[j] = m
		j = j + 1
	end

end
table.sort(a, function(a,b)
	return a.date > b.date
end)
io.write(lyaml.dump{a})