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
10
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("|")
23
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 <const> = texiscount("c_tabl_tabulate_nofcolumns")
31local c_tabl_tabulate_has_rule_spec_first <const> = texiscount("c_tabl_tabulate_has_rule_spec_first")
32local c_tabl_tabulate_has_rule_spec_last <const> = texiscount("c_tabl_tabulate_has_rule_spec_last")
33
34
35
36local function presettabulate(preamble)
37 preamble = gsub(preamble,"~","d")
38 if find(preamble,"*",1,true) then
39
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 |