-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_workaround_filter.lua
78 lines (70 loc) · 2.08 KB
/
table_workaround_filter.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- Adapted from <https://github.com/yuki/pandoc-filter-tabularray/blob/f98c9050d0e1bad8cf46e01d49a5c529890861a0/filter.lua>.
function get_rows_data(rows)
local data = ""
for j, row in ipairs(rows) do
for k, cell in ipairs(row.cells) do
data = data .. pandoc.utils.stringify(cell.contents)
if k == #row.cells then
data = data .. " \\\\ \n"
else
data = data .. " & "
end
-- Escape % as \%
data = data:gsub("([^\\])%%", "%1\\%%")
data = data:gsub("^%%", "\\%%")
end
end
return data
end
function generate_tabular(tbl)
local col_specs = tbl.colspecs
local col_specs_latex = ""
for i, col_spec in ipairs(col_specs) do
local align = col_spec[1]
if align == "AlignLeft" then
col_specs_latex = col_specs_latex .. "l"
elseif align == "AlignRight" then
col_specs_latex = col_specs_latex .. "r"
else -- align == "AlignCenter"
col_specs_latex = col_specs_latex .. "c"
end
end
local result = pandoc.List:new({
pandoc.RawBlock("latex", "\\begin{tabular}{" .. col_specs_latex .. "}"),
pandoc.RawBlock("latex", "\\toprule"),
})
-- HEADER
local header_latex = get_rows_data(tbl.head.rows)
result = result
.. pandoc.List:new({ pandoc.RawBlock("latex", header_latex), pandoc.RawBlock("latex", "\\midrule") })
-- ROWS
local rows_latex = ""
for j, tablebody in ipairs(tbl.bodies) do
rows_latex = get_rows_data(tablebody.body)
end
result = result .. pandoc.List:new({ pandoc.RawBlock("latex", rows_latex) })
-- FOOTER
local footer_latex = get_rows_data(tbl.foot.rows)
result = result .. pandoc.List:new({ pandoc.RawBlock("latex", footer_latex) })
result = result
.. pandoc.List:new({
pandoc.RawBlock("latex", "\\bottomrule"),
pandoc.RawBlock("latex", "\\end{tabular}"),
})
return result
end
if FORMAT:match("latex") then
function Table(tbl)
return generate_tabular(tbl)
end
function RawBlock(raw)
if raw.format:match("html") and raw.text:match("%<table") then
blocks = pandoc.read(raw.text, raw.format).blocks
for i, block in ipairs(blocks) do
if block.t == "Table" then
return generate_tabular(block)
end
end
end
end
end