| 1 |
local stringify = pandoc.utils.stringify |
| 2 |
local List = pandoc.List |
| 3 |
local DefinitionList = pandoc.DefinitionList |
| 4 |
|
| 5 |
-- We use bold paragraphs for flags (e.g. **-help**) |
| 6 |
local function is_term(b) |
| 7 |
return b.tag == "Para" and b.content[1] and b.content[1].tag == "Strong" |
| 8 |
end |
| 9 |
|
| 10 |
-- GitHub-flavored markdown lacks native definition lists (i.e. ":" syntax). |
| 11 |
-- Therefore, we generate them on the fly to get a better-looking "OPTIONS" |
| 12 |
-- section in man pages and plain-text help without changing the source file. |
| 13 |
function Blocks(blocks) |
| 14 |
local out = List() |
| 15 |
local i = 1 |
| 16 |
local in_opts, lvl = false, nil |
| 17 |
|
| 18 |
while blocks[i] do |
| 19 |
local b = blocks[i] |
| 20 |
i = i + 1 |
| 21 |
|
| 22 |
if b.tag == "Header" then |
| 23 |
-- Apply definition lists inside "OPTIONS" only |
| 24 |
if stringify(b.content) == "OPTIONS" then |
| 25 |
in_opts, lvl = true, b.level |
| 26 |
else |
| 27 |
in_opts = in_opts and b.level > lvl |
| 28 |
end |
| 29 |
out:insert(b) |
| 30 |
elseif in_opts and is_term(b) then |
| 31 |
-- Collect definition for current term |
| 32 |
local def = List() |
| 33 |
while blocks[i] and blocks[i].tag ~= "Header" and not is_term(blocks[i]) do |
| 34 |
def:insert(blocks[i]) |
| 35 |
i = i + 1 |
| 36 |
end |
| 37 |
out:insert(DefinitionList({ { b.content, { def } } })) |
| 38 |
else |
| 39 |
-- Pass through unchanged |
| 40 |
out:insert(b) |
| 41 |
end |
| 42 |
end |
| 43 |
|
| 44 |
return out |
| 45 |
end |