summaryrefslogtreecommitdiff
path: root/tex/context/base/publ-reg.lua
blob: 3df853736cd2650fa2a83ddb3966feea36a1242f (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
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 commands       = commands

local variables      = interfaces.variables

local v_once         = variables.once
local v_standard     = variables.standard
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)

function commands.setbtxregister(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 d = s[dataset]
    if not d then
        d = { }
        s[dataset] = d
    end
    --
    -- check all
    --
    local processors = name ~= register and name or ""
    if processor == "" then
        processor = nil
    elseif processor then
        processor = "btx:r:" .. processor
    end
    --
    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 { }
    --
    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

function commands.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

-- 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) == "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,short))
            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