summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/publ-reg.lua
blob: 3f276b49abc0b5fbd6245ac26d931a0389e32965 (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
if not modules then modules = { } end modules ['publ-reg'] = {
    version   = 1.001,
    comment   = "this module part of publication support",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local formatters = string.formatters
local concat     = table.concat
local sortedhash = table.sortedhash
local lpegmatch  = lpeg.match

local context        = context

local implement      = interfaces.implement
local variables      = interfaces.variables

local v_once         = variables.once
local v_stop         = variables.stop
local v_all          = variables.all

local publications   = publications
local datasets       = publications.datasets
local specifications = publications.specifications
local writers        = publications.writers
local getcasted      = publications.getcasted

local registrations  = { }
local sequence       = { }
local flushers       = table.setmetatableindex(function(t,k) local v = t.default t[k] = v return v end)

local function btxsetregister(specification)
    local name     = specification.name
    local register = specification.register
    local dataset  = specification.dataset
    local field    = specification.field
    if not field or field == "" or not register or register == "" then
        return
    end
    if not dataset or dataset == "" then
        dataset = v_all
    end
    -- could be metatable magic
    local s = registrations[register]
    if not s then
        s = { }
        registrations[register] = s
    end
    local processors = name ~= register and name or ""
    if processor == "" then
        processor = nil
    elseif processor then
        processor = "btx:r:" .. processor
    end
    local datasets = utilities.parsers.settings_to_array(dataset)
    for i=1,#datasets do
        local dataset = datasets[i]
        local d = s[dataset]
        if not d then
            d = { }
            s[dataset] = d
        end
        --
        -- check all
        --
        d.active      = specification.state ~= v_stop
        d.once        = specification.method == v_once or false
        d.field       = field
        d.processor   = processor
        d.alternative = d.alternative or specification.alternative
        d.register    = register
        d.dataset     = dataset
        d.done        = d.done or { }
    end
    --
    sequence   = { }
    for register, s in sortedhash(registrations) do
        for dataset, d in sortedhash(s) do
            if d.active then
                sequence[#sequence+1] = d
            end
        end
    end
end

local function btxtoregister(dataset,tag)
    local current = datasets[dataset]
    for i=1,#sequence do
        local step = sequence[i]
        local dset = step.dataset
        if dset == v_all or dset == dataset then
            local done = step.done
            if not done[tag] then
                local value, field, kind = getcasted(current,tag,step.field,specifications[step.specification])
                if value then
                    flushers[kind](step,field,value)
                end
                done[tag] = true
            end
        end
    end
end

implement {
    name      = "btxsetregister",
    actions   = btxsetregister,
    arguments = {
        {
            { "specification" },
            { "name" },
            { "state" },
            { "dataset" },
            { "field" },
            { "register" },
            { "method" },
            { "alternative" },
        }
    }
}

implement {
    name      = "btxtoregister",
    actions   = btxtoregister,
    arguments = { "string", "string" }
}

-- context.setregisterentry (
--     { register },
--     {
--         ["entries:1"] = value,
--         ["keys:1"]    = value,
--     }
-- )

local ctx_dosetfastregisterentry = context.dosetfastregisterentry -- register entry key

----- p_keywords = lpeg.tsplitat(lpeg.patterns.whitespace^0 * lpeg.P(";") * lpeg.patterns.whitespace^0)
local components = publications.components.author
local f_author   = formatters[ [[\btxindexedauthor{%s}{%s}{%s}{%s}{%s}{%s}]] ]

function flushers.string(step,field,value)
    if type(value) == "string" and value ~= "" then
        ctx_dosetfastregisterentry(step.register,value or "","",step.processor or "","")
    end
end

flushers.default = flushers.string

local shorts = {
    normalshort   = "normalshort",
    invertedshort = "invertedshort",
}

function flushers.author(step,field,value)
    if type(value) == "string" then
        value = publications.authorcache[value]
    end
    if type(value) == "table" and #value > 0 then
        local register    = step.register
        local processor   = step.processor
        local alternative = shorts[step.alternative or "invertedshort"] or "invertedshort"
        for i=1,#value do
            local a = value[i]
            local k = writers[field] { a }
            local e = f_author(alternative,components(a))
            ctx_dosetfastregisterentry(register,e,k,processor or "","")
        end
    end
end

function flushers.keyword(step,field,value)
    if type(value) == "table" and #value > 0 then
        local register  = step.register
        local processor = step.processor
        for i=1,#value do
            ctx_dosetfastregisterentry(register,value[i],"",processor or "","")
        end
    end
end

-- publications.registerflushers = flushers

local function btxtoregister(dataset,tag)
    local current = datasets[dataset]
    for i=1,#sequence do
        local step = sequence[i]
        local dset = step.dataset
        if dset == v_all or dset == dataset then
            local done = step.done
            if not done[tag] then
                local value, field, kind = getcasted(current,tag,step.field,specifications[step.specification])
                if value then
                    flushers[kind](step,field,value)
                end
                done[tag] = true
            end
        end
    end
end

local function authortoregister(dataset,hash)
    local author = publications.authorcache[hash]
    if author then
        local current = datasets[dataset]
        for i=1,#sequence do
            local step = sequence[i]
            local dset = step.dataset
            if dset == v_all or dset == dataset then
                local register    = step.register
                local processor   = step.processor
                local alternative = shorts[step.alternative or "invertedshort"] or "invertedshort"
                local k = writers.author { author }
                local e = f_author(alternative,components(author,short))
                ctx_dosetfastregisterentry(register,e,k,processor or "","")
            end
        end
    end
end

publications.authortoregister = authortoregister

implement {
    name      = "btxauthortoregister",
    actions   = authortoregister,
    arguments = { "string", "string" }
}