publ-reg.lua /size: 6805 b    last modification: 2020-07-01 14:35
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
94                local value, field, kind = getcasted(current,tag,step.field,specifications[step.specification])
95                if value then
96                    flushers[kind](step,field,value)
97                end
98                done[tag] = true
99            end
100        end
101    end
102end
103
104implement {
105    name      = "btxsetregister",
106    actions   = btxsetregister,
107    arguments = {
108        {
109            { "specification" },
110            { "name" },
111            { "state" },
112            { "dataset" },
113            { "field" },
114            { "register" },
115            { "method" },
116            { "alternative" },
117        }
118    }
119}
120
121implement {
122    name      = "btxtoregister",
123    actions   = btxtoregister,
124    arguments = "2 strings",
125}
126
127-- context.setregisterentry (
128--     { register },
129--     {
130--         ["entries:1"] = value,
131--         ["keys:1"]    = value,
132--     }
133-- )
134
135local ctx_dosetfastregisterentry = context.dosetfastregisterentry -- register entry key
136
137local components = publications.components.author
138local f_author   = formatters[ [[\btxindexedauthor{%s}{%s}{%s}{%s}{%s}{%s}]] ]
139
140function flushers.string(step,field,value)
141    if type(value) == "string" and value ~= "" then
142        ctx_dosetfastregisterentry(step.register,value or "","",step.processor or "","")
143    end
144end
145
146flushers.default = flushers.string
147
148local shorts = {
149    normalshort   = "normalshort",
150    invertedshort = "invertedshort",
151}
152
153function flushers.author(step,field,value)
154    if type(value) == "string" then
155        value = publications.authorcache[value]
156    end
157    if type(value) == "table" and #value > 0 then
158        local register    = step.register
159        local processor   = step.processor
160        local alternative = shorts[step.alternative or "invertedshort"] or "invertedshort"
161        for i=1,#value do
162            local a = value[i]
163            local k = writers[field] { a }
164            local e = f_author(alternative,components(a))
165            ctx_dosetfastregisterentry(register,e,k,processor or "","")
166        end
167    end
168end
169
170function flushers.keyword(step,field,value)
171    if type(value) == "table" and #value > 0 then
172        local register  = step.register
173        local processor = step.processor
174        for i=1,#value do
175            ctx_dosetfastregisterentry(register,value[i],"",processor or "","")
176        end
177    end
178end
179
180-- publications.registerflushers = flushers
181
182local function btxtoregister(dataset,tag)
183    local current = datasets[dataset]
184    for i=1,#sequence do
185        local step = sequence[i]
186        local dset = step.dataset
187        if dset == v_all or dset == dataset then
188            local done = step.done
189            if not done[tag] then
190                local value, field, kind = getcasted(current,tag,step.field,specifications[step.specification])
191                if value then
192                    flushers[kind](step,field,value)
193                end
194                done[tag] = true
195            end
196        end
197    end
198end
199
200local function authortoregister(dataset,hash)
201    local author = publications.authorcache[hash]
202    if author then
203        local current = datasets[dataset]
204        for i=1,#sequence do
205            local step = sequence[i]
206            local dset = step.dataset
207            if dset == v_all or dset == dataset then
208                local register    = step.register
209                local processor   = step.processor
210                local alternative = shorts[step.alternative or "invertedshort"] or "invertedshort"
211                local k = writers.author { author }
212                local e = f_author(alternative,components(author,short))
213                ctx_dosetfastregisterentry(register,e,k,processor or "","")
214            end
215        end
216    end
217end
218
219publications.authortoregister = authortoregister
220
221implement {
222    name      = "btxauthortoregister",
223    actions   = authortoregister,
224    arguments = "2 strings",
225}
226