1
+ --[[
2
+ MIT License
3
+
4
+ Copyright (c) 2017-2021 pandoc Lua filters contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+
24
+ tables-vrules - adds vertical rules to tables for latex output
25
+
26
+ Copyright: © 2021 Christophe Agathon <[email protected] >
27
+
28
+ License: MIT – see license for details
29
+
30
+ Credits: marijnschraagen for the original Latex hack
31
+
32
+ Output: latex, pdf.
33
+
34
+ Usage: See README.md for details
35
+
36
+ --]]
37
+ local List = require ' pandoc.List'
38
+
39
+ local vars = {}
40
+
41
+ function get_vars (meta )
42
+ vars .vrules = meta [' tables-vrules' ]
43
+ vars .hrules = meta [' tables-hrules' ]
44
+ end
45
+
46
+ function repl_midrules (m1 , m2 )
47
+ if m2 :match (' ^\\ [%w]+rule' ) then
48
+ -- don't double the rule
49
+ return m1 .. m2
50
+ else
51
+ return m1 .. ' \n\\ midrule\n ' .. m2
52
+ end
53
+ end
54
+
55
+ function Table (table )
56
+ local returned_list
57
+ local latex_code = ' '
58
+ local coldef = ' '
59
+ local envdef = ' '
60
+ local new_coldef = ' '
61
+ local end_line = ' '
62
+
63
+ if not vars .vrules and not vars .hrules then return nil end
64
+
65
+ if FORMAT :match ' latex' then
66
+
67
+ -- Get latex code for the whole table
68
+ latex_code = pandoc .write ( pandoc .Pandoc ({table }),' latex' )
69
+
70
+ -- Rewrite column definition to add vertical rules if needed
71
+ if vars .vrules then
72
+ envdef , begdef , coldef , enddef =
73
+ latex_code :match (" ((\\ begin{longtable}%[[^%]]*%]{@{})(.*)(@{}}))" )
74
+
75
+ if coldef then
76
+ if coldef :match (' ^[lrc]+$' ) then
77
+ -- old style
78
+ new_coldef = coldef :gsub (' (.)' , ' |%1' ) .. ' |'
79
+ else
80
+ -- asuming new style
81
+ new_coldef = coldef :gsub (' (>)' , ' |%1' ) .. ' |'
82
+ end
83
+ latex_code = latex_code :sub (envdef :len () + 1 )
84
+ end
85
+ end
86
+
87
+ -- Add \midrules after each row if needed
88
+ if vars .hrules then
89
+ latex_code = latex_code :gsub (' (\\\\\n )([\\ %w]+)' , repl_midrules )
90
+ end
91
+
92
+ -- Return modified latex code as a raw block
93
+ if vars .vrules then
94
+ returned_list = List :new {pandoc .RawBlock (' tex' ,
95
+ begdef .. new_coldef .. enddef ..
96
+ latex_code )}
97
+ else
98
+ returned_list = List :new {pandoc .RawBlock (' tex' , latex_code )}
99
+ end
100
+ end
101
+ return returned_list
102
+ end
103
+
104
+ function Meta (meta )
105
+ -- We have to add this since Pandoc doesn't because there are no
106
+ -- table anymore in the AST. We converted them in RawBlocks
107
+
108
+ if not vars .vrules and not vars .hrules then return nil end
109
+ includes = [[
110
+ %begin tables-vrules.lua
111
+ \usepackage{longtable,booktabs,array}
112
+ \usepackage{calc} % for calculating minipage widths
113
+ % Correct order of tables after \paragraph or \subparagraph
114
+ \usepackage{etoolbox}
115
+ \makeatletter
116
+ \patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
117
+ \makeatother
118
+ % Allow footnotes in longtable head/foot
119
+ \IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
120
+ \makesavenoteenv{longtable}
121
+ \setlength{\aboverulesep}{0pt}
122
+ \setlength{\belowrulesep}{0pt}
123
+ \renewcommand{\arraystretch}{1.3}
124
+ %end tables-vrules.lua
125
+ ]]
126
+
127
+ if meta [' header-includes' ] then
128
+ table.insert (meta [' header-includes' ], pandoc .RawBlock (' tex' , includes ))
129
+ else
130
+ meta [' header-includes' ] = List :new {pandoc .RawBlock (' tex' , includes )}
131
+ end
132
+
133
+ return meta
134
+ end
135
+
136
+ return {{Meta = get_vars }, {Table = Table }, {Meta = Meta }}
0 commit comments