| 1 |
\RequirePackage{expl3} |
| 2 |
\RequirePackage{xparse} |
| 3 |
\RequirePackage{luatexbase} |
| 4 |
\RequirePackage{iftex} |
| 5 |
|
| 6 |
\RequireLuaTeX |
| 7 |
|
| 8 |
\ProvidesExplPackage {luaprogtable}{08/06/2020}{1.0}{Programmatic table interface} |
| 9 |
|
| 10 |
\makeatletter |
| 11 |
\savecatcodetable 20996 |
| 12 |
|
| 13 |
\directlua{ |
| 14 |
require~"luaprogtable.lua" |
| 15 |
} |
| 16 |
|
| 17 |
\tl_new:N \g__luaprogtable_tmpa_tl |
| 18 |
\tl_new:N \g__luaprogtable_tmpb_tl |
| 19 |
\tl_new:N \g__luaprogtable_tmpc_tl |
| 20 |
|
| 21 |
\str_new:N \g__luaprogtable_tmpa_str |
| 22 |
\str_new:N \g__luaprogtable_tmpb_str |
| 23 |
\str_new:N \g__luaprogtable_tmpc_str |
| 24 |
|
| 25 |
\int_new:N \g__luaprogtable_tmpa_int |
| 26 |
\int_new:N \g__luaprogtable_tmpb_int |
| 27 |
\int_new:N \g__luaprogtable_tmpc_int |
| 28 |
|
| 29 |
\cs_set:Npn \__luaprogtable_lua_table_item:nn #1#2 { |
| 30 |
["#1"]="#2" |
| 31 |
} |
| 32 |
|
| 33 |
\seq_new:N \g__luaprogtable_prop_item_seq |
| 34 |
|
| 35 |
\cs_set:Npn \__luaprogtable_add_lua_table_item:nn #1#2 { |
| 36 |
\str_gset:Nx \g__luaprogtable_tmpa_str {\luaescapestring{\tl_to_str:n {#1}}} |
| 37 |
\str_gset:Nx \g__luaprogtable_tmpb_str {\luaescapestring{\tl_to_str:n {#2}}} |
| 38 |
\seq_gput_right:Nx \g__luaprogtable_prop_item_seq { |
| 39 |
\exp_args:NVV \__luaprogtable_lua_table_item:nn \g__luaprogtable_tmpa_str \g__luaprogtable_tmpb_str |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
\str_new:N \g__luaprogtable_lua_table_str |
| 44 |
|
| 45 |
\cs_set:Npn \__luaprogtable_prop_to_lua_table:N #1 { |
| 46 |
\seq_gclear:N \g__luaprogtable_prop_item_seq |
| 47 |
\prop_map_function:NN #1 \__luaprogtable_add_lua_table_item:nn |
| 48 |
\str_gclear:N \g__luaprogtable_lua_table_str |
| 49 |
\str_gput_right:NV \g__luaprogtable_lua_table_str \c_left_brace_str |
| 50 |
\str_gput_right:Nx \g__luaprogtable_lua_table_str {\seq_use:Nn \g__luaprogtable_prop_item_seq {,~}} |
| 51 |
\str_gput_right:NV \g__luaprogtable_lua_table_str \c_right_brace_str |
| 52 |
} |
| 53 |
|
| 54 |
\prop_new:N \g__luaprogtable_new_prop |
| 55 |
\keys_define:nn { __luaprogtable_new } |
| 56 |
{ |
| 57 |
backend .prop_gput:N = \g__luaprogtable_new_prop, |
| 58 |
ncols .prop_gput:N = \g__luaprogtable_new_prop, |
| 59 |
nrows .prop_gput:N = \g__luaprogtable_new_prop, |
| 60 |
default~before~line .prop_gput:N = \g__luaprogtable_new_prop, |
| 61 |
default~after~line .prop_gput:N = \g__luaprogtable_new_prop, |
| 62 |
default~after~spacing .prop_gput:N = \g__luaprogtable_new_prop, |
| 63 |
input~method .prop_gput:N = \g__luaprogtable_new_prop |
| 64 |
} |
| 65 |
|
| 66 |
\str_new:N \g__luaprogtable_lua_cur_table_str |
| 67 |
\DeclareDocumentCommand{\LPTSetCurrentTable}{m}{ |
| 68 |
\directlua{ |
| 69 |
local _test_table = _lpt_get_table("\luaescapestring{#1}") |
| 70 |
} |
| 71 |
\str_gset:Nx \g__luaprogtable_lua_cur_table_str {\luaescapestring{#1}} |
| 72 |
} |
| 73 |
|
| 74 |
\newcommand{\LPTGetCurrentTable}{ |
| 75 |
\str_use:N \g__luaprogtable_lua_cur_table_str |
| 76 |
} |
| 77 |
|
| 78 |
\DeclareDocumentCommand{\LPTNewTable}{mmmO{}}{ |
| 79 |
\prop_gclear:N \g__luaprogtable_new_prop |
| 80 |
\prop_gput:Nnn \g__luaprogtable_new_prop {backend} {tabular} |
| 81 |
\prop_gput:Nnn \g__luaprogtable_new_prop {nrows} {0} |
| 82 |
\prop_gput:Nnn \g__luaprogtable_new_prop {default~before~line} {} |
| 83 |
\prop_gput:Nnn \g__luaprogtable_new_prop {default~after~line} {} |
| 84 |
\prop_gput:Nnn \g__luaprogtable_new_prop {default~after~spacing} {} |
| 85 |
\prop_gput:Nnn \g__luaprogtable_new_prop {input~method} {stringbuffer} |
| 86 |
|
| 87 |
\keys_set:nn { __luaprogtable_new } {#4} |
| 88 |
|
| 89 |
\prop_gput:Nnn \g__luaprogtable_new_prop {table~name} {#1} |
| 90 |
\prop_gput:Nnn \g__luaprogtable_new_prop {ncols} {#2} |
| 91 |
\prop_gput:Nnn \g__luaprogtable_new_prop {table~preamble} {#3} |
| 92 |
\prop_gput:Nnn \g__luaprogtable_new_prop {default~value} {} |
| 93 |
|
| 94 |
\__luaprogtable_prop_to_lua_table:N \g__luaprogtable_new_prop |
| 95 |
|
| 96 |
\directlua{ |
| 97 |
local~_tmp_table = \str_use:N \g__luaprogtable_lua_table_str; |
| 98 |
lpt_new_table(_tmp_table); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
\newcommand{\LPTUseTable}{ |
| 103 |
\directlua{ |
| 104 |
lpt_use_table("\LPTGetCurrentTable"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
\DeclareDocumentEnvironment{lptview}{m}{ |
| 109 |
\directlua{ |
| 110 |
_lpt_register_verbatim([[\string\end{lptview}]]) |
| 111 |
} |
| 112 |
}{ |
| 113 |
% now that verbatim material is passed to lua, call corresponding handler |
| 114 |
\directlua{ |
| 115 |
lpt_table_view("\LPTGetCurrentTable", "\luaescapestring{#1}") |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
\DeclareDocumentEnvironment{lptfill}{m}{ |
| 120 |
\directlua{ |
| 121 |
_lpt_register_verbatim([[\string\end{lptfill}]]) |
| 122 |
} |
| 123 |
}{ |
| 124 |
\directlua{ |
| 125 |
lpt_table_fill("\LPTGetCurrentTable", "\luaescapestring{#1}") |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
\DeclareDocumentCommand{\LPTFill}{mm}{ |
| 130 |
\directlua{ |
| 131 |
lpt_table_fill("\LPTGetCurrentTable", "\luaescapestring{#1}", "\luaescapestring{\tl_to_str:n {#2}}") |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
\prop_new:N \g__luaprogtable_row_prop |
| 136 |
\keys_define:nn { __luaprogtable_row } |
| 137 |
{ |
| 138 |
before~line .prop_gput:N = \g__luaprogtable_row_prop, |
| 139 |
after~line .prop_gput:N = \g__luaprogtable_row_prop, |
| 140 |
after~spacing .prop_gput:N = \g__luaprogtable_row_prop |
| 141 |
} |
| 142 |
|
| 143 |
\DeclareDocumentCommand{\LPTSetRowProp}{mm}{ |
| 144 |
\prop_gclear:N \g__luaprogtable_row_prop |
| 145 |
\keys_set:nn { __luaprogtable_row } {#2} |
| 146 |
|
| 147 |
\__luaprogtable_prop_to_lua_table:N \g__luaprogtable_row_prop |
| 148 |
\directlua{ |
| 149 |
local~_tmp_table = \str_use:N \g__luaprogtable_lua_table_str; |
| 150 |
lpt_set_row_prop("\LPTGetCurrentTable", "\luaescapestring{#1}", _tmp_table) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
\DeclareDocumentCommand{\LPTAddRow}{O{}}{ |
| 155 |
\prop_gclear:N \g__luaprogtable_row_prop |
| 156 |
\keys_set:nn { __luaprogtable_row } {#1} |
| 157 |
|
| 158 |
\__luaprogtable_prop_to_lua_table:N \g__luaprogtable_row_prop |
| 159 |
\directlua{ |
| 160 |
local~_tmp_table = \str_use:N \g__luaprogtable_lua_table_str; |
| 161 |
lpt_add_row("\LPTGetCurrentTable", _tmp_table) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
\DeclareDocumentCommand{\LPTSetCell}{mO{1,1}m}{ |
| 166 |
\directlua{ |
| 167 |
local~_table = _lpt_get_table("\LPTGetCurrentTable"); |
| 168 |
local~_index = _table:fix_cell_index("\luaescapestring{#1}"); |
| 169 |
local~_shape = _lpt_parse_shape("\luaescapestring{#2}"); |
| 170 |
_table:set_cell(_index[1], _index[2], "\luaescapestring{ \tl_to_str:n {#3} }", _shape) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
\newcommand{\LPTGetTableShape}{ |
| 175 |
\directlua{ |
| 176 |
lpt_get_table_shape("\LPTGetCurrentTable") |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
\ExplSyntaxOff |
| 181 |
|
| 182 |
\newcommand{\LPTGetTableNames}{% |
| 183 |
\directlua{ |
| 184 |
local _names = {} |
| 185 |
for key, val in pairs(lpt_info) do |
| 186 |
table.insert(_names, key) |
| 187 |
end |
| 188 |
table.sort(_names) |
| 189 |
local _ret = table.concat(_names, ",") |
| 190 |
tex.print(_ret) |
| 191 |
}% |
| 192 |
} |
| 193 |
|
| 194 |
\newcommand{\LPTGetCellData}[1]{% |
| 195 |
\directlua{ |
| 196 |
local _table = _lpt_get_table("\LPTGetCurrentTable"); |
| 197 |
local _index = _table:fix_cell_index("\luaescapestring{#1}"); |
| 198 |
local _cell = _table:get_cell(_index[1], _index[2]); |
| 199 |
tex.print([[\string\unexpanded{]] .. _cell.data .. "}") |
| 200 |
}% |
| 201 |
} |
| 202 |
|
| 203 |
\newcommand{\LPTGetCellMetaIndex}[2]{% |
| 204 |
\directlua{ |
| 205 |
local _table = _lpt_get_table("\LPTGetCurrentTable"); |
| 206 |
local _index = _table:fix_cell_index("\luaescapestring{#2}"); |
| 207 |
local _cell = _table:get_cell(_index[1], _index[2]); |
| 208 |
local _field = "\luaescapestring{#1}" |
| 209 |
local _ret = rawget(_cell, _field) |
| 210 |
if _ret == nil then |
| 211 |
tex.print(20996, [[\string\c_novalue_tl]]) |
| 212 |
else |
| 213 |
tex.print("{".. tostring(_ret[1]) .. "}{".. tostring(_ret[2]) .. "}") |
| 214 |
end |
| 215 |
}% |
| 216 |
} |
| 217 |
|
| 218 |
\newcommand{\LPTGetCellShape}[1]{% |
| 219 |
\LPTGetCellMetaIndex{shape}{#1}% |
| 220 |
} |
| 221 |
|
| 222 |
\newcommand{\LPTGetCellParent}[1]{% |
| 223 |
\LPTGetCellMetaIndex{parent}{#1}% |
| 224 |
} |
| 225 |
|
| 226 |
\newcommand{\LPTDeleteTable}[1]{ |
| 227 |
\directlua{ |
| 228 |
local _name = "\luaescapestring{#1}" |
| 229 |
local _table = _lpt_get_table(_name) |
| 230 |
lpt_info[_name] = nil |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
\makeatother |
| 236 |
|
| 237 |
\endinput |