core-two.lmt /size: 5493 b    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['core-two'] = {
2    version   = 1.001,
3    comment   = "companion to core-two.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-- This is actually one of the oldest MkIV files and basically a port of MkII but
10-- the old usage has long be phased out. Also, the public part is now handled by
11-- datasets which makes this a more private store.
12
13-- local next = next
14-- local remove, concat = table.remove, table.concat
15
16local allocate = utilities.storage.allocate
17
18local collected = allocate()
19local tobesaved = allocate()
20
21local jobpasses = {
22    collected = collected,
23    tobesaved = tobesaved,
24}
25
26job.passes = jobpasses
27
28local function initializer()
29    collected = jobpasses.collected
30    tobesaved = jobpasses.tobesaved
31end
32
33job.register('job.passes.collected', tobesaved, initializer, nil)
34
35function jobpasses.getcollected(id)
36    return collected[id] or { }
37end
38
39function jobpasses.gettobesaved(id)
40    local t = tobesaved[id]
41    if not t then
42        t = { }
43        tobesaved[id] = t
44    end
45    return t
46end
47
48-- local function define(id)
49--     local p = tobesaved[id]
50--     if not p then
51--         p = { }
52--         tobesaved[id] = p
53--     end
54--     return p
55-- end
56--
57-- local function save(id,str,index)
58--     local jti = define(id)
59--     if index then
60--         jti[index] = str
61--     else
62--         jti[#jti+1] = str
63--     end
64-- end
65--
66-- local function savetagged(id,tag,str)
67--     local jti = define(id)
68--     jti[tag] = str
69-- end
70--
71-- local function getdata(id,index,default)
72--     local jti = collected[id]
73--     local value = jti and jti[index]
74--     return value ~= "" and value or default or ""
75-- end
76--
77-- local function getfield(id,index,tag,default)
78--     local jti = collected[id]
79--     jti = jti and jti[index]
80--     local value = jti and jti[tag]
81--     return value ~= "" and value or default or ""
82-- end
83--
84-- local function getcollected(id)
85--     return collected[id] or { }
86-- end
87--
88-- local function gettobesaved(id)
89--     return define(id)
90-- end
91--
92-- local function get(id)
93--     local jti = collected[id]
94--     if jti and #jti > 0 then
95--         return remove(jti,1)
96--     end
97-- end
98--
99-- local function first(id)
100--     local jti = collected[id]
101--     return jti and jti[1]
102-- end
103--
104-- local function last(id)
105--     local jti = collected[id]
106--     return jti and jti[#jti]
107-- end
108--
109-- local function find(id,n)
110--     local jti = collected[id]
111--     return jti and jti[n] or nil
112-- end
113--
114-- local function count(id)
115--     local jti = collected[id]
116--     return jti and #jti or 0
117-- end
118--
119-- local function list(id)
120--     local jti = collected[id]
121--     if jti then
122--         return concat(jti,',')
123--     end
124-- end
125--
126-- local function inlist(id,str)
127--     local jti = collected[id]
128--     if jti then
129--         for _, v in next, jti do
130--             if v == str then
131--                 return true
132--             end
133--         end
134--     end
135--     return false
136-- end
137--
138-- local check = first
139--
140-- jobpasses.define       = define
141-- jobpasses.save         = save
142-- jobpasses.savetagged   = savetagged
143-- jobpasses.getdata      = getdata
144-- jobpasses.getfield     = getfield
145-- jobpasses.getcollected = getcollected
146-- jobpasses.gettobesaved = gettobesaved
147-- jobpasses.get          = get
148-- jobpasses.first        = first
149-- jobpasses.last         = last
150-- jobpasses.find         = find
151-- jobpasses.list         = list
152-- jobpasses.count        = count
153-- jobpasses.check        = check
154-- jobpasses.inlist       = inlist
155--
156-- -- interface
157--
158-- local implement = interfaces.implement
159--
160-- implement { name = "gettwopassdata",     actions = { get,   context }, arguments = "string" }
161-- implement { name = "getfirsttwopassdata",actions = { first, context }, arguments = "string" }
162-- implement { name = "getlasttwopassdata", actions = { last,  context }, arguments = "string" }
163-- implement { name = "findtwopassdata",    actions = { find,  context }, arguments = "2 strings" }
164-- implement { name = "gettwopassdatalist", actions = { list,  context }, arguments = "string" }
165-- implement { name = "counttwopassdata",   actions = { count, context }, arguments = "string" }
166-- implement { name = "checktwopassdata",   actions = { check, context }, arguments = "string" }
167--
168-- implement {
169--     name      = "definetwopasslist",
170--     actions   = define,
171--     arguments = "string"
172-- }
173--
174-- implement {
175--     name      = "savetwopassdata",
176--     actions   = save,
177--     arguments = "2 strings",
178-- }
179--
180-- implement {
181--     name      = "savetaggedtwopassdata",
182--     actions   = savetagged,
183--     arguments = "3 strings",
184-- }
185--
186-- implement {
187--     name      = "doifelseintwopassdata",
188--     actions   = { inlist, commands.doifelse },
189--     arguments = "2 strings",
190-- }
191--
192-- -- local ctx_latelua = context.latelua
193--
194-- -- implement {
195-- --     name      = "lazysavetwopassdata",
196-- --     arguments = "3 strings",
197-- --     public    = true,
198-- --     actions   = function(a,b,c)
199-- --         ctx_latelua(function() save(a,c) end)
200-- --     end,
201-- -- }
202--
203-- -- implement {
204-- --     name      = "lazysavetaggedtwopassdata",
205-- --     arguments = "3 strings",
206-- --     public    = true,
207-- --     actions   = function(a,b,c)
208-- --         ctx_latelua(function() savetagged(a,b,c) end)
209-- --     end,
210-- -- }
211