summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/lang-tra.lmt
blob: b3fedc7c1058b19142f7fa3014d38fffec178657 (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
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).

local concat = table.concat
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 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 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
        if t_mapping then
            local p = Cs ( (
                utfchartabletopattern(t_mapping) / t_mapping
              + C(utfcharacterpattern)
            )^0 )
         -- lpeg.print(p)
            return function(s)
                return lpegmatch(p,s) or s
            end
        else
            return false
        end
    end,
    ["default"] = function(m)
        return function(s)
            return 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 data then
            local transliterations = data.transliterations
            if transliterations then
                for name, d in table.sortedhash(transliterations) do
                    local vector = d.vector
                    if vector then
                        report("vector %a in %a is %sloaded with index %i",name,library," already",d.attribute)
                    else
                        lastmapping = lastmapping + 1
                        d.vector = (converters[name] or converters.mapping or converters.default)(d)
                                or (converters.default)(d)
                        report("vector %a in set %a is %sloaded with index %i",name,library,"",lastmapping)
                    end
                    d.attribute = lastmapping
                    d.library   = library
                    d.name      = name
                    loadedmappings[name] = d
                    loadedmappings[lastmapping] = 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.set(vector)
    if enable then
        enable()
    end
    local m = loadedmappings[vector]
    local a = m and m.attribute or unsetvalue
    if trace then
        report("setting transliteration %s",vector)
    end
    texsetattribute(a_transliteration,a)
end

function transliteration.register(vector)
    if enable then
        enable()
    end
    local m = loadedmappings[vector]
    local a = m and m.attribute or unsetvalue
    if trace then
        report("registering transliteration %s",vector)
    end
    return a
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 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)
        -- we can do some optimization here by having the split as replacement
        local old = concat(list,"",1,size)
        local new = vector(old)
        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

    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
                    vector = loadedmappings[a]
                    if vector then
                        vector = vector.vector
                    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 = "string",
    actions   = transliteration.set,
}

implement {
    name      = "registertransliteration",
    arguments = "string",
    actions   = { transliteration.register, context },
}

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,
}