forked from dialoa/dialectica-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongtable-to-xtab.lua
118 lines (89 loc) · 3.39 KB
/
longtable-to-xtab.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--- # Longtable-to-xtab - switch LaTeX table outputs from longtable to xtab.
--
-- This Lua filter for Pandoc converts Pandoc's default `longtable` output
-- of tables in LaTeX with `xtab` tables.
--
-- @author Julien Dutant <[email protected]>
-- @copyright (c) 2021 Julien Dutant
-- @license MIT - see LICENSE file for details.
-- @release 1.0
--- type: pandoc-friendly type function
-- pandoc.utils.type is only defined in Pandoc >= 2.17
-- if it isn't, we extend Lua's type function to give the same values
-- as pandoc.utils.type on Meta objects: Inlines, Inline, Blocks, Block,
-- string and booleans
-- Caution: not to be used on non-Meta Pandoc elements, the
-- results will differ (only 'Block', 'Blocks', 'Inline', 'Inlines' in
-- >=2.17, the .t string in <2.17).
local type = pandoc.utils.type or function (obj)
local tag = type(obj) == 'table' and obj.t and obj.t:gsub('^Meta', '')
return tag and tag ~= 'Map' and tag or type(obj)
end
--- Add a block to the document's header-includes meta-data field.
-- @param meta the document's metadata block
-- @param block Pandoc block element (e.g. RawBlock or Para) to be added to header-includes
-- @return meta the modified metadata block
local function add_header_includes(meta, block)
local header_includes
-- make meta['header-includes'] a list if needed
if meta['header-includes'] and type(meta['header-includes']) == 'List' then
header_includes = meta['header-includes']
else
header_includes = pandoc.MetaList{meta['header-includes']}
end
-- insert `block` in header-includes and add it to `meta`
header_includes[#header_includes + 1] =
pandoc.MetaBlocks{block}
meta['header-includes'] = header_includes
return meta
end
--- Main filter
local filter = {
Meta = function (element)
local latex_code = [[
\usepackage{xtab}
\renewenvironment{longtable}{%
\begin{center}\begin{xtabular}%
}{%
\end{xtabular}\end{center}%
}
\renewcommand{\caption}[1]{}
\renewcommand{\endhead}{}
]]
add_header_includes(element, pandoc.RawBlock('latex', latex_code))
return element
end,
Table = function (element)
if element.caption and #element.caption['long'] > 0 then
local result = pandoc.List:new({})
local inlines = pandoc.List:new({})
inlines:insert(pandoc.RawInline('latex', '\\tablecaption{'))
inlines:extend(pandoc.utils.blocks_to_inlines(element.caption['long']))
inlines:insert(pandoc.RawInline('latex', '}'))
result:insert(pandoc.Plain(inlines))
-- place the table
result:insert(element)
-- if the table has both header and caption we need to hide the
-- duplicate header. We turn `\endfirsthead` into `\iffalse`
-- and `\endhead` into `\fi`. The latter must be present
-- when `\iffalse` is encountered, so we use `\let` for the
-- latter and `\renewcommand` for the former.
-- we wrap the whole in `{...}` to avoid affecting `\endhead`
-- down the line.
if #element.head[2] > 0 then
latex_pre = [[{
{\renewcommand{\endfirsthead}{\iffalse}
\let\endhead\fi
]]
latex_post = '}'
result:insert(1, pandoc.RawBlock('latex', latex_pre))
result:insert(pandoc.RawBlock('latex', latex_post))
end
return result
end
end
}
-- return filter if targetting LaTeX
if FORMAT:match('latex') then
return {filter}
end