summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/core-two.lua
blob: 3ab2112b9aa1037bad90dd3ff004cd948a8579d0 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
if not modules then modules = { } end modules ['core-two'] = {
    version   = 1.001,
    comment   = "companion to core-two.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local next = next
local remove, concat = table.remove, table.concat
local allocate = utilities.storage.allocate

--[[ldx--
<p>We save multi-pass information in the main utility table. This is a
bit of a mess because we support old and new methods.</p>
--ldx]]--

local collected = allocate()
local tobesaved = allocate()

local jobpasses = {
    collected = collected,
    tobesaved = tobesaved,
}

job.passes = jobpasses

local function initializer()
    collected = jobpasses.collected
    tobesaved = jobpasses.tobesaved
end

job.register('job.passes.collected', tobesaved, initializer, nil)

local function define(id)
    local p = tobesaved[id]
    if not p then
        p = { }
        tobesaved[id] = p
    end
    return p
end

local function save(id,str,index)
    local jti = define(id)
    if index then
        jti[index] = str
    else
        jti[#jti+1] = str
    end
end

local function savetagged(id,tag,str)
    local jti = define(id)
    jti[tag] = str
end

local function getdata(id,index,default)
    local jti = collected[id]
    local value = jti and jti[index]
    return value ~= "" and value or default or ""
end

local function getfield(id,index,tag,default)
    local jti = collected[id]
    jti = jti and jti[index]
    local value = jti and jti[tag]
    return value ~= "" and value or default or ""
end

local function getcollected(id)
    return collected[id] or { }
end

local function gettobesaved(id)
    return define(id)
end

local function get(id)
    local jti = collected[id]
    if jti and #jti > 0 then
        return remove(jti,1)
    end
end

local function first(id)
    local jti = collected[id]
    return jti and jti[1]
end

local function last(id)
    local jti = collected[id]
    return jti and jti[#jti]
end

local function find(id,n)
    local jti = collected[id]
    return jti and jti[n] or nil
end

local function count(id)
    local jti = collected[id]
    return jti and #jti or 0
end

local function list(id)
    local jti = collected[id]
    if jti then
        return concat(jti,',')
    end
end

local function inlist(id,str)
    local jti = collected[id]
    if jti then
        for _, v in next, jti do
            if v == str then
                return true
            end
        end
    end
    return false
end

local check = first

jobpasses.define       = define
jobpasses.save         = save
jobpasses.savetagged   = savetagged
jobpasses.getdata      = getdata
jobpasses.getfield     = getfield
jobpasses.getcollected = getcollected
jobpasses.gettobesaved = gettobesaved
jobpasses.get          = get
jobpasses.first        = first
jobpasses.last         = last
jobpasses.find         = find
jobpasses.list         = list
jobpasses.count        = count
jobpasses.check        = check
jobpasses.inlist       = inlist

-- interface

local implement = interfaces.implement

implement { name = "gettwopassdata",     actions = { get,   context }, arguments = "string" }
implement { name = "getfirsttwopassdata",actions = { first, context }, arguments = "string" }
implement { name = "getlasttwopassdata", actions = { last,  context }, arguments = "string" }
implement { name = "findtwopassdata",    actions = { find,  context }, arguments = "2 strings" }
implement { name = "gettwopassdatalist", actions = { list,  context }, arguments = "string" }
implement { name = "counttwopassdata",   actions = { count, context }, arguments = "string" }
implement { name = "checktwopassdata",   actions = { check, context }, arguments = "string" }

implement {
    name      = "definetwopasslist",
    actions   = define,
    arguments = "string"
}

implement {
    name      = "savetwopassdata",
    actions   = save,
    arguments = "2 strings",
}

implement {
    name      = "savetaggedtwopassdata",
    actions   = savetagged,
    arguments = "3 strings",
}

implement {
    name      = "doifelseintwopassdata",
    actions   = { inlist, commands.doifelse },
    arguments = "2 strings",
}

-- local ctx_latelua = context.latelua

-- implement {
--     name      = "lazysavetwopassdata",
--     arguments = "3 strings",
--     public    = true,
--     actions   = function(a,b,c)
--         ctx_latelua(function() save(a,c) end)
--     end,
-- }

-- implement {
--     name      = "lazysavetaggedtwopassdata",
--     arguments = "3 strings",
--     public    = true,
--     actions   = function(a,b,c)
--         ctx_latelua(function() savetagged(a,b,c) end)
--     end,
-- }