tabl-tbl.lmt /size: 2284 b    last modification: 2024-01-16 10:22
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
20local texiscount  = tex.iscount
21
22local separator   = P("|")                  -- keep   { }
23----- nested      = C(lpeg.patterns.nested) -- remove { }
24local nested      = lpeg.patterns.argument
25local pattern     = Ct((separator * (nested + Cc("")) * C((1-separator)^0))^0)
26
27local ctx_settabulatelastentry = context.settabulatelastentry
28local ctx_settabulateentry     = context.settabulateentry
29
30local c_tabl_tabulate_nofcolumns          = texiscount("c_tabl_tabulate_nofcolumns")
31local c_tabl_tabulate_has_rule_spec_first = texiscount("c_tabl_tabulate_has_rule_spec_first")
32local c_tabl_tabulate_has_rule_spec_last  = texiscount("c_tabl_tabulate_has_rule_spec_last")
33
34-- the lmtx raw processor handles {} like the normal one so we need to prune
35
36local function presettabulate(preamble)
37    preamble = gsub(preamble,"~","d") -- let's get rid of ~ mess here
38    if find(preamble,"*",1,true) then
39        -- todo: lpeg but not now
40        preamble = gsub(preamble, "%*(%b{})(%b{})", function(n,p)
41            return rep(sub(p,2,-2),tonumber(sub(n,2,-2)) or 1)
42        end)
43    end
44    local t = lpegmatch(pattern,preamble)
45    local m = #t - 2
46    texsetcount("global",c_tabl_tabulate_nofcolumns, m/2)
47    texsetcount("global",c_tabl_tabulate_has_rule_spec_first, t[1]   == "" and 0 or 1)
48    texsetcount("global",c_tabl_tabulate_has_rule_spec_last,  t[m+1] == "" and 0 or 1)
49    for i=1,m,2 do
50        ctx_settabulateentry(t[i],t[i+1])
51    end
52    ctx_settabulatelastentry(t[m+1])
53end
54
55interfaces.implement {
56    name      = "presettabulate",
57    actions   = presettabulate,
58    arguments = "string",
59    scope     = "private",
60}
61