summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/tabl-tbl.lmt
blob: 50d8719055bf896185d4c60ca4c7233ba0381f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
if not modules then modules = { } end modules ['tabl-tbl'] = {
    version   = 1.001,
    comment   = "companion to tabl-tbl.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- A couple of hacks ... easier to do in Lua than in regular TeX. More will
-- follow.

local tonumber = tonumber
local gsub, rep, sub, find = string.gsub, string.rep, string.sub, string.find
local P, C, Cc, Ct, lpegmatch = lpeg.P, lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.match

local context     = context
local commands    = commands

local texsetcount = tex.setcount
local texiscount  = tex.iscount

local separator   = P("|")                  -- keep   { }
----- nested      = C(lpeg.patterns.nested) -- remove { }
local nested      = lpeg.patterns.argument
local pattern     = Ct((separator * (nested + Cc("")) * C((1-separator)^0))^0)

local ctx_settabulatelastentry = context.settabulatelastentry
local ctx_settabulateentry     = context.settabulateentry

local c_tabl_tabulate_nofcolumns          = texiscount("c_tabl_tabulate_nofcolumns")
local c_tabl_tabulate_has_rule_spec_first = texiscount("c_tabl_tabulate_has_rule_spec_first")
local c_tabl_tabulate_has_rule_spec_last  = texiscount("c_tabl_tabulate_has_rule_spec_last")

-- the lmtx raw processor handles {} like the normal one so we need to prune

local function presettabulate(preamble)
    preamble = gsub(preamble,"~","d") -- let's get rid of ~ mess here
    if find(preamble,"*",1,true) then
        -- todo: lpeg but not now
        preamble = gsub(preamble, "%*(%b{})(%b{})", function(n,p)
            return rep(sub(p,2,-2),tonumber(sub(n,2,-2)) or 1)
        end)
    end
    local t = lpegmatch(pattern,preamble)
    local m = #t - 2
    texsetcount("global",c_tabl_tabulate_nofcolumns, m/2)
    texsetcount("global",c_tabl_tabulate_has_rule_spec_first, t[1]   == "" and 0 or 1)
    texsetcount("global",c_tabl_tabulate_has_rule_spec_last,  t[m+1] == "" and 0 or 1)
    for i=1,m,2 do
        ctx_settabulateentry(t[i],t[i+1])
    end
    ctx_settabulatelastentry(t[m+1])
end

interfaces.implement {
    name      = "presettabulate",
    actions   = presettabulate,
    arguments = "string",
    scope     = "private",
}