summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/core-reg.lua
blob: f490f2ccaab2d416f170b53e6fe7c70d72339acf (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
-- filename : core-reg.lua
-- comment  : companion to core-reg.tex
-- author   : Hans Hagen, PRAGMA-ADE, Hasselt NL
-- copyright: PRAGMA ADE / ConTeXt Development Team
-- license  : see context related readme files

if not versions      then versions      = { } end versions['core-reg'] = 1.001
if not jobs          then jobs          = { } end
if not job           then jobs['main']  = { } end job = jobs['main']
if not job.registers then job.registers = { } end

function job.defineregister(id)
    if not job.registers[id] then
        job.registers[id] = { }
    end
end

-- {'e','3','','test+test+test','2--0-0-0-0-0-0-0--1','1'}

-- load index (we could rease the original entry afterwards, freeing memory)

-- index sorting

sorters            = sorters            or { }
sorters.index      = sorters.index      or { }
sorters.index.data = sorters.index.data or { }

do

    function sorters.index.compare(a,b)
        local result = 0
        for i=1,4 do
            if result == 0 then
                result = sorters.comparers.basic(a,b,i)
            else
                return result
            end
        end
        if a[1] ~= 's' then -- e/f/t
            local page_a, page_b = a[3], b[3]
            if page_a < page_b then
                return -1
            elseif page_a > page_b then
                return  1
            end
        end
        return 0
    end

    function sorters.index.prepare(data)
        sorters.prepare(data,sorters.splitters.utf,4)
    end

    function sorters.index.sort(data)
        sorters.sort(data,sorters.index.compare)
    end

    function sorters.index.unique(data)
        sorters.unique(data)
    end

    function sorters.index.cleanup(data)
        sorters.cleanup(data)
    end

    function sorters.index.finalize(data)
        local split = { }
        for k,v in ipairs(data) do
            local entry, tag = v[2][1][3][1], ""
            local se = sorters.entries[sorters.language]
            if se and se[entry] then
                if type(se[entry]) == "number" then
                    entry = se[entry]
                end
                tag = se[entry]
            else
                entry = 0
                tag = "unknown"
            end
            split[entry] = split[entry] or { tag = tag, data = { } }
            split[entry].data[#split[entry].data+1] = v
        end
        return split
    end

    -- \registerpage{index}{,}{6}{2--0-0-0-0-0-0-0--1}{1}

    -- for the moment we use the old structure, some day mkiv code
    -- will be different: more structure, less mess

    local template = {
        page = "\\registerpage{%s}{%s}{%s}{%s}{%s}",
        see = "\\registersee{%s}{%s}{%s}{%s}",
        letter = "\\registerentry{%s}{%s}",
        entry = {
            "\\registerentrya{%s}{%s}",
            "\\registerentryb{%s}{%s}",
            "\\registerentryc{%s}{%s}",
            "\\registerentryd{%s}{%s}",
        },
    }

    function sorters.index.flush(sorted,class,flush)
        flush = flush or print
        class = class or 'index'
        for k,v in ipairs(table.sortedkeys(sorted)) do
            local s = sorted[v]
            flush(template.letter:format(class,s.tag))
            local done = { false, false, false }
            for kk,vv in ipairs(s.data) do
                if vv[2][1] then
                    local e = { false, false, false, false }
                    for i=1,4,1 do
                        if vv[2][i] then
                            e[i] = vv[2][i][1]
                        end
                        if e[i] ~= done[i] then
                            if e[i] and e[i] ~= "" then
                                done[i] = e[i]
                                flush(template.entry[i]:format(class,e[i]))
                            else
                                done[i] = false
                            end
                        end
                    end
                    if vv[1] == 'e' then
                        -- format reference pagespec realpage
                        flush(template.page:format(class,",",vv[4],vv[5],vv[3]))
                    elseif vv[1] == 's' then
                        flush(template.see:format(class,",",vv[5],vv[3]))
                    end
                end
            end
        end
    end

    function sorters.index.process(data)
        return sorters.process('index',data)
    end

end

-- { { entry, key }, { entry, key }, { entry, key }, { entry, key } }, kind, realpage|see, reference, pagespec

function job.loadregister(class)
    if job.registers[class] then
        if not sorters.index.data[class] then
            sorters.index.data[class] = {
                language = 'en',
                entries  = { },
                flush    = function(s) tex.sprint(tex.ctxcatcodes,s) end,
                sorted   = false,
                class    = class
            }
            local entries = sorters.index.data[class].entries
            for k,v in ipairs(job.registers[class]) do
                if v[1] == 'l' then -- language
                    sorters.index.data[class].language = v[2]
                else
                    local key, entry = v[3], v[4]
                    if type(entry) == 'string' then
                        entry = entry:splitchr('+')
                    end
                    if type(key) == 'string' then
                        key = key:splitchr('+')
                    end
                    entries[#entries+1] = {
                        v[1], -- kind (e, f, t, s)
                        {
                            { entry[1] or "", key[1] or "" },
                            { entry[2] or "", key[2] or "" },
                            { entry[3] or "", key[3] or "" },
                            { entry[4] or "", key[4] or "" }
                        },
                        v[6], -- realpage or seeword (check see)
                        v[2], -- reference
                        v[5], -- pagespec
                    }
                end
            end
        end
        -- maybe we should also save the register result stream
        sorters.index.process(sorters.index.data[class])
    end
end