about summary refs log tree commit diff
path: root/preproc.lua
blob: e9aa36602c14d0d9a611bab3765bae46d996f325 (plain) (blame)
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
---@diagnostic disable: undefined-global
-- see https://pandoc.org/lua-filters.html
return {
	Link = function(link)
		-- set target="_blank" for outgoing links (open them in new tab)
		if string.match(link.target, "gzr.im") == nil then
			table.insert(link.attributes, { "target", "_blank" })
		end
		return link
	end,
	Pandoc = function(doc)
		local front = {}

		-- add a comment
		table.insert(
			front,
			pandoc.RawBlock(
				"html5",
				"<!-- Generated file, you may edit and discard "
					.. pandoc.path.filename(PANDOC_STATE.input_files[1])
					.. " :) -->"
			)
		)

		local toc = pandoc.structure.table_of_contents(doc)
		if doc.meta.toc and toc then
			table.insert(front, pandoc.RawBlock("html5", '<nav class="table-of-contents capital">'))
			table.insert(front, pandoc.RawBlock("html5", "<ol>"))
			-- insert jump to top
			table.insert(front, pandoc.Link("back to top", "#top"))
			-- create custom toc
			toc:walk({
				Link = function(e)
					table.insert(front, pandoc.RawBlock("html5", "<li>"))
					table.insert(front, e)
					table.insert(front, pandoc.RawBlock("html5", "</li>"))
				end,
			})
			table.insert(front, pandoc.RawBlock("html5", "</ol>"))
			table.insert(front, pandoc.RawBlock("html5", "</nav>"))
		end

		-- insert date blocks
		if doc.meta.date then
			table.insert(front, pandoc.RawBlock("html5", '<span class="date">'))
			table.insert(front, doc.meta.date)
			table.insert(front, pandoc.RawBlock("html5", "</span>"))
		end

		-- prepend front stuffs
		for i = #front, 1, -1 do
			table.insert(doc.blocks, 1, front[i])
		end

		-- return finished ast
		return doc
	end,
}