tabl-tbl.lua /size: 1982 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['tabl-tbl'] = {
2    version   = 1.001,
3    comment   = "companion to tabl-tbl.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- A couple of hacks ... easier to do in Lua than in regular TeX. More will
10-- follow.
11
12local tonumber = tonumber
13local gsub, rep, sub, find = string.gsub, string.rep, string.sub, string.find
14local P, C, Cc, Ct, lpegmatch = lpeg.P, lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.match
15
16local context     = context
17local commands    = commands
18
19local texsetcount = tex.setcount
20
21local separator   = P("|")                  -- keep   { }
22----- nested      = C(lpeg.patterns.nested) -- remove { }
23local nested      = lpeg.patterns.argument
24local pattern     = Ct((separator * (nested + Cc("")) * C((1-separator)^0))^0)
25
26local ctx_settabulatelastentry = context.settabulatelastentry
27local ctx_settabulateentry     = context.settabulateentry
28
29-- the lmtx raw processor handles {} like the normal one so we need to prune
30
31local function presettabulate(preamble)
32    preamble = gsub(preamble,"~","d") -- let's get rid of ~ mess here
33    if find(preamble,"*",1,true) then
34        -- todo: lpeg but not now
35        preamble = gsub(preamble, "%*(%b{})(%b{})", function(n,p)
36            return rep(sub(p,2,-2),tonumber(sub(n,2,-2)) or 1)
37        end)
38    end
39    local t = lpegmatch(pattern,preamble)
40    local m = #t - 2
41    texsetcount("global","c_tabl_tabulate_nofcolumns", m/2)
42    texsetcount("global","c_tabl_tabulate_has_rule_spec_first", t[1] == "" and 0 or 1)
43    texsetcount("global","c_tabl_tabulate_has_rule_spec_last", t[m+1] == "" and 0 or 1)
44    for i=1,m,2 do
45        ctx_settabulateentry(t[i],t[i+1])
46    end
47    ctx_settabulatelastentry(t[m+1])
48end
49
50interfaces.implement {
51    name      = "presettabulate",
52    actions   = presettabulate,
53    arguments = "string",
54    scope     = "private",
55}
56