Skip to content

Commit 9157a19

Browse files
not-my-profiletarleb
authored andcommitted
list-table: refactor out colspecs helper function
1 parent 3e5c72d commit 9157a19

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

list-table/list-table.lua

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,72 @@ else
77
error("pandoc version >=2.11 is required")
88
end
99

10-
local function process(div)
11-
if div.attr.classes[1] ~= "list-table" then return nil end
12-
table.remove(div.attr.classes, 1)
13-
14-
local caption = {}
15-
16-
if div.content[1].t == "Para" then
17-
caption = table.remove(div.content, 1).content
18-
end
19-
20-
assert(div.content[1].t == "BulletList",
21-
"expected bullet list, found " .. div.content[1].t)
22-
local list = div.content[1]
23-
24-
local rows = {}
25-
26-
for i = 1, #list.content do
27-
assert(#list.content[i] == 1, "expected item to contain only one block")
28-
assert(list.content[i][1].t == "BulletList",
29-
"expected bullet list, found " .. list.content[i][1].t)
30-
table.insert(rows, list.content[i][1].content)
31-
end
32-
10+
local function get_colspecs(div_attributes, column_count)
3311
local aligns = {}
3412
local widths = {}
3513

36-
local headers = table.remove(rows, 1)
37-
38-
if div.attr.attributes.align then
14+
if div_attributes.align then
3915
local alignments = {
4016
d = 'AlignDefault',
4117
l = 'AlignLeft',
4218
r = 'AlignRight',
4319
c = 'AlignCenter'
4420
}
45-
for a in div.attr.attributes.align:gmatch('[^,]') do
21+
for a in div_attributes.align:gmatch('[^,]') do
4622
assert(alignments[a] ~= nil,
4723
"unknown column alignment " .. tostring(a))
4824
table.insert(aligns, alignments[a])
4925
end
50-
div.attr.attributes.align = nil
26+
div_attributes.align = nil
5127
else
52-
for i = 1, #headers do table.insert(aligns, pandoc.AlignDefault) end
28+
for i = 1, column_count do
29+
table.insert(aligns, pandoc.AlignDefault)
30+
end
5331
end
5432

55-
if div.attr.attributes.widths then
33+
if div_attributes.widths then
5634
local total = 0
57-
for w in div.attr.attributes.widths:gmatch('[^,]') do
35+
for w in div_attributes.widths:gmatch('[^,]') do
5836
table.insert(widths, tonumber(w))
5937
total = total + tonumber(w)
6038
end
61-
for i = 1, #headers do widths[i] = widths[i] / total end
62-
div.attr.attributes.widths = nil
39+
for i = 1, column_count do widths[i] = widths[i] / total end
40+
div_attributes.widths = nil
6341
else
64-
for i = 1, #headers do
42+
for i = 1, column_count do
6543
table.insert(widths, 0) -- let pandoc determine col widths
6644
end
6745
end
6846

47+
return aligns, widths
48+
end
49+
50+
local function process(div)
51+
if div.attr.classes[1] ~= "list-table" then return nil end
52+
table.remove(div.attr.classes, 1)
53+
54+
local caption = {}
55+
56+
if div.content[1].t == "Para" then
57+
caption = table.remove(div.content, 1).content
58+
end
59+
60+
assert(div.content[1].t == "BulletList",
61+
"expected bullet list, found " .. div.content[1].t)
62+
local list = div.content[1]
63+
64+
local rows = {}
65+
66+
for i = 1, #list.content do
67+
assert(#list.content[i] == 1, "expected item to contain only one block")
68+
assert(list.content[i][1].t == "BulletList",
69+
"expected bullet list, found " .. list.content[i][1].t)
70+
table.insert(rows, list.content[i][1].content)
71+
end
72+
73+
local headers = table.remove(rows, 1)
74+
local aligns, widths = get_colspecs(div.attr.attributes, #headers)
75+
6976
local table = pandoc.utils.from_simple_table(
7077
pandoc.SimpleTable(caption, aligns, widths, headers, rows))
7178
table.attr = div.attr

0 commit comments

Comments
 (0)