| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
function Meta(m) |
| 17 |
m.title = "Sequoia PGP Manual Pages" |
| 18 |
return m |
| 19 |
end |
| 20 |
|
| 21 |
|
| 22 |
function Header (h) |
| 23 |
|
| 24 |
|
| 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 |
|
| 44 |
local function rewrite_synopsis (inlines) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 72 |
|
| 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 |
|
| 93 |
local function rewrite_see_also (inlines) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 117 |
local function rewrite_inline_code (inlines) |
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 147 |
|
| 148 |
local function subcommand_to_link (s) |
| 149 |
return string.gsub(s, " ", "-") .. ".1.html" |
| 150 |
end |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 161 |
local text = tokens_to_text(table.pack(table.unpack(inlines, i, i+len))) |
| 162 |
|
| 163 |
|
| 164 |
for j = math.min(i+len, #inlines), i, -1 do |
| 165 |
inlines:remove(j) |
| 166 |
end |
| 167 |
|
| 168 |
|
| 169 |
local replacement |
| 170 |
|
| 171 |
|
| 172 |
local s, e = string.find(text, "`" .. BINARY .. "[^`]*`") |
| 173 |
if s then |
| 174 |
|
| 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 |
|
| 183 |
|
| 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 |
|
| 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 |