publ-reg.lua /size: 6268 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['publ-reg'] = {
2    version   = 1.001,
3    comment   = "this module part of publication support",
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 formatters = string.formatters
10local concat     = table.concat
11local sortedhash = table.sortedhash
12
13local context        = context
14
15local implement      = interfaces.implement
16local variables      = interfaces.variables
17
18local v_once         = variables.once
19local v_stop         = variables.stop
20local v_all          = variables.all
21
22local publications   = publications
23local datasets       = publications.datasets
24local specifications = publications.specifications
25local writers        = publications.writers
26local getcasted      = publications.getcasted
27
28local registrations  = { }
29local sequence       = { }
30local flushers       = table.setmetatableindex(function(t,k) local v = t.default t[k] = v return v end)
31
32local function btxsetregister(specification)
33    local name     = specification.name
34    local register = specification.register
35    local dataset  = specification.dataset
36    local field    = specification.field
37    if not field or field == "" or not register or register == "" then
38        return
39    end
40    if not dataset or dataset == "" then
41        dataset = v_all
42    end
43    -- could be metatable magic
44    local s = registrations[register]
45    if not s then
46        s = { }
47        registrations[register] = s
48    end
49    local processors = name ~= register and name or ""
50    if processor == "" then
51        processor = nil
52    elseif processor then
53        processor = "btx:r:" .. processor
54    end
55    local datasets = utilities.parsers.settings_to_array(dataset)
56    for i=1,#datasets do
57        local dataset = datasets[i]
58        local d = s[dataset]
59        if not d then
60            d = { }
61            s[dataset] = d
62        end
63        --
64        -- check all
65        --
66        d.active      = specification.state ~= v_stop
67        d.once        = specification.method == v_once or false
68        d.field       = field
69        d.processor   = processor
70        d.alternative = d.alternative or specification.alternative
71        d.register    = register
72        d.dataset     = dataset
73        d.done        = d.done or { }
74    end
75    --
76    sequence   = { }
77    for register, s in sortedhash(registrations) do
78        for dataset, d in sortedhash(s) do
79            if d.active then
80                sequence[#sequence+1] = d
81            end
82        end
83    end
84end
85
86local function btxtoregister(dataset,tag)
87    local current = datasets[dataset]
88    for i=1,#sequence do
89        local step = sequence[i]
90        local dset = step.dataset
91        if dset == v_all or dset == dataset then
92            local done = step.done
93--             if not done[tag] then
94if not step.once or not done[tag] then
95                local value, field, kind = getcasted(current,tag,step.field,specifications[step.specification])
96                if value then
97                    flushers[kind](step,field,value)
98                end
99                done[tag] = true
100            end
101        end
102    end
103end
104
105implement {
106    name      = "btxsetregister",
107    actions   = btxsetregister,
108    arguments = {
109        {
110            { "specification" },
111            { "name" },
112            { "state" },
113            { "dataset" },
114            { "field" },
115            { "register" },
116            { "method" },
117            { "alternative" },
118        }
119    }
120}
121
122implement {
123    name      = "btxtoregister",
124    actions   = btxtoregister,
125    arguments = "2 strings",
126}
127
128-- context.setregisterentry (
129--     { register },
130--     {
131--         ["entries:1"] = value,
132--         ["keys:1"]    = value,
133--     }
134-- )
135
136local ctx_dosetfastregisterentry = context.dosetfastregisterentry -- register entry key
137
138local components = publications.components.author
139local f_author   = formatters[ [[\btxindexedauthor{%s}{%s}{%s}{%s}{%s}{%s}]] ]
140
141function flushers.string(step,field,value)
142    if type(value) == "string" and value ~= "" then
143        ctx_dosetfastregisterentry(step.register,value or "","",step.processor or "","")
144    end
145end
146
147flushers.default = flushers.string
148
149local shorts = {
150    normalshort   = "normalshort",
151    invertedshort = "invertedshort",
152}
153
154function flushers.author(step,field,value)
155    if type(value) == "string" then
156        value = publications.authorcache[value]
157    end
158    if type(value) == "table" and #value > 0 then
159        local register    = step.register
160        local processor   = step.processor
161        local alternative = shorts[step.alternative or "invertedshort"] or "invertedshort"
162        for i=1,#value do
163            local a = value[i]
164            local k = writers[field] { a }
165            local e = f_author(alternative,components(a))
166            ctx_dosetfastregisterentry(register,e,k,processor or "","")
167        end
168    end
169end
170
171function flushers.keyword(step,field,value)
172    if type(value) == "table" and #value > 0 then
173        local register  = step.register
174        local processor = step.processor
175        for i=1,#value do
176            ctx_dosetfastregisterentry(register,value[i],"",processor or "","")
177        end
178    end
179end
180
181-- publications.registerflushers = flushers
182
183local function authortoregister(dataset,hash)
184    local author = publications.authorcache[hash]
185    if author then
186        local current = datasets[dataset]
187        for i=1,#sequence do
188            local step = sequence[i]
189            local dset = step.dataset
190            if dset == v_all or dset == dataset then
191                local register    = step.register
192                local processor   = step.processor
193                local alternative = shorts[step.alternative or "invertedshort"] or "invertedshort"
194                local k = writers.author { author }
195                local e = f_author(alternative,components(author,short))
196                ctx_dosetfastregisterentry(register,e,k,processor or "","")
197            end
198        end
199    end
200end
201
202publications.authortoregister = authortoregister
203
204implement {
205    name      = "btxauthortoregister",
206    actions   = authortoregister,
207    arguments = "2 strings",
208}
209