summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/strc-syn.lmt
blob: 940b982a72ea5da8a5eb85774b4698446b337ec9 (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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
if not modules then modules = { } end modules ['strc-syn'] = {
    version   = 1.001,
    comment   = "companion to str-syn.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local next, type = next, type
local sortedkeys = table.sortedkeys

local context      = context
local implement    = interfaces.implement

local allocate     = utilities.storage.allocate

local sorters      = sorters

local structures   = structures
local synonyms     = structures.synonyms
local tags         = structures.tags

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

local firstofsplit = sorters.firstofsplit
local strip        = sorters.strip
local splitter     = sorters.splitters.utf

synonyms.collected = collected
synonyms.tobesaved = tobesaved

local progressions = { } -- false=not_yet_shown  true=shown

local variables    = interfaces.variables
local v_all        = variables.all
local v_current    = variables.current

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

local function finalizer()
    for entry, data in next, tobesaved do
        data.hash = nil
     -- being sparse can be an option but often we actually do want the
     -- whole list so we don't do this ... only as possible option
     --
-- maybe pages = sortedkeys(pages) is more efficient
     --
     -- local entries = data.entries
     -- local t = { }
     -- local n = 0
     -- for i=1,#entries do
     --     local e = entries[i]
     --     if e.definition.shown then
     --         n = n + 1
     --         t[n] = e
     --     end
     -- end
     -- data.entries = t
    end
end

job.register('structures.synonyms.collected', tobesaved, initializer, finalizer)

-- todo: allocate becomes metatable

table.setmetatableindex(tobesaved,function(t,k)
    local v = {
        metadata = {
            language = 'en',
            sorted   = false,
            class    = v
        },
        entries  = {
        },
        hash = {
        }
    }
    t[k] = v
    return v
end)

function synonyms.define(class,kind)
    local data = tobesaved[class]
    data.metadata.kind = kind
end

function synonyms.register(class,kind,spec)
    local data       = tobesaved[class]
    local hash       = data.hash
    local definition = spec.definition
    local tag        = definition.tag or ""
    data.metadata.kind = kind -- runtime, not saved in format (yet)
    if not hash[tag] then
        local entries = data.entries
        entries[#entries+1] = spec
        hash[tag] = spec
    end
end

function synonyms.registerused(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    if okay then
        local definition = okay.definition
        definition.used = true
        definition.list = true
    end
end

function synonyms.registershown(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    if okay then
        local definition = okay.definition
        definition.shown = true
        definition.list  = true
    end
end

function synonyms.isused(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    return okay and okay.definition.used or false
end

function synonyms.isshown(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    return okay and okay.definition.shown or false
end

local function resetused(class)
    for tag, data in next, tobesaved[class].hash do
        data.definition.used = nil
    end
end

local function resetshown(class)
    for tag, data in next, tobesaved[class].hash do
        data.definition.shown = nil
    end
end

local function resetlist(class)
    for tag, data in next, tobesaved[class].hash do
        data.definition.list = nil
    end
end

local function resetall(class)
    for tag, data in next, tobesaved[class].hash do
        local definition = data.definition
        definition.used  = nil
        definition.shown = nil
        definition.list  = nil
    end
end

synonyms.resetused  = resetused
synonyms.resetshown = resetshown
synonyms.resetlist  = resetlist
synonyms.resetall   = resetall

function synonyms.reset(class,what)
    if what == "progress" then
        progressions = { }
    elseif what == "used" then
        resetused(class)
    elseif what == "shown" then
        resetshown(class)
    elseif what == "list" then
        resetlist(class)
    else
        resetall(class)
    end
end

function synonyms.synonym(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    if okay then
        local definition = okay.definition
        definition.used = true
        definition.list = true
        context(definition.synonym)
    end
    if progressions[tag] == nil then
        progressions[tag] = false -- not yet shown
    end
end

function synonyms.meaning(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    if okay then
        local definition = okay.definition
        definition.shown = true
        definition.list  = true
        context(definition.meaning)
    end
end

function synonyms.pages(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    if okay then
        local definition = okay.definition
        local pages = definition.pages
        if pages and next(pages) then
            context("%,t",sortedkeys(pages))
        end
    end
end

local ctx_latelua = context.latelua

local function enhance(data)
    local page = tex.getcount("realpageno")
    data.pages[page] = true
end

function synonyms.enhance(class,tag)
    local data = tobesaved[class]
    local okay = data.hash[tag]
    if okay then
        local definition = okay.definition
        local pages      = definition.pages
        if not pages then
            pages            = { }
            definition.pages = pages
        end
        ctx_latelua { action = enhance, pages = pages }
    end
end

synonyms.compare = sorters.comparers.basic -- (a,b)

function synonyms.filter(data,options)
    local result    = { }
    local entries   = data.entries
    local criterium = options and options.criterium
    if criterium == v_all then
        for i=1,#entries do
            result[i] = entries[i]
        end
    else
        for i=1,#entries do
            local entry      = entries[i]
            local definition = entry.definition
            if definition.list then
                local tag  = definition.tag
                local done = progressions[tag]
                if done == false then
                    result[#result+1] = entry
                    progressions[tag] = true
                end
            end
        end
        if criterium == v_current then
            progressions = { }
        end
    end
    data.result = result
end

function synonyms.prepare(data)
    local result = data.result
    if result then
        for i=1, #result do
            local entry      = result[i]
            local definition = entry.definition
            if definition then
                local srt = definition.sortkey or ""
                local tag = definition.tag or ""
                local key = (srt ~= "" and srt) or (tag ~= "" and tag) or definition.synonym
                if key then
                    entry.split = splitter(strip(key))
                end
            end
        end
    end
end

function synonyms.sort(data,options)
    sorters.sort(data.result,synonyms.compare)
    data.metadata.sorted = true
end

function synonyms.finalize(data,options) -- mostly the same as registers so we will generalize it: sorters.split
    local result   = data.result
    local split    = { }
    local nofsplit = 0
    local lasttag  = nil
    local lasttag  = nil
    local nofdone  = 0
    for k=1,#result do
        local entry = result[k]
        local first, tag = firstofsplit(entry)
        if tag ~= lasttag then
         -- if trace_registers then
         --     report_registers("splitting at %a",tag)
         -- end
            done     = { }
            nofdone  = 0
            nofsplit = nofsplit + 1
            lasttag  = tag
            split[nofsplit] = { tag = tag, data = done }
        end
        nofdone = nofdone + 1
        done[nofdone] = entry
    end
    data.result = split
end

-- for now, maybe at some point we will do a multipass or so
-- maybe pass the settings differently

local ctx_synonymentry = context.synonymentry

function synonyms.flush(data,options)
    local result = data.result
    for i=1,#result do
        local sublist = result[i]
        local data    = sublist.data
        for d=1,#data do
            local entry = data[d].definition
            ctx_synonymentry(d,entry.tag,entry.synonym,entry.meaning or "")
        end
    end
    data.result          = nil
    data.metadata.sorted = false
end

function synonyms.analyzed(class,options)
    local data = collected[class]
    if data and data.entries then
        options = options or { }
        sorters.setlanguage(options.language,options.method)
        synonyms.filter(data,options)   -- filters entries to result
        synonyms.prepare(data,options)  -- adds split table parallel to list table
        synonyms.sort(data,options)     -- sorts entries in result
        synonyms.finalize(data,options) -- do things with data.entries
        data.metadata.sorted = true
    end
    return data and data.metadata.sorted and data.result and next(data.result)
end

function synonyms.process(class,options)
    if synonyms.analyzed(class,options) then
        synonyms.flush(collected[class],options)
    end
end

-- todo: local higher up

implement { name = "registerusedsynonym",  actions = synonyms.registerused,  arguments = "2 strings" }
implement { name = "registershownsynonym", actions = synonyms.registershown, arguments = "2 strings" }
implement { name = "synonymmeaning",       actions = synonyms.meaning,       arguments = "2 arguments" }
implement { name = "synonymname",          actions = synonyms.synonym,       arguments = "2 arguments" }
implement { name = "synonympages",         actions = synonyms.pages,         arguments = "2 arguments" }
implement { name = "enhancesynonym",       actions = synonyms.enhance,       arguments = "2 arguments" }
--        { name = "resetusedsynonyms",    actions = resetused,              arguments = "string" }
--        { name = "resetshownsynonyms",   actions = resetshown,             arguments = "string" }
--        { name = "resetlistsynonyms",    actions = resetlist,              arguments = "string" }
implement { name = "resetsynonyms",        actions = synonyms.reset,         arguments = "2 strings" }

implement {
    name      = "doifelsesynonymused",
    actions   = { synonyms.isused, commands.doifelse },
    arguments = "2 arguments",
}

implement {
    name      = "doifelsesynonymshown",
    actions   = { synonyms.isshown, commands.doifelse },
    arguments = "2 arguments",
}

implement {
    name      = "registersynonym",
    actions   = synonyms.register,
    arguments = {
        "string",
        "string",
        {
            { "metadata", {
                    { "catcodes", "integer" },
                    { "coding" },
                    { "xmlroot" }
                }
            },
            {
                "definition", {
                    { "tag" },
                    { "synonym" },
                    { "meaning" },
                    { "sortkey" },
                    { "used", "boolean" }
                }
            }
        }
    }
}

implement {
    name      = "processsynonyms",
    actions   = synonyms.process,
    arguments = {
        "string",
        {
            { "criterium" },
            { "language" },
            { "method" }
        }
    }
}