Skip to content

Commit 8e92e98

Browse files
not-my-profiletarleb
authored andcommitted
list-table: switch to advanced table API
1 parent 9157a19 commit 8e92e98

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

list-table/expected.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<table>
2-
<caption></caption>
32
<thead>
43
<tr class="header">
54
<th>Heading row 1, column 1</th>

list-table/list-table.lua

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ else
88
end
99

1010
local function get_colspecs(div_attributes, column_count)
11-
local aligns = {}
12-
local widths = {}
11+
-- list of (align, width) pairs
12+
local colspecs = {}
13+
14+
for i = 1, column_count do
15+
table.insert(colspecs, {pandoc.AlignDefault, nil})
16+
end
1317

1418
if div_attributes.align then
1519
local alignments = {
@@ -18,33 +22,57 @@ local function get_colspecs(div_attributes, column_count)
1822
r = 'AlignRight',
1923
c = 'AlignCenter'
2024
}
25+
local i = 1
2126
for a in div_attributes.align:gmatch('[^,]') do
2227
assert(alignments[a] ~= nil,
2328
"unknown column alignment " .. tostring(a))
24-
table.insert(aligns, alignments[a])
29+
colspecs[i][1] = alignments[a]
30+
i = i + 1
2531
end
2632
div_attributes.align = nil
27-
else
28-
for i = 1, column_count do
29-
table.insert(aligns, pandoc.AlignDefault)
30-
end
3133
end
3234

3335
if div_attributes.widths then
3436
local total = 0
37+
local widths = {}
3538
for w in div_attributes.widths:gmatch('[^,]') do
3639
table.insert(widths, tonumber(w))
3740
total = total + tonumber(w)
3841
end
39-
for i = 1, column_count do widths[i] = widths[i] / total end
40-
div_attributes.widths = nil
41-
else
4242
for i = 1, column_count do
43-
table.insert(widths, 0) -- let pandoc determine col widths
43+
colspecs[i][2] = widths[i] / total
4444
end
45+
div_attributes.widths = nil
4546
end
4647

47-
return aligns, widths
48+
return colspecs
49+
end
50+
51+
local function new_table_head(rows)
52+
return {{}, rows}
53+
end
54+
55+
local function new_table_body(rows)
56+
return {
57+
attr = {},
58+
body = rows,
59+
head = {},
60+
row_head_columns = 0
61+
}
62+
end
63+
64+
local function new_row(cells)
65+
return {{}, cells}
66+
end
67+
68+
local function new_cell(contents)
69+
return {
70+
attr = {},
71+
alignment = pandoc.AlignDefault,
72+
contents = contents,
73+
col_span = 1,
74+
row_span = 1
75+
}
4876
end
4977

5078
local function process(div)
@@ -54,7 +82,8 @@ local function process(div)
5482
local caption = {}
5583

5684
if div.content[1].t == "Para" then
57-
caption = table.remove(div.content, 1).content
85+
local para = table.remove(div.content, 1)
86+
caption = {pandoc.Plain(para.content)}
5887
end
5988

6089
assert(div.content[1].t == "BulletList",
@@ -67,16 +96,26 @@ local function process(div)
6796
assert(#list.content[i] == 1, "expected item to contain only one block")
6897
assert(list.content[i][1].t == "BulletList",
6998
"expected bullet list, found " .. list.content[i][1].t)
70-
table.insert(rows, list.content[i][1].content)
99+
local cells = {}
100+
for _, cell_content in pairs(list.content[i][1].content) do
101+
table.insert(cells, new_cell(cell_content))
102+
end
103+
local row = new_row(cells)
104+
table.insert(rows, row)
71105
end
72106

73-
local headers = table.remove(rows, 1)
74-
local aligns, widths = get_colspecs(div.attr.attributes, #headers)
107+
local colspecs = get_colspecs(div.attr.attributes, #rows[1][2])
108+
local thead_rows = {table.remove(rows, 1)}
75109

76-
local table = pandoc.utils.from_simple_table(
77-
pandoc.SimpleTable(caption, aligns, widths, headers, rows))
78-
table.attr = div.attr
79-
return {table}
110+
local table_foot = {{}, {}};
111+
return pandoc.Table(
112+
{long = caption, short = {}},
113+
colspecs,
114+
new_table_head(thead_rows),
115+
{new_table_body(rows)},
116+
table_foot,
117+
div.attr
118+
)
80119
end
81120

82121
return {{Div = process}}

0 commit comments

Comments
 (0)