summaryrefslogtreecommitdiff
path: root/tex/context/base/core-dat.lua
blob: 071a3fe0b4723f6798a0ad982365f8fedff94f1d (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
if not modules then modules = { } end modules ['core-dat'] = {
    version   = 1.001,
    comment   = "companion to core-dat.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

--[[ldx--
<p>This module provides a (multipass) container for arbitrary data. It
replaces the twopass data mechanism.</p>
--ldx]]--

local tonumber = tonumber

local context, commands = context, commands

local trace_datasets   = false  trackers.register("job.datasets" ,  function(v) trace_datasets   = v end)
local trace_pagestates = false  trackers.register("job.pagestates", function(v) trace_pagestates = v end)

local report_dataset   = logs.reporter("dataset")
local report_pagestate = logs.reporter("pagestate")

local allocate = utilities.storage.allocate
local settings_to_hash = utilities.parsers.settings_to_hash
local format = string.format
local texcount = tex.count

local v_yes = interfaces.variables.yes

local new_latelua = nodes.pool.latelua

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

local datasets = {
    collected = collected,
    tobesaved = tobesaved,
}

job.datasets = datasets

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

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

local sets = { }

table.setmetatableindex(tobesaved, function(t,k)
    local v = { }
    t[k] = v
    return v
end)

table.setmetatableindex(sets, function(t,k)
    local v = {
        index = 0,
        order = 0,
    }
    t[k] = v
    return v
end)

local function setdata(settings)
    local name = settings.name
    local tag  = settings.tag
    local data = settings.data
    local list = tobesaved[name]
    data = settings_to_hash(data) or { }
    if not tag then
        tag = #list + 1
    else
        tag = tonumber(tag) or tag -- autonumber saves keys
    end
    list[tag] = data
    if settings.delay == v_yes then
        local set = sets[name]
        local index = set.index + 1
        set.index = index
        data.index = index
        data.order = index
        data.realpage = texcount.realpageno
        if trace_datasets then
            report_dataset("delayed: name %s, tag %s, index %s",name,tag,index)
        end
    elseif trace_datasets then
        report_dataset("immediate: name %s, tag %s",name,tag)
    end
    return name, tag, data
end

datasets.setdata = setdata

function datasets.extend(name,tag)
    local set = sets[name]
    local order = set.order + 1
    local realpage = texcount.realpageno
    set.order = order
    local t = tobesaved[name][tag]
    t.realpage = realpage
    t.order = order
    if trace_datasets then
        report_dataset("flushed: name %s, tag %s, page %s, index %s, order",name,tag,t.index or 0,order,realpage)
    end
end

function datasets.getdata(name,tag,key,default)
    local t = collected[name]
    if t then
        t = t[tag] or t[tonumber(tag)]
        if t then
            if key then
                return t[key] or default
            else
                return t
            end
        elseif trace_datasets then
            report_dataset("unknown: name %s, tag %s",name,tag)
        end
    elseif trace_datasets then
        report_dataset("unknown: name %s",name)
    end
    return default
end

function commands.setdataset(settings)
    local name, tag, data = setdata(settings)
    if settings.delay ~= v_yes then
        --
    elseif type(tag) == "number" then
        context(new_latelua(format("job.datasets.extend(%q,%i)",name,tag)))
    else
        context(new_latelua(format("job.datasets.extend(%q,%q)",name,tag)))
    end
end

function commands.datasetvariable(name,tag,key)
    local t = collected[name]
    t = t and (t[tag] or t[tonumber(tag)])
    if t then
        local s = t[key]
        if s then
            context(s)
        end
    end
end

--[[ldx--
<p>We also provide an efficient variant for page states.</p>
--ldx]]--

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

local pagestates = {
    collected = collected,
    tobesaved = tobesaved,
}

job.pagestates = pagestates

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

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

table.setmetatableindex(tobesaved, function(t,k)
    local v = { }
    t[k] = v
    return v
end)

local function setstate(settings)
    local name = settings.name
    local tag  = settings.tag
    local list = tobesaved[name]
    if not tag then
        tag = #list + 1
    else
        tag = tonumber(tag) or tag -- autonumber saves keys
    end
    local realpage = texcount.realpageno
    local data = realpage
    list[tag] = data
    if trace_pagestates then
        report_pagestate("setting: name %s, tag %s, preset %s",name,tag,realpage)
    end
    return name, tag, data
end

pagestates.setstate = setstate

function pagestates.extend(name,tag)
    local realpage = texcount.realpageno
    if trace_pagestates then
        report_pagestate("synchronizing: name %s, tag %s, preset %s",name,tag,realpage)
    end
    tobesaved[name][tag] = realpage
end

function pagestates.realpage(name,tag,default)
    local t = collected[name]
    if t then
        t = t[tag] or t[tonumber(tag)]
        if t then
            return tonumber(t or default)
        elseif trace_pagestates then
            report_pagestate("unknown: name %s, tag %s",name,tag)
        end
    elseif trace_pagestates then
        report_pagestate("unknown: name %s",name)
    end
    return default
end

function commands.setpagestate(settings)
    local name, tag, data = setstate(settings)
    if type(tag) == "number" then
        context(new_latelua(format("job.pagestates.extend(%q,%i)",name,tag)))
    else
        context(new_latelua(format("job.pagestates.extend(%q,%q)",name,tag)))
    end
end

function commands.pagestaterealpage(name,tag)
    local t = collected[name]
    t = t and (t[tag] or t[tonumber(tag)])
    if t then
        context(t)
    end
end

function commands.setpagestaterealpageno(name,tag)
    local t = collected[name]
    t = t and (t[tag] or t[tonumber(tag)])
    if t then
        texcount.realpagestateno = t
    else
        texcount.realpagestateno = texcount.realpageno
    end
end