aboutsummaryrefslogtreecommitdiff
path: root/assets/manual/filter_man.lua
blob: bdb1b33d296f670fab3609936226e1dac84069a4 (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
68
69
70
71
72
73
local filters = {}

local function add_filter(x)
	table.insert(filters, x)
end

local is_latex = FORMAT:match "latex"
local is_man = FORMAT:match "man"

add_filter {
	Pandoc = function(elem)
		local seealso = elem.meta.seealso
		if not seealso then
			return
		end
		local blocks = elem.blocks
		blocks:insert(pandoc.Header(1, pandoc.Str("SEE ALSO")))
		if is_man then
			for k, v in pairs(seealso) do
				seealso[k] = v[1].text
			end
			blocks:insert(pandoc.Plain(table.concat(seealso, ", ")))
		else
			local list = {}
			for _, i in ipairs(seealso) do
				local page = i[1].text
				local pgname, pgsection = string.match(page, "^([^%)]+)%(([^%)]+)%)$")
				local item = pandoc.Plain(page)
				if is_latex and pgname and pgsection then
					pgname = pgname:gsub("_","\\string_")
					item = pandoc.RawBlock("latex", string.format("\\manref{%s}{%s}", pgname, pgsection))
				end
				table.insert(list, item)
			end
			blocks:insert(pandoc.BulletList(list))
		end
		return elem
	end
}

if is_latex then
	add_filter {
		Str = function(elem)
			if elem.tex == "Advtrains" then
				return pandoc.RawInline("latex", "\\advtrains{}")
			end
		end
	}
	add_filter {
		Header = function(elem)
			local attr = {
				class = "unnumbered unlisted",
			}
			return pandoc.Header(elem.level+1, elem.content, attr)
		end,
	}
	add_filter {
		Pandoc = function(elem)
			local outputfn = PANDOC_STATE.output_file or error("No output file specified")
			local pgname, pgsection = outputfn:match("([^%./\\]+)%.([^%.]+)%.tex$")
			assert(pgname and pgsection, "Cannot fetch manpage name and section")
			local blocks = elem.blocks
			local titlestr = string.format("%s(%s)", pgname, pgsection)
			local titleid = string.format("man:%s.%s", pgname, pgsection)
			local titleobj = pandoc.Header(1, pandoc.Code(titlestr))
			titleobj.identifier = titleid
			blocks:insert(1, titleobj)
			return elem
		end,
	}
end

return filters