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