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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
if not modules then modules = { } end modules ['strc-ini'] = {
version = 1.001,
comment = "companion to strc-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
--[[
The restructuring is the (intermediate) result of quite some experiments. I started
with the basic structure, followed by lists, numbers, enumerations, itemgroups
and floats. All these have something in common, like pagenumbers and section
prefixes. I played with some generic datastructure (in order to save space) but
the code at both the lua and tex end then quickly becomes messy due to the fact
that access to variables is too different. So, eventually I ended up with
dedicated structures combined with sharing data. In lua this is quite efficient
because tables are referenced. However, some precautions are to be taken in
order to keep the utility file small. Utility data and process data share much
but it does not make sense to store all processdata.
]]--
local lpegmatch = lpeg.match
local type, next, tonumber, select = type, next, tonumber, select
local formatters = string.formatters
local settings_to_array = utilities.parsers.settings_to_array
local settings_to_hash = utilities.parsers.settings_to_hash
local allocate = utilities.storage.allocate
local catcodenumbers = catcodes.numbers -- better use the context(...) way to switch
local ctxcatcodes = catcodenumbers.ctxcatcodes
local xmlcatcodes = catcodenumbers.xmlcatcodes
local notcatcodes = catcodenumbers.notcatcodes
local txtcatcodes = catcodenumbers.txtcatcodes
local context = context
local commands = commands
local trace_processors = false
local report_processors = logs.reporter("processors","structure")
trackers.register("typesetters.processors", function(v) trace_processors = v end)
local xmlconvert = lxml.convert
local xmlstore = lxml.store
local ctx_pushcatcodes = context.pushcatcodes
local ctx_popcatcodes = context.popcatcodes
local ctx_xmlsetup = context.xmlsetup
local ctx_xmlprocessbuffer = context.xmlprocessbuffer
-- -- -- namespace -- -- --
-- This is tricky: we have stored and initialized already some of
-- the job.registered tables so we have a forward reference!
structures = structures or { }
local structures = structures
structures.blocks = structures.blocks or { }
structures.sections = structures.sections or { }
structures.pages = structures.pages or { }
structures.registers = structures.registers or { }
structures.references = structures.references or { }
structures.lists = structures.lists or { }
structures.helpers = structures.helpers or { }
structures.documents = structures.documents or { }
structures.notes = structures.notes or { }
structures.descriptions = structures.descriptions or { }
structures.itemgroups = structures.itemgroups or { }
structures.specials = structures.specials or { }
structures.counters = structures.counters or { }
structures.tags = structures.tags or { }
structures.formulas = structures.formulas or { } -- not used but reserved
structures.sets = structures.sets or { }
structures.marks = structures.marks or { }
structures.floats = structures.floats or { }
structures.synonyms = structures.synonyms or { }
--~ table.print(structures)
local processors = typesetters.processors
-- -- -- specials -- -- --
-- we can store information and get back a reference; this permits
-- us to store rather raw data in references
local specials = structures.specials
local collected = allocate()
local tobesaved = allocate()
specials.collected = collected
specials.tobesaved = tobesaved
local function initializer()
collected = specials.collected
tobesaved = specials.tobesaved
end
if job then
job.register('structures.specials.collected', tobesaved, initializer)
end
function specials.store(class,data)
if class and data then
local s = tobesaved[class]
if not s then
s = { }
tobesaved[class] = s
end
s[#s+1] = data
context(#s)
else
context(0)
end
end
function specials.retrieve(class,n)
if class and n then
local c = collected[class]
return c and c[n]
end
end
-- -- -- helpers -- -- --
local helpers = structures.helpers
-- function helpers.touserdata(str)
-- local hash = str and str ~= "" and settings_to_hash(str)
-- if hash and next(hash) then
-- return hash
-- end
-- end
function helpers.touserdata(data)
if type(data) == "string" then
if data == "" then
return nil
else
data = settings_to_hash(data)
end
end
if data and next(data) then
return data
end
end
local function simplify(d,nodefault)
if d then
local t = { }
for k, v in next, d do
local tv = type(v)
if tv == "table" then
if next(v) then
t[k] = simplify(v)
end
elseif tv == "string" then
if v ~= "" then
t[k] = v
end
elseif tv == "boolean" then
if v then
t[k] = v
end
else
t[k] = v
end
end
return next(t) and t
elseif nodefault then
return nil
else
return { }
end
end
-- we only care about the tuc file so this would do too:
--
-- local function simplify(d,nodefault)
-- if d then
-- for k, v in next, d do
-- local tv = type(v)
-- if tv == "string" then
-- if v == "" or v == "default" then
-- d[k] = nil
-- end
-- elseif tv == "table" then
-- if next(v) then
-- simplify(v)
-- end
-- elseif tv == "boolean" then
-- if not v then
-- d[k] = nil
-- end
-- end
-- end
-- return d
-- elseif nodefault then
-- return nil
-- else
-- return { }
-- end
-- end
helpers.simplify = simplify
function helpers.merged(...)
local t = { }
for k=1, select("#",...) do
local v = select(k,...)
if v and v ~= "" and not t[k] then
t[k] = v
end
end
return t
end
local tags = {
generic = "ctx:genericentry",
section = "ctx:sectionentry",
entry = "ctx:registerentry",
}
-- We had the following but it overloads the main document so it's a no-go as we
-- no longer push and pop. So now we use the tag as buffername, namespace and also
-- (optionally) as a setups to be applied but keep in mind that document setups
-- also get applied (when they use #1's).
--
-- local command = formatters["\\xmlprocessbuffer{%s}{%s}{}"](metadata.xmlroot or "main",tag)
local overload_catcodes = true
directives.register("typesetters.processors.overloadcatcodes",function(v)
-- number | true | false | string
overload_catcodes = v
end)
local experiment = true
function helpers.title(title,metadata) -- coding is xml is rather old and not that much needed now
if title and title ~= "" then -- so it might disappear
if metadata then
local xmlsetup = metadata.xmlsetup
if metadata.coding == "xml" then
-- title can contain raw xml
local tag = tags[metadata.kind] or tags.generic
local xmldata = formatters["<?xml version='1.0'?><%s>%s</%s>"](tag,title,tag)
if not experiment then
buffers.assign(tag,xmldata)
end
if trace_processors then
report_processors("putting xml data in buffer: %s",xmldata)
report_processors("processing buffer with setup %a and tag %a",xmlsetup,tag)
end
if experiment then
-- the question is: will this be forgotten ... better store in a via file
local xmltable = xmlconvert("temp",xmldata or "")
xmlstore("temp",xmltable)
ctx_xmlsetup("temp",xmlsetup or "")
else
ctx_xmlprocessbuffer("dummy",tag,xmlsetup or "")
end
elseif xmlsetup then -- title is reference to node (so \xmlraw should have been used)
if trace_processors then
report_processors("feeding xmlsetup %a using node %a",xmlsetup,title)
end
ctx_xmlsetup(title,metadata.xmlsetup)
else
local catcodes = metadata.catcodes
if overload_catcodes == false then
if trace_processors then
report_processors("catcodetable %a, text %a",catcodes,title)
end
--
-- context.sprint(catcodes,title)
--
-- doesn't work when a newline is in there \section{Test\ A} so we do
-- it this way:
--
ctx_pushcatcodes(catcodes)
context(title)
ctx_popcatcodes()
elseif overload_catcodes == true then
if catcodes == notcatcodes or catcodes == xmlcatcodes then
-- when was this needed
if trace_processors then
report_processors("catcodetable %a, overloads %a, text %a",ctxcatcodes,catcodes,title)
end
context(title)
else
ctx_pushcatcodes(catcodes)
context(title)
ctx_popcatcodes()
end
else
if trace_processors then
report_processors("catcodetable %a, overloads %a, text %a",catcodes,overload_catcodes,title)
end
ctx_pushcatcodes(overload_catcodes)
context(title)
ctx_popcatcodes()
end
end
else
-- no catcode switch, was: texsprint(title)
context(title)
end
end
end
-- -- -- sets -- -- --
local sets = structures.sets
sets.setlist = sets.setlist or { }
storage.register("structures/sets/setlist", structures.sets.setlist, "structures.sets.setlist")
local setlist = sets.setlist
function sets.define(namespace,name,values,default,numbers)
local dn = setlist[namespace]
if not dn then
dn = { }
setlist[namespace] = dn
end
if values == "" then
dn[name] = { { }, default }
else
local split = settings_to_array(values)
if numbers then
-- convert to numbers (e.g. for reset)
for i=1,#split do
split[i] = tonumber(split[i]) or 0
end
end
dn[name] = { split, default }
end
end
function sets.getall(namespace,block,name)
local ds = setlist[namespace]
if not ds then
return { }
else
local dn
if block and block ~= "" then
dn = ds[block..":"..name] or ds[name] or ds[block] or ds.default
else
dn = ds[name] or ds.default
end
return (dn and dn[1]) or { }
end
end
-- messy (will be another keyword, fixedconversion) .. needs to be documented too
-- maybe we should cache
local splitter = lpeg.splitat("::")
function sets.get(namespace,block,name,level,default) -- check if name is passed
--fixed::R:a: ...
local kind, rest = lpegmatch(splitter,name)
if rest and kind == "fixed" then -- fixed::n,a,i
local s = settings_to_array(rest)
return s[level] or s[#s] or default
end
--
local ds = setlist[namespace]
if not ds then
return default
end
local dn
if name and name ~= "" then
if block and block ~= "" then
dn = ds[block..":"..name] or ds[name] or ds[block] or ds.default
else
dn = ds[name] or ds.default
end
else
if block and block ~= "" then
dn = ds[block] or ds[block..":default"] or ds.default
else
dn = ds.default
end
end
if not dn then
return default
end
local dl = dn[1][level]
return dl or dn[2] or default
end
-- interface
interfaces.implement {
name = "definestructureset",
actions = sets.define,
arguments = { "string", "string", "string", "string", "boolean" }
}
|