Debian Codegraph @

texmf-dist/tex/lualatex/luaprogtable/luaprogtable.lua

raw
1 require "luaprogtable-parse.lua"
2 require "luaprogtable-stringbuffer.lua"
3
4 TeXTable = {}
5 TeXTable.__index = TeXTable
6
7 function TeXTable:new()
8 local ret = {
9 rows = {}
10 }
11 setmetatable(ret, TeXTable)
12 return ret
13 end
14
15 TeXTableCell = {}
16 TeXTableCell.__index = TeXTableCell
17
18 local _std_shape = {1, 1}
19
20 function TeXTableCell:new(default_val)
21 local ret = {
22 data=default_val,
23 shape=_std_shape,
24 parent=nil
25 }
26 setmetatable(ret, TeXTableCell)
27 return ret
28 end
29
30
31 TeXTableRow = {}
32 TeXTableRow.__index = TeXTableRow
33
34 function TeXTableRow:new(tex_table)
35
36 local ncols = tex_table.ncols
37 local default_val = rawget(tex_table, "default value")
38
39 local cells = {}
40
41 for i=1,ncols do
42 table.insert(cells, TeXTableCell:new(default_val))
43 end
44
45 local ret = {
46 ["before line"] = "",
47 ["after line"] = rawget(tex_table, "default after line"),
48 ["after spacing"] = rawget(tex_table, "default after spacing"),
49 ["cells"] = cells
50 }
51
52 setmetatable(ret, TeXTableRow)
53 return ret
54 end
55
56
57 function TeXTable:resize(nrows, ncols)
58 local now_nrows = #self.rows
59 local now_ncols = self.ncols
60
61 if nrows < now_nrows or ncols < now_ncols then
62 raise_error("'TeXTable:resize' doesn not support table truncation")
63 end
64
65 local nrows_delta = nrows - now_nrows
66 local ncols_delta = ncols - now_ncols
67
68 -- change column for existing rows
69 self.ncols = ncols
70 for i=1,now_nrows do
71 local row = self.rows[i]
72 for j=1,ncols_delta do
73 table.insert(row.cells, TeXTableCell:new(rawget(self, "default value")))
74 end
75 end
76
77 -- add new rows
78 for i=1,nrows_delta do
79 table.insert(self.rows, TeXTableRow:new(self))
80 end
81 end
82
83 function TeXTable:to_string(no_linebreak)
84 local ret = {}
85 local no_linebreak = no_linebreak or false
86 local lb = "\n"
87 if no_linebreak then
88 lb = " "
89 end
90
91 table.insert(ret, string.format([[\begin{%s}{%s}]], self.backend, rawget(self, "table preamble")))
92
93 local nrows = #self.rows
94 for i=1,nrows do
95
96 local row = self.rows[i]
97
98 local cell_data = {}
99 local j = 1
100
101 while j <= self.ncols do
102 local cell = row.cells[j]
103
104 local next_j = nil
105
106 if cell.shape ~= nil then
107 table.insert(cell_data, cell.data)
108 next_j = j + cell.shape[2]
109 else
110 local parent_row = cell.parent[1]
111 local parent_col = cell.parent[2]
112 local parent_cell = self:get_cell(parent_row, parent_col)
113 table.insert(cell_data, "")
114 next_j = j + parent_cell.shape[2]
115 end
116
117 j = next_j
118 end
119
120 local row_str = ""
121
122 if string.len(rawget(row, "before line")) > 0 then
123 row_str = rawget(row, "before line") .. lb .. row_str
124 end
125
126 row_str = row_str .. table.concat(cell_data, " & ") .. [[ \\]]
127
128 if string.len(rawget(row, "after spacing")) > 0 then
129 row_str = row_str .. "[" .. rawget(row, "after spacing") .. "]"
130 end
131
132 row_str = row_str .. rawget(row, "after line")
133
134 table.insert(ret, row_str)
135 end
136
137 table.insert(ret, string.format([[\end{%s}]], self.backend))
138
139 return table.concat(ret, lb)
140
141 end
142
143
144 function TeXTable:get_cell(row_id, cell_id)
145 local row = self.rows[row_id]
146 if row == nil then
147 raise_error(string.format("row id '%d' is invalid for table '%s'", row_id, rawget(self, "table name")))
148 end
149 local cell = row.cells[cell_id]
150 if cell == nil then
151 raise_error(string.format("cell location '(%d,%d)' is invalid for table '%s'", row_id, cell_id, rawget(self, "table name")))
152 end
153 return cell
154 end
155
156 function TeXTable:set_cell(row_id, cell_id, data, shape)
157 local cell = self:get_cell(row_id, cell_id)
158
159 -- check if we can modify this cell
160 if cell.parent ~= nil then
161 local parent_1 = cell.parent[1]
162 local parent_2 = cell.parent[2]
163 raise_error(string.format("cell location '(%d,%d)' of table '%s' is not acceesible because it is overlapped by cell '(%d,%d)'", row_id, cell_id, rawget(self, "table name"), parent_1, parent_2))
164 end
165
166
167 -- check cell boundary
168 local max_row = row_id + shape[1] - 1
169 local max_col = cell_id + shape[2] - 1
170 if max_row > #self.rows or max_col > self.ncols then
171 raise_error(string.format("cell '(%d,%d)' is too large for table '%s'", row_id, col_id, rawget(self, "table name")))
172 end
173
174
175 local old_shape = cell.shape
176
177 local std_shape = {1, 1}
178 -- clear control info for children
179 for i=1, old_shape[1] do
180 local row_id = row_id + i - 1
181 for j = 1, old_shape[2] do
182 local col_id = cell_id + j - 1
183 local other_cell = self:get_cell(row_id, col_id)
184 other_cell.parent = nil
185 other_cell.shape = std_shape
186 end
187 end
188
189 local parent_t = {row_id, cell_id}
190 -- adjust control info based on new shape
191 for i=1, shape[1] do
192 local row_id = row_id + i - 1
193 for j = 1, shape[2] do
194 local col_id = cell_id + j - 1
195 local other_cell = self:get_cell(row_id, col_id)
196 other_cell.parent = parent_t
197 other_cell.shape = nil
198 end
199 end
200
201 -- adjust control info for this cell
202 cell.parent = nil
203 cell.shape = shape
204 cell.data = data
205 end
206
207 function TeXTable:fill(data)
208 local nrows = #self.rows
209 for i=1,nrows do
210 for j=1,self.ncols do
211 local cell = self:get_cell(i, j)
212 cell.shape = _std_shape
213 cell.parent = nil
214 cell.data = data
215 end
216 end
217 end
218
219 function TeXTable:fix_row_index(index_pair)
220 local ret = {index_pair[1], index_pair[2]}
221
222 if ret[1] < 0 then
223 ret[1] = #self.rows + ret[1] + 1
224 end
225 if ret[2] < 0 then
226 ret[2] = #self.rows + ret[2] + 1
227 end
228
229 if ret[2] < ret[1] then
230 raise_error("invalid index expression: second element must not be smaller than the first")
231 end
232
233 if ret[1] <= 0 then
234 raise_error("invalid index expression: index must be greater than zero after conversion")
235 end
236
237 return ret
238 end
239
240 function TeXTable:fix_col_index(index_pair)
241 local ret = {index_pair[1], index_pair[2]}
242
243 if ret[1] < 0 then
244 ret[1] = self.ncols + ret[1] + 1
245 end
246 if ret[2] < 0 then
247 ret[2] = self.ncols + ret[2] + 1
248 end
249
250 if ret[2] < ret[1] then
251 raise_error("invalid index expression: second element must not be smaller than the first")
252 end
253
254 if ret[1] <= 0 then
255 raise_error("invalid index expression: index must be greater than zero after conversion")
256 end
257
258 return ret
259 end
260
261 -- fix the index of a single cell
262 function TeXTable:fix_cell_index(index_expr)
263 local the_table = self
264 local index_parse = _lpt_parse_index_expr(index_expr)
265 if #index_parse ~= 2 then
266 raise_error(string.format("invalid index expression for cell: '%s'", index_expr))
267 end
268
269 local row_expr = the_table:fix_row_index(index_parse[1])
270 local col_expr = the_table:fix_col_index(index_parse[2])
271
272 if row_expr[1] ~= row_expr[2] then
273 raise_error(string.format("invalid row index expression for cell: '%s'", index_expr))
274 end
275 if col_expr[1] ~= col_expr[2] then
276 raise_error(string.format("invalid column index expression for cell: '%s'", index_expr))
277 end
278
279 return {row_expr[1], col_expr[1]}
280 end
281
282 -- make sure table views do not cut through multicol/multirow
283 function TeXTable:check_view_validity(start_row, start_col, row_span, col_span)
284 local max_row = start_row + row_span - 1
285 local max_col = start_col + col_span - 1
286
287 for i = 1, row_span do
288 local row_id = start_row + i - 1
289 for j=1,col_span do
290 local col_id = start_col + j - 1
291 local cell = self:get_cell(row_id, col_id)
292 if cell.shape ~= nil then
293 local boundary_1 = row_id + cell.shape[1] - 1
294 local boundary_2 = col_id + cell.shape[2] - 1
295 if boundary_1 > max_row or boundary_2 > max_col then
296 raise_error(string.format("invalid table view: cell '(%d, %d)' has shape '(%d, %d)', which does not fit into viewed region", row_id, col_id, cell.shape[1], cell.shape[2]))
297 end
298 end
299 end
300 end
301 end
302
303
304 lpt_info = {}
305
306 function _lpt_get_table(table_name)
307 local table_name = _lpt_strip(table_name)
308 local the_table = lpt_info[table_name]
309 if the_table == nil then
310 raise_error(string.format("table '%s' does not exist", table_name))
311 end
312 return the_table
313 end
314
315
316 _verb_lines = {}
317 _verb_end_env = ""
318
319 function _lpt_store_verbatim_lines(str)
320 if string.find(str , _verb_end_env) then
321 luatexbase.remove_from_callback("process_input_buffer" , "lpt_store_verbatim_lines")
322 return _verb_end_env
323 else
324 if str ~= nil then
325 table.insert(_verb_lines, str)
326 end
327 return ""
328 end
329 end
330
331 function _lpt_register_verbatim(end_env)
332 for k in next, _verb_lines do rawset(_verb_lines, k, nil) end
333 _verb_end_env = end_env
334 luatexbase.add_to_callback("process_input_buffer", _lpt_store_verbatim_lines, "lpt_store_verbatim_lines")
335 end
336
337 local _lpt_ignored_fields = {nrows=true}
338 local _lpt_numeric_fields = {ncols=true}
339
340
341
342 function lpt_new_table(param)
343 local new_table = TeXTable:new()
344
345 for key, val in pairs(param) do
346 if _lpt_numeric_fields[key] then
347 rawset(new_table, key, _lpt_tonumber(val))
348 elseif _lpt_ignored_fields[key] then
349 -- do nothing
350 else
351 rawset(new_table, key, val)
352 end
353 end
354
355 local table_name = new_table["table name"]
356
357 local table_fetch = lpt_info[table_name]
358 if table_fetch ~= nil then
359 raise_error(string.format("table '%s' already exists", table_name))
360 end
361
362 local nrows = _lpt_tonumber(param["nrows"])
363
364 -- integrity check
365 if nrows < 0 then
366 raise_error(string.format("'nrows' (%d) of table '%s' is invalid", nrows, table_name))
367 end
368 if new_table.ncols <= 0 then
369 raise_error(string.format("'ncols' (%d) of table '%s' is invalid", new_table.ncols, table_name))
370 end
371
372 -- create table
373 new_table:resize(nrows, new_table.ncols)
374
375 lpt_info[table_name] = new_table
376 end
377
378 function lpt_table_fill(table_name, index_expr, env_content)
379 local the_table = _lpt_get_table(table_name)
380 local env_content = env_content or table.concat(_verb_lines, " ")
381
382 if index_expr:len() == 0 then
383 the_table:fill(env_content)
384 else
385 local index_parse = _lpt_parse_index_expr(index_expr)
386 if #index_parse ~= 2 then
387 raise_error(string.format("invalid index expr '%s' for table '%s'", index_expr, table_name))
388 end
389
390 local row_expr = the_table:fix_row_index(index_parse[1])
391 local col_expr = the_table:fix_col_index(index_parse[2])
392
393 local row_span = row_expr[2] - row_expr[1] + 1
394 local col_span = col_expr[2] - col_expr[1] + 1
395
396 the_table:check_view_validity(row_expr[1], col_expr[1], row_span, col_span)
397
398 for i=1,row_span do
399 local row_id = i + row_expr[1] - 1
400 for j=1,col_span do
401 local col_id = j + col_expr[1] - 1
402 the_table:set_cell(row_id, col_id, env_content, _std_shape)
403 end
404 end
405 end
406 end
407
408
409 function lpt_table_view(table_name, index_expr)
410 local env_content = table.concat(_verb_lines, "\n")
411 local the_table = _lpt_get_table(table_name)
412
413 local index_parse = _lpt_parse_index_expr(index_expr)
414 if #index_parse ~= 2 then
415 raise_error(string.format("invalid index expr '%s' for table '%s'", index_expr, table_name))
416 end
417
418 local row_expr = the_table:fix_row_index(index_parse[1])
419 local col_expr = the_table:fix_col_index(index_parse[2])
420
421 local row_span = row_expr[2] - row_expr[1] + 1
422 local col_span = col_expr[2] - col_expr[1] + 1
423
424 the_table:check_view_validity(row_expr[1], col_expr[1], row_span, col_span)
425
426 local parsed_table = _lpt_parse_table_view(env_content)
427
428 if #parsed_table ~= row_span then
429 raise_error(string.format("lpttableview: expecting '%d' rows, but get '%d' instead for table '%s'", row_span, #parsed_table, table_name))
430 end
431
432 for i=1,row_span do
433 local row_id = i + row_expr[1] - 1
434 local cols = parsed_table[i]
435
436 -- sum the number of total columns in this row
437 local n_table_cols = 0
438 local max_row_size = 0
439 for _, val in ipairs(cols) do
440 n_table_cols = n_table_cols + val["shape"][2]
441 max_row_size = math.max(max_row_size, val["shape"][1])
442 end
443
444 local max_row = row_id + max_row_size - 1
445 if max_row > row_expr[2] then
446 raise_error("lpttableview: the height of table is greater than table view")
447 end
448
449
450 -- count the numbers of columns needs to be filled in this row
451 local pending_table_cols = 0
452 for j=1,col_span do
453 local col_id = j + col_expr[1] - 1
454 local cell = the_table:get_cell(row_id, col_id)
455 if cell.shape ~= nil then
456 pending_table_cols = pending_table_cols + 1
457 else
458 local parent_row = cell.parent[1]
459 -- if the parent is in the same row,
460 -- then this space needs to be filled in the same row
461 if parent_row == row_id then
462 pending_table_cols = pending_table_cols + 1
463 end
464 end
465 end
466
467 if n_table_cols ~= pending_table_cols then
468 raise_error(string.format("lpttableview: expecting '%d' cols for row '%d', but get '%d' instead for table '%s'", pending_table_cols, i, n_table_cols, table_name))
469 end
470
471
472 --[[
473 if n_table_cols ~= col_span then
474 raise_error(string.format("lpttableview: expecting '%d' cols for row '%d', but get '%d' instead for table '%s'", col_span, i, n_table_cols, table_name))
475 end
476 --]]
477
478 local k = 1
479 for j=1,col_span do
480 local col_id = j + col_expr[1] - 1
481 local cell = the_table:get_cell(row_id, col_id)
482 if cell.shape ~= nil then
483 local col_data = cols[k]["data"]
484 local col_shape = cols[k]["shape"]
485 the_table:set_cell(row_id, col_id, col_data, col_shape)
486 k = k + 1
487 end
488 end
489
490 end
491
492
493 end
494
495 local _s_buf = TeXStringBuffer:new()
496
497 function lpt_use_table(table_name)
498 local the_table = _lpt_get_table(table_name)
499 local input_method = rawget(the_table, "input method")
500
501 if input_method == "stringbuffer" then
502 local table_str = the_table:to_string(true)
503 _s_buf:clear()
504 _s_buf:append(table_str)
505 _s_buf:register_callback()
506 tex.print([[\input{stringbuffer}]])
507 elseif input_method == "file" then
508 local table_str = the_table:to_string()
509 local filename = tex.jobname .. "-" .. table_name .. ".table"
510 local outfile = io.open(filename, "w")
511 outfile:write(table_str)
512 outfile:close()
513 tex.print(string.format([[\input{%s}]], filename))
514 else
515 raise_error(string.format("invalid input method '%s' for table '%s'", input_method, table_name))
516 end
517
518 end
519
520 function lpt_set_row_prop(table_name, index_expr, param)
521
522 local the_table = _lpt_get_table(table_name)
523 local index_parse = _lpt_parse_index_expr(index_expr)
524 for key, val in ipairs(index_parse) do
525 local fixed_val = the_table:fix_row_index(val)
526
527 local l = fixed_val[1]
528 local r = fixed_val[2]
529
530 for i=l,r do
531 local row = the_table.rows[i]
532 if row == nil then
533 raise_error(string.format("lpt_set_row_prop: invalid index expression '%s'", index_expr))
534 end
535 for kkey, vval in pairs(param) do
536 if rawget(row, kkey) == nil then
537 raise_error(string.format("row '%d' of table '%s' does not have attribute '%s'", i, table_name, kkey))
538 end
539 rawset(row, kkey, vval)
540 end
541 end
542 end
543 end
544
545
546 function lpt_add_row(table_name, param)
547 local the_table = _lpt_get_table(table_name)
548 local nrows = #the_table.rows
549 local ncols = the_table.ncols
550 the_table:resize(nrows+1, ncols)
551 lpt_set_row_prop(table_name, "-1", param)
552 end
553
554 function lpt_get_table_shape(table_name)
555 local the_table = _lpt_get_table(table_name)
556 local nrows = #the_table.rows
557 local ncols = the_table.ncols
558 tex.print(string.format("{%d}{%d}", nrows, ncols))
559 end