Files
Desktop2.0/assets/table-to-list.lua
T

78 lines
3.0 KiB
Lua

-- Filtr dla Pandoc 3.1.x (EPUB Table to List)
-- Rozwiązuje problem struktury TableBody.body
function Table(el)
-- 1. Obsługuj tylko formaty EPUB
if not FORMAT:match('epub') then
return nil
end
io.stderr:write("--------------------------------------\n")
io.stderr:write("INFO: Rozpoczynam transformację tabeli...\n")
local headers = {}
local result_blocks = {}
-- 2. Pobierz nagłówki (TableHead -> rows -> cells)
if el.head and el.head.rows and #el.head.rows > 0 then
for _, cell in ipairs(el.head.rows[1].cells) do
table.insert(headers, pandoc.utils.stringify(cell.contents))
end
io.stderr:write("DEBUG: Znaleziono nagłówki: " .. #headers .. "\n")
end
-- 3. Przetwarzaj ciała tabeli (TableBody)
-- W Pandoc 3.x wiersze danych są w body.body, a nie body.rows!
local total_rows = 0
for _, table_body in ipairs(el.bodies or {}) do
-- table_body.body to lista wierszy (Rows)
local rows = table_body.body or {}
for _, row in ipairs(rows) do
total_rows = total_rows + 1
local list_items = {}
-- row.cells to lista komórek (Cells)
for i, cell in ipairs(row.cells or {}) do
local label = headers[i] or "Informacja"
local label_strong = pandoc.Strong({pandoc.Str(label .. ": ")})
-- Pobieramy zawartość komórki (bloków)
local cell_content = cell.contents
-- Budujemy nowy akapit z nagłówkiem na początku
local new_inlines = {label_strong}
-- Wyciągamy istniejące inliny z pierwszego bloku komórki
if #cell_content > 0 then
for _, block in ipairs(cell_content) do
if block.content then
for _, inline in ipairs(block.content) do
table.insert(new_inlines, inline)
end
else
-- Jeśli blok to np. Plain bez pola content
table.insert(new_inlines, pandoc.Str(pandoc.utils.stringify(block)))
end
end
end
table.insert(list_items, {pandoc.Plain(new_inlines)})
end
if #list_items > 0 then
table.insert(result_blocks, pandoc.BulletList(list_items))
table.insert(result_blocks, pandoc.HorizontalRule())
end
end
end
if #result_blocks > 0 then
io.stderr:write("SUKCES: Przetworzono " .. total_rows .. " wierszy tabeli.\n")
io.stderr:write("--------------------------------------\n")
return result_blocks
else
io.stderr:write("BŁĄD: Nie znaleziono wierszy w strukturze el.bodies[].body\n")
io.stderr:write("--------------------------------------\n")
return nil
end
end