diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2020-09-21 00:48:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-20 15:48:31 -0700 |
commit | acbea6b8c610dba4b63c0f6063c51b26ab9d2b76 (patch) | |
tree | 2140ee75afafc3d87ca3d50c4cd989efec221a51 /test | |
parent | b2decdfd1370b5291a6c1be758d4e0bfeaf9fcc7 (diff) | |
download | pandoc-acbea6b8c610dba4b63c0f6063c51b26ab9d2b76.tar.gz |
Lua filters: add SimpleTable for backwards compatibility (#6575)
A new type `SimpleTable` is made available to Lua filters. It is
similar to the `Table` type in pandoc versions before 2.10;
conversion functions from and to the new Table type are provided.
Old filters using tables now require minimal changes and can use,
e.g.,
if PANDOC_VERSION > {2,10,1} then
pandoc.Table = pandoc.SimpleTable
end
and
function Table (tbl)
tbl = pandoc.utils.to_simple_table(tbl)
…
return pandoc.utils.from_simple_table(tbl)
end
to work with the current pandoc version.
Diffstat (limited to 'test')
-rw-r--r-- | test/lua/module/pandoc-utils.lua | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test/lua/module/pandoc-utils.lua b/test/lua/module/pandoc-utils.lua index 963e70686..9bd903f2d 100644 --- a/test/lua/module/pandoc-utils.lua +++ b/test/lua/module/pandoc-utils.lua @@ -90,4 +90,70 @@ return { assert.is_falsy(pcall(utils.to_roman_numeral, 'not a number')) end) }, + + group 'to_simple_table' { + test('convertes Table', function () + function simple_cell (blocks) + return { + attr = pandoc.Attr(), + alignment = "AlignDefault", + contents = blocks, + col_span = 1, + row_span = 1, + } + end + local tbl = pandoc.Table( + {long = {pandoc.Plain { + pandoc.Str "the", pandoc.Space(), pandoc.Str "caption"}}}, + {{pandoc.AlignDefault, nil}}, + {pandoc.Attr(), {{pandoc.Attr(), {simple_cell{pandoc.Plain "head1"}}}}}, + {{ + attr = pandoc.Attr(), + body = {{pandoc.Attr(), {simple_cell{pandoc.Plain "cell1"}}}}, + head = {}, + row_head_columns = 0 + }}, + {pandoc.Attr(), {}}, + pandoc.Attr() + ) + local stbl = utils.to_simple_table(tbl) + assert.are_equal('SimpleTable', stbl.t) + assert.are_equal('head1', utils.stringify(stbl.headers[1])) + assert.are_equal('cell1', utils.stringify(stbl.rows[1][1])) + assert.are_equal('the caption', utils.stringify(pandoc.Span(stbl.caption))) + end), + test('fails on para', function () + assert.is_falsy(pcall(utils.to_simple_table, pandoc.Para "nope")) + end), + }, + group 'from_simple_table' { + test('converts SimpleTable to Table', function () + local caption = {pandoc.Str "Overview"} + local aligns = {pandoc.AlignDefault, pandoc.AlignDefault} + local widths = {0, 0} -- let pandoc determine col widths + local headers = { + {pandoc.Plain "Language"}, + {pandoc.Plain "Typing"} + } + local rows = { + {{pandoc.Plain "Haskell"}, {pandoc.Plain "static"}}, + {{pandoc.Plain "Lua"}, {pandoc.Plain "Dynamic"}}, + } + local simple_table = pandoc.SimpleTable( + caption, + aligns, + widths, + headers, + rows + ) + local tbl = utils.from_simple_table(simple_table) + assert.are_equal("Table", tbl.t) + assert.are_same( + {pandoc.Plain(caption)}, + tbl.caption.long + ) + -- reversible + assert.are_same(simple_table, utils.to_simple_table(tbl)) + end), + } } |