Debian Codegraph @

src/man-pandoc.lua

raw symbols SCIP
1 -- Tweaks our manual pages during conversion to html by pandoc.
2 --
3 -- See https://pandoc.org/lua-filters.html#debugging-lua-filters on
4 -- how to debug and develop these filters.
5
6 -- Note: you must set the environment variable BINARY to the name of
7 -- the primary executable, e.g., 'sq' or `sq-git`.
8 BINARY = os.getenv("BINARY")
9 if BINARY == nil or BINARY == "" then
10 error("BINARY environment variable should be set to the binary's name, e.g., sq")
11 end
12
13 -- Overwrites the title.
14 --
15 -- It just says 'sq` which looks odd.
16 function Meta(m)
17 m.title = "Sequoia PGP Manual Pages"
18 return m
19 end
20
21 -- Turns subcommand headers into links.
22 function Header (h)
23 -- Given a sequence of subcommands, return the appropriate file
24 -- name.
25 local function subcommand_to_link (s)
26 t = ""
27 for i = 1, #s do
28 if s[i].t == 'Space' then
29 t = t .. '-'
30 else
31 t = t .. s[i].text
32 end
33 end
34 return t .. ".1.html"
35 end
36
37 if h.c[1].t == 'Str' and h.c[1].text == BINARY then
38 h.c = pandoc.Link(h.c, subcommand_to_link(h.c))
39 return h
40 end
41 end
42
43 -- Transform the entries in the "SYNOPSIS" section into links.
44 local function rewrite_synopsis (inlines)
45 -- Returns true if we're looking at a synopsis entry.
46 --
47 -- Matches on the following sequence:
48 --
49 -- [1] LineBreak {}
50 -- [2] Strong {
51 -- content: Inlines[7] {
52 -- [1] Str "sq"
53 -- [2] Space
54 -- [3] Str "key"
55 -- [4] Space
56 -- [5] Str "userid"
57 -- [6] Space
58 -- [7] Str "add"
59 -- }
60 -- }
61 -- [3] Space
62 -- [4] Str "["
63 local function synopsis_p (s)
64 return s[1] and s[1].t == 'LineBreak'
65 and s[2] and s[2].t == 'Strong'
66 and s[2].c[1].text == BINARY and (not s[2].c[2] or s[2].c[2].t == 'Space')
67 and s[3] and s[3].t == 'Space'
68 and s[4] and s[4].t == 'Str' and s[4].text == '['
69 end
70
71 -- Given a sequence of subcommands, return the appropriate file
72 -- name.
73 local function subcommand_to_link (s)
74 t = ""
75 for i = 1, #s do
76 if s[i].t == 'Space' then
77 t = t .. '-'
78 else
79 t = t .. s[i].text
80 end
81 end
82 return t .. ".1.html"
83 end
84
85 for i = 1, #inlines-4 do
86 if synopsis_p(table.pack(table.unpack(inlines, i, i + 3))) then
87 inlines[i+1] = pandoc.Link(inlines[i+1], subcommand_to_link(inlines[i+1].c))
88 end
89 end
90 end
91
92 -- Transforms the entries in the "SEE ALSO" section into links.
93 local function rewrite_see_also (inlines)
94 -- Returns true if we're looking at a see-also entry.
95 --
96 -- Matches on the following sequence:
97 --
98 -- [43] Strong {
99 -- content: Inlines[1] {
100 -- [1] Str "sq-wkd"
101 -- }
102 -- }
103 -- [44] Str "(1),"
104 local function see_also_p (a, b)
105 return a and a.t == 'Strong'
106 and b and b.t == 'Str' and string.sub(b.text, 1, 3) == '(1)'
107 end
108
109 for i = 1, #inlines-1 do
110 if see_also_p(inlines[i], inlines[i+1]) then
111 inlines[i] = pandoc.Link(inlines[i], inlines[i].c[1].text .. ".1.html")
112 end
113 end
114 end
115
116 -- Transforms text enclosed in backticks into inline code or links.
117 local function rewrite_inline_code (inlines)
118 -- Returns the length of the token stream containing quoted text
119 -- starting from the first token, or nil if no quoted text is
120 -- found.
121 local function inline_quoted (s)
122 if s[1] and s[1].t == 'Str' and string.sub(s[1].text, 1, 1) == "`" then
123 -- Find the end, i.e. the first token containing the closing
124 -- tick (which may be the first token).
125 for i = 1, #s do
126 if s[i] and s[i].t == 'Str' and string.find(s[i].text, "`", 2) then
127 return i
128 end
129 end
130 end
131 end
132
133 -- Given a sequence of tokens, returns the text representation.
134 local function tokens_to_text (s)
135 t = ""
136 for i = 1, #s do
137 if s[i].t == 'Space' then
138 t = t .. ' '
139 else
140 t = t .. s[i].text
141 end
142 end
143 return t
144 end
145
146 -- Given a string subcommand, return the appropriate file
147 -- name.
148 local function subcommand_to_link (s)
149 return string.gsub(s, " ", "-") .. ".1.html"
150 end
151
152 -- Note: as we mutate `inlines` here, we use a while loop to
153 -- iterate over it. Using a for loop doesn't cut it as `#inlines`
154 -- is then evaluated only once when entering the loop for the first
155 -- time.
156 local i = 1
157 while i <= #inlines do
158 local len = inline_quoted(table.pack(table.unpack(inlines, i)))
159 if len then
160 -- First, flatten the matched tokens to a text.
161 local text = tokens_to_text(table.pack(table.unpack(inlines, i, i+len)))
162
163 -- Then, remove the tokens from the AST.
164 for j = math.min(i+len, #inlines), i, -1 do
165 inlines:remove(j)
166 end
167
168 -- We need to create a replacement node.
169 local replacement
170
171 -- See if the quoted text is a reference to a (sub)command.
172 local s, e = string.find(text, "`" .. BINARY .. "[^`]*`")
173 if s then
174 -- If so, we turn it into a link.
175 local sub = string.sub(text, s+1, e-1)
176 replacement = pandoc.Link(sub, subcommand_to_link(sub))
177 else
178 s, e = string.find(text, "`[^`]*`")
179 replacement = pandoc.Code(string.sub(text, s+1, e-1))
180 end
181
182 -- Finally, insert prefix, replacement, and suffix into the
183 -- AST.
184 inlines:insert(i+0, pandoc.Str(string.sub(text, 1, s-1)))
185 inlines:insert(i+1, replacement)
186 inlines:insert(i+2, pandoc.Str(string.sub(text, e+1)))
187 end
188
189 i = i + 1
190 end
191 end
192
193 function Inlines (inlines)
194 rewrite_synopsis(inlines)
195 rewrite_see_also(inlines)
196 rewrite_inline_code(inlines)
197 return inlines
198 end
199
200 -- Turns implicit links into actual links.
201 function Str (s)
202 s = s.text
203 a, b = string.find(s, "<https://[^>]+>")
204 if a and b then
205 target = string.sub(s, a+1, b-1)
206 return pandoc.Inlines {
207 pandoc.Str(string.sub(s, 0, a)),
208 pandoc.Link(target, target),
209 pandoc.Str(string.sub(s, b)),
210 }
211 end
212 end