BINARY = os.getenv("BINARY")
if BINARY == nil or BINARY == "" then
error("BINARY environment variable should be set to the binary's name, e.g., sq")
end
function Meta(m)
m.title = "Sequoia PGP Manual Pages"
return m
end
function Header (h)
local function subcommand_to_link (s)
t = ""
for i = 1, #s do
if s[i].t == 'Space' then
t = t .. '-'
else
t = t .. s[i].text
end
end
return t .. ".1.html"
end
if h.c[1].t == 'Str' and h.c[1].text == BINARY then
h.c = pandoc.Link(h.c, subcommand_to_link(h.c))
return h
end
end
local function rewrite_synopsis (inlines)
local function synopsis_p (s)
return s[1] and s[1].t == 'LineBreak'
and s[2] and s[2].t == 'Strong'
and s[2].c[1].text == BINARY and (not s[2].c[2] or s[2].c[2].t == 'Space')
and s[3] and s[3].t == 'Space'
and s[4] and s[4].t == 'Str' and s[4].text == '['
end
local function subcommand_to_link (s)
t = ""
for i = 1, #s do
if s[i].t == 'Space' then
t = t .. '-'
else
t = t .. s[i].text
end
end
return t .. ".1.html"
end
for i = 1, #inlines-4 do
if synopsis_p(table.pack(table.unpack(inlines, i, i + 3))) then
inlines[i+1] = pandoc.Link(inlines[i+1], subcommand_to_link(inlines[i+1].c))
end
end
end
local function rewrite_see_also (inlines)
local function see_also_p (a, b)
return a and a.t == 'Strong'
and b and b.t == 'Str' and string.sub(b.text, 1, 3) == '(1)'
end
for i = 1, #inlines-1 do
if see_also_p(inlines[i], inlines[i+1]) then
inlines[i] = pandoc.Link(inlines[i], inlines[i].c[1].text .. ".1.html")
end
end
end
local function rewrite_inline_code (inlines)
local function inline_quoted (s)
if s[1] and s[1].t == 'Str' and string.sub(s[1].text, 1, 1) == "`" then
for i = 1, #s do
if s[i] and s[i].t == 'Str' and string.find(s[i].text, "`", 2) then
return i
end
end
end
end
local function tokens_to_text (s)
t = ""
for i = 1, #s do
if s[i].t == 'Space' then
t = t .. ' '
else
t = t .. s[i].text
end
end
return t
end
local function subcommand_to_link (s)
return string.gsub(s, " ", "-") .. ".1.html"
end
local i = 1
while i <= #inlines do
local len = inline_quoted(table.pack(table.unpack(inlines, i)))
if len then
local text = tokens_to_text(table.pack(table.unpack(inlines, i, i+len)))
for j = math.min(i+len, #inlines), i, -1 do
inlines:remove(j)
end
local replacement
local s, e = string.find(text, "`" .. BINARY .. "[^`]*`")
if s then
local sub = string.sub(text, s+1, e-1)
replacement = pandoc.Link(sub, subcommand_to_link(sub))
else
s, e = string.find(text, "`[^`]*`")
replacement = pandoc.Code(string.sub(text, s+1, e-1))
end
inlines:insert(i+0, pandoc.Str(string.sub(text, 1, s-1)))
inlines:insert(i+1, replacement)
inlines:insert(i+2, pandoc.Str(string.sub(text, e+1)))
end
i = i + 1
end
end
function Inlines (inlines)
rewrite_synopsis(inlines)
rewrite_see_also(inlines)
rewrite_inline_code(inlines)
return inlines
end
function Str (s)
s = s.text
a, b = string.find(s, "<https://[^>]+>")
if a and b then
target = string.sub(s, a+1, b-1)
return pandoc.Inlines {
pandoc.Str(string.sub(s, 0, a)),
pandoc.Link(target, target),
pandoc.Str(string.sub(s, b)),
}
end
end