54 lines
2.0 KiB
Lua
54 lines
2.0 KiB
Lua
-- Render manual tables as labeled lists so shortcuts remain readable at narrow
|
|
-- terminal widths. Keep cell contents as Pandoc elements to preserve formatting.
|
|
function Table(manualTable)
|
|
local columnNames = {}
|
|
local headerRow = manualTable.head.rows[1]
|
|
if headerRow then
|
|
for columnIndex, cell in ipairs(headerRow.cells) do
|
|
columnNames[columnIndex] = pandoc.utils.stringify(cell.contents)
|
|
end
|
|
end
|
|
|
|
local listItems = {}
|
|
local function append_row(row)
|
|
local itemBlocks = pandoc.List()
|
|
for columnIndex, cell in ipairs(row.cells) do
|
|
local cellBlocks = cell.contents:clone()
|
|
if columnIndex == 1 then
|
|
cellBlocks = cellBlocks:walk({
|
|
Plain = function(block)
|
|
return pandoc.Para({pandoc.Strong(block.content)})
|
|
end,
|
|
Para = function(block)
|
|
return pandoc.Para({pandoc.Strong(block.content)})
|
|
end,
|
|
})
|
|
elseif columnNames[columnIndex] then
|
|
local label = pandoc.Strong({pandoc.Str(columnNames[columnIndex] .. ":")})
|
|
local firstBlock = cellBlocks[1]
|
|
if firstBlock and (firstBlock.t == "Plain" or firstBlock.t == "Para") then
|
|
firstBlock.content:insert(1, pandoc.Space())
|
|
firstBlock.content:insert(1, label)
|
|
else
|
|
cellBlocks:insert(1, pandoc.Para({label}))
|
|
end
|
|
end
|
|
itemBlocks:extend(cellBlocks)
|
|
end
|
|
listItems[#listItems + 1] = itemBlocks
|
|
end
|
|
|
|
for _, body in ipairs(manualTable.bodies) do
|
|
for _, row in ipairs(body.head) do
|
|
append_row(row)
|
|
end
|
|
for _, row in ipairs(body.body) do
|
|
append_row(row)
|
|
end
|
|
end
|
|
for _, row in ipairs(manualTable.foot.rows) do
|
|
append_row(row)
|
|
end
|
|
return pandoc.BulletList(listItems)
|
|
end
|