|
7 | 7 | error("pandoc version >=2.11 is required")
|
8 | 8 | end
|
9 | 9 |
|
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) |
33 | 11 | local aligns = {}
|
34 | 12 | local widths = {}
|
35 | 13 |
|
36 |
| - local headers = table.remove(rows, 1) |
37 |
| - |
38 |
| - if div.attr.attributes.align then |
| 14 | + if div_attributes.align then |
39 | 15 | local alignments = {
|
40 | 16 | d = 'AlignDefault',
|
41 | 17 | l = 'AlignLeft',
|
42 | 18 | r = 'AlignRight',
|
43 | 19 | c = 'AlignCenter'
|
44 | 20 | }
|
45 |
| - for a in div.attr.attributes.align:gmatch('[^,]') do |
| 21 | + for a in div_attributes.align:gmatch('[^,]') do |
46 | 22 | assert(alignments[a] ~= nil,
|
47 | 23 | "unknown column alignment " .. tostring(a))
|
48 | 24 | table.insert(aligns, alignments[a])
|
49 | 25 | end
|
50 |
| - div.attr.attributes.align = nil |
| 26 | + div_attributes.align = nil |
51 | 27 | 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 |
53 | 31 | end
|
54 | 32 |
|
55 |
| - if div.attr.attributes.widths then |
| 33 | + if div_attributes.widths then |
56 | 34 | local total = 0
|
57 |
| - for w in div.attr.attributes.widths:gmatch('[^,]') do |
| 35 | + for w in div_attributes.widths:gmatch('[^,]') do |
58 | 36 | table.insert(widths, tonumber(w))
|
59 | 37 | total = total + tonumber(w)
|
60 | 38 | 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 |
63 | 41 | else
|
64 |
| - for i = 1, #headers do |
| 42 | + for i = 1, column_count do |
65 | 43 | table.insert(widths, 0) -- let pandoc determine col widths
|
66 | 44 | end
|
67 | 45 | end
|
68 | 46 |
|
| 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 | + |
69 | 76 | local table = pandoc.utils.from_simple_table(
|
70 | 77 | pandoc.SimpleTable(caption, aligns, widths, headers, rows))
|
71 | 78 | table.attr = div.attr
|
|
0 commit comments