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