summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/lang-tra.lmt
blob: 28a534dc36563663ee3c68e012bcc037ecc94acc (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
if not modules then modules = { } end modules ['lang-tra'] = {
    version   = 1.001,
    comment   = "companion to lang-tra.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- The indic transliterations was researched by kauśika and after some experiments
-- we settled on the current approach (mappings and a more specific lpeg).

-- Todo: initial and final in addition to mapping so that we can do hebrew and
-- such.

local concat, setmetatableindex = table.concat, table.setmetatableindex
local nospaces = string.nospaces
local utfbyte, utfchar, utfsplit, utfvalues = utf.byte, utf.char, utf.split, utf.values
local C, Cc, Cs, lpegmatch = lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.match
local utfchartabletopattern = lpeg.utfchartabletopattern
local utfcharacterpattern = lpeg.patterns.utf8character

local nuts                = nodes.nuts

local nextchar            = nuts.traversers.char

local getattr             = nuts.getattr
local setchar             = nuts.setchar
local getnext             = nuts.getnext
local isnextchar          = nuts.isnextchar

local insertafter         = nuts.insertafter
local copynode            = nuts.copy
local removenode          = nuts.remove

local texsetattribute     = tex.setattribute

local registervalue       = attributes.registervalue
local getvalue            = attributes.getvalue

local transliteration     = { }
languages.transliteration = transliteration

local a_transliteration   = attributes.private("transliteration")
local unsetvalue          = attributes.unsetvalue

local implement           = interfaces.implement
local context             = context

local zwj                 = utf.char(0x200D)

-- local lastmapping         = 0
local loadedmappings      = { }
local loadedlibraries     = { }
local exceptions          = { }

local report              = logs.reporter("transliteration")
local trace               = false  trackers.register("languages.transliteration", function(v) trace = v end)

local converters = {
--     ["iast to deva"] = function(m)
--         local t_svara          = m.svara
--         local p_svara          = utfchartabletopattern(t_svara)
--         local t_vyanjana       = m.vyanjana
--         local p_vyanjana       = utfchartabletopattern(t_vyanjana)
--         local t_maatra         = m.maatra
--         local p_maatra         = utfchartabletopattern(t_maatra)
--         local t_viraama        = m.viraama
--         local p_viraama        = utfchartabletopattern(t_viraama)
--         local t_boundary       = m.boundary
--         local p_boundary       = utfchartabletopattern(t_boundary)
--         local t_yogavaaha      = m.yogavaaha
--         local p_yogavaaha      = utfchartabletopattern(t_yogavaaha)
--         local p_svara_boundary = 1 - p_svara - p_vyanjana - p_yogavaaha
--         local p = Cs ( (
--             p_svara                     / t_svara
--           + p_vyanjana                  / t_vyanjana
--           + p_viraama                   / t_viraama
--           + p_yogavaaha                 / t_yogavaaha
--           + C(utfcharacterpattern)
--         )^0 )
--         return function(s)
--             -- for now
-- --             s = zwj .. s
--             --
--             return lpegmatch(p,s) or s
--         end
--     end,
    ["mapping"] = function(m)
        local t_mapping = m.mapping
--         inspect(t_mapping)
        if t_mapping then
            local t_exceptions = m.exceptions
            local p = Cs ( (
                utfchartabletopattern(t_mapping) / t_mapping
              + C(utfcharacterpattern)
            )^0 )
--          lpeg.print(p)
            return function(s,e)
                return (e and e[s]) or t_exceptions[s] or lpegmatch(p,s) or s
            end
        else
            return false
        end
    end,
    ["default"] = function(m)
        local t_exceptions = m.exceptions
        return function(s,e)
            return (e and e[s]) or t_exceptions[s] or s
        end
    end,
}

function transliteration.use(library)
    local lib = loadedlibraries[library]
    if lib == nil then
        -- todo: use library loader
        local data = require("lang-imp-" .. library)
        if type(data) == "table" then
            local transliterations = data.transliterations
            if transliterations then
                for name, d in table.sortedhash(transliterations) do
                    local vector = d.vector
                    if not d.exceptions then
                        d.exceptions = { }
                    end
                    if vector then
                        report("vector %a in %a is %sloaded",name,library," already")
                    else
                        d.vector = (converters[name] or converters.mapping or converters.default)(d)
                                or (converters.default)(d)
                        report("vector %a in set %a is %sloaded",name,library,"")
                    end
                    d.library   = library
                    d.name      = name
                    d.mapping   = nil -- for now, saves memory
                    loadedmappings[name] = d
                    loadedmappings[nospaces(name)] = d
                end
            else
                report("library %a has no transliterations",library)
            end
            loadedlibraries[library] = data
        else
            loadedlibraries[library] = false
        end
    end
end

local enable = false

enable = function()
    nodes.tasks.enableaction("processors", "languages.transliteration.handler")
    enable = false
end

function transliteration.register(parent,name)
    local p = exceptions[parent]
    if p then
        if trace then
            report("%a has exceptions that default to %a",name,parent)
        end
        exceptions[name] = setmetatableindex({ },p)
    else
        if trace then
            report("%a has independent exceptions",name)
        end
        exceptions[name] = { }
    end
end

function transliteration.set(name,vector)
    if enable then
        enable()
    end
    local a = registervalue(a_transliteration, {
        m = loadedmappings[vector],
        e = exceptions[name],
    })
    if trace then
        report("setting transliteration %i, name %a, vector %a",a,name,vector)
    end
    texsetattribute(a_transliteration,a)
end

function transliteration.exception(name,old,new)
    local m = loadedmappings[name]
    if m then
        m.exceptions[old] = new
    else
        local e = exceptions[name]
        if not e then
            e = { }
            exceptions[name] = e
        end
        e[old] = new
    end
end

-- When there is need I will improve the performance of the next handler.

function transliteration.handler(head)
    local aprev   = nil
    local vector  = nil
    local except  = nil
    local current = head
    local first   = nil
    local last    = nil
    local list    = { }
    local size    = 0

    -- we need a more clever one: run over small ranges in order to keep colors etc

    -- actually we can generalize the replacer elsewhere

    local function flush(nxt)
        if vector then
            -- we can do some optimization here by having the split as replacement
            local old = concat(list,"",1,size)
            local new = vector(old,except)
            if old ~= new then
                if trace then
                    report("old: %s",old)
                    report("new: %s",new)
                end
                local c = first
                local x = false
                for s in utfvalues(new) do
                    if x then
                        head, c = insertafter(head,c,copynode(first))
                        setchar(c,s)
                    else
                        setchar(c,s)
                        if c == last then
                            x = true
                        else
                            c = getnext(c)
                        end
                    end
                end
                if not x then
                    while c do
                        head, c = removenode(head,c,true)
                        if c == nxt then
                            break
                        end
                    end
                end
            end
        end
    end

    while current do
        local nxt, chr, more = isnextchar(current)
        if chr then
            local a = getattr(current,a_transliteration)
            if a then
                if a ~= aprev then
                    if first then
                        flush(nxt)
                        first = nil
                        size  = 0
                    end
                    aprev = a
                    local data = getvalue(a_transliteration,a)
                    if data then
                        local m = data.m
                        if m then
                            vector = m.vector
                            except = data.e
                        else
                            vector = nil
                            except = nil
                        end
                    else
                        vector = nil
                        except = nil
                    end
                end
                if not first then
                    first = current
                end
                last = current
                size = size + 1
                list[size] = utfchar(chr)
                if not more then
                    flush(nxt)
                    first = nil
                    size  = 0
                    -- we can go ahead one next
                end
            else
                if first then
                    flush(nxt)
                    first = nil
                    size  = 0
                end
            end
        end
        current = nxt
    end
    if first then
       flush(nxt)
    end
    return head
end

interfaces.implement {
    name      = "usetransliteration",
    public    = true,
    protected = true,
    arguments = "optional",
    actions   = transliteration.use,
}

implement {
    name      = "settransliteration",
    arguments = "2 strings",
    actions   = transliteration.set,
}

implement {
    name      = "registertransliteration",
    arguments = "3 strings",
    actions   = transliteration.register,
}

implement {
    name      = "transliterationexception",
    arguments = "3 strings",
    actions   = transliteration.exception,
}

nodes.tasks.prependaction("processors", "normalizers", "languages.transliteration.handler", nil, "nut", "disabled" )

local function transliterate(scheme,str)
    if str and str ~= "" then
        local m = loadedmappings[scheme]
        local c = m and m.vector
        context(c and c(str) or str)
    end
end

local getbuffer = buffers.getcontent

implement {
    name      = "transliterate",
    public    = true,
    protected = true,
    arguments = { "optional", "string" },
    actions   = transliterate,
}

implement {
    name      = "transliteratebuffer",
    public    = true,
    protected = true,
    arguments = { "optional", "string" },
    actions   = function(scheme,name) transliterate(scheme,getbuffer(name)) end,
}

implement {
    name      = "transliterated",
    public    = true,
    arguments = { "optional", "string" },
    actions   = transliterate,
}

implement {
    name      = "transliteratedbuffer",
    public    = true,
    arguments = { "optional", "string" },
    actions   = function(scheme,name) transliterate(scheme,getbuffer(name)) end,
}