summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/lua/mtx-babel.lua
blob: 8c11465dea4d54e4047c18cb34cf8f9d05feb276 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
if not modules then modules = { } end modules ['mtx-babel'] = {
    version   = 1.002,
    comment   = "companion to mtxrun.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- data tables by Thomas A. Schmitz

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-babel</entry>
  <entry name="detail">Babel Input To UTF Conversion</entry>
  <entry name="version">1.20</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="language" value="string"><short>conversion language (e.g. greek)</short></flag>
    <flag name="structure" value="string"><short>obey given structure (e.g. 'document', default: 'context')</short></flag>
    <flag name="convert"><short>convert babel codes into utf</short></flag>
   </subcategory>
  </category>
 </flags>
</application>
]]

local application = logs.application {
    name     = "mtx-babel",
    banner   = "Babel Input To UTF Conversion 1.20",
    helpinfo = helpinfo,
}

local report = application.report

scripts       = scripts       or { }
scripts.babel = scripts.babel or { }

do

    local converters = { }

    -- greek

    local replace_01 = { -- <' * |
        a = "ᾅ",
        h = "ᾕ",
        w = "ᾥ",
    }

    local replace_02 = { -- >' * |
        a = "ᾄ",
        h = "ᾔ",
        w = "ᾤ",
    }

    local replace_03 = { -- <` * |
        a = "ᾃ",
        h = "ᾓ",
        w = "ᾣ",
    }

    local replace_04 = { -- >` * |
        a = "ᾂ",
        h = "ᾒ",
        w = "ᾢ",
    }

    local replace_05 = { -- <~ * |
        a = "ᾇ",
        h = "ᾗ",
        w = "ᾧ",
    }

    local replace_06 = { -- >~ * |
        a = "ᾆ",
        h = "ᾖ",
        w = "ᾦ"
    }

    local replace_07 = { -- "' *
        i = "ΐ",
        u = "ΰ",
    }

    local replace_08 = { -- "` *
        i = "ῒ",
        u = "ῢ",
    }

    local replace_09 = { -- "~ *
        i = "ῗ",
        u = "ῧ",
    }

    local replace_10 = { -- <' *
        a = "ἅ",
        e = "ἕ",
        h = "ἥ",
        i = "ἵ",
        o = "ὅ",
        u = "ὕ",
        w = "ὥ",
        A = "Ἅ",
        E = "Ἕ",
        H = "Ἥ",
        I = "Ἵ",
        O = "Ὅ",
        U = "Ὕ",
        W = "Ὥ",
    }

    local replace_11 = { -- >' *
        a = "ἄ",
        e = "ἔ",
        h = "ἤ",
        i = "ἴ",
        o = "ὄ",
        u = "ὔ",
        w = "ὤ",
        A = "Ἄ",
        E = "Ἔ",
        H = "Ἤ",
        I = "Ἴ",
        O = "Ὄ",
        U = "῎Υ",
        W = "Ὤ",
    }

    local replace_12 = { -- <` *
        a = "ἃ",
        e = "ἓ",
        h = "ἣ",
        i = "ἳ",
        o = "ὃ",
        u = "ὓ",
        w = "ὣ",
        A = "Ἃ",
        E = "Ἒ",
        H = "Ἣ",
        I = "Ἳ",
        O = "Ὃ",
        U = "Ὓ",
        W = "Ὣ",
    }

    local replace_13 = { -- >` *
        a = "ἂ",
        e = "ἒ",
        h = "ἢ",
        i = "ἲ",
        o = "ὂ",
        u = "ὒ",
        w = "ὢ",
        A = "Ἂ",
        E = "Ἒ",
        H = "Ἢ",
        I = "Ἲ",
        O = "Ὂ",
        U = "῍Υ",
        W = "Ὢ",
    }

    local replace_14 = { -- <~ *
        a = "ἇ",
        h = "ἧ",
        i = "ἷ",
        u = "ὗ",
        w = "ὧ",
        A = "Ἇ",
        H = "Ἧ",
        I = "Ἷ",
        U = "Ὗ",
        W = "Ὧ",
    }

    local replace_15 = { -- >~ *
        a = "ἆ",
        h = "ἦ",
        i = "ἶ",
        u = "ὖ",
        w = "ὦ",
        A = "Ἆ",
        H = "Ἦ",
        I = "Ἶ",
        U = "῏Υ",
        W = "Ὦ",
    }

    local replace_16 = { -- ' * |
        a = "ᾴ",
        h = "ῄ",
        w = "ῴ",
    }

    local replace_17 = { -- ` * |
        a = "ᾲ",
        h = "ῂ",
        w = "ῲ",
    }

    local replace_18 = { -- ~ * |
        a = "ᾷ",
        h = "ῇ",
        w = "ῷ"
    }

    local replace_19 = { -- ' *
        a = "ά",
        e = "έ",
        h = "ή",
        i = "ί",
        o = "ό",
        u = "ύ",
        w = "ώ",
    ["'"] = "’",
    }

    local replace_20 = { -- ` *
        a = "ὰ",
        e = "ὲ",
        h = "ὴ",
        i = "ὶ",
        o = "ὸ",
        u = "ὺ",
        w = "ὼ",
    }

    local replace_21 = { -- ~ *
        a = "ᾶ",
        h = "ῆ",
        i = "ῖ",
        u = "ῦ",
        w = "ῶ",
    }

    local replace_22 = { -- < *
        a = "ἁ",
        e = "ἑ",
        h = "ἡ",
        i = "ἱ",
        o = "ὁ",
        u = "ὑ",
        w = "ὡ",
        r = "ῥ",
        A = "Ἁ",
        E = "Ἑ",
        H = "Ἡ",
        I = "Ἱ",
        O = "Ὁ",
        U = "Ὑ",
        W = "Ὡ",
        R = "Ῥ",
    }

    local replace_23 = { -- > *
        a = "ἀ",
        e = "ἐ",
        h = "ἠ",
        i = "ἰ",
        o = "ὀ",
        u = "ὐ",
        w = "ὠ",
        A = "Ἀ",
        E = "Ἐ",
        H = "Ἠ",
        I = "Ἰ",
        O = "Ὀ",
        U = "᾿Υ",
        W = "Ὠ",
    }

    local replace_24 = { -- * |
        a = "ᾳ",
        h = "ῃ",
        w = "ῳ",
    }

    local replace_25 = { -- " *
        i = "ϊ",
        u = "ϋ",
    }

    local replace_26 = { -- *
        a = "α",
        b = "β",
        g = "γ",
        d = "δ",
        e = "ε",
        z = "ζ",
        h = "η",
        j = "θ",
        i = "ι",
        k = "κ",
        l = "λ",
        m = "μ",
        n = "ν",
        x = "ξ",
        o = "ο",
        p = "π",
        r = "ρ",
        s = "σ",
        c = "ς",
        t = "τ",
        u = "υ",
        f = "φ",
        q = "χ",
        y = "ψ",
        w = "ω",
        A = "Α",
        B = "Β",
        G = "Γ",
        D = "Δ",
        E = "Ε",
        Z = "Ζ",
        H = "Η",
        J = "Θ",
        I = "Ι",
        K = "Κ",
        L = "Λ",
        M = "Μ",
        N = "Ν",
        X = "Ξ",
        O = "Ο",
        P = "Π",
        R = "Ρ",
        S = "Σ",
        T = "Τ",
        U = "Υ",
        F = "Φ",
        Q = "Χ",
        Y = "Ψ",
        W = "Ω",
    [";"] = "·",
    ["?"] = ";",
    }

    local P, R, S, V, Cs = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.Cs

    local skips_01 = P("\\")   * R("az", "AZ")^1
    local skips_02 = P("[")    * (1- S("[]"))^1  * P("]")

    local greek_01 = (P("<'")  * Cs(1) * P('|')) / replace_01
    local greek_02 = (P(">'")  * Cs(1) * P('|')) / replace_02
    local greek_03 = (P("<`")  * Cs(1) * P('|')) / replace_03
    local greek_04 = (P(">`")  * Cs(1) * P('|')) / replace_04
    local greek_05 = (P("<~")  * Cs(1) * P('|')) / replace_05
    local greek_06 = (P(">~")  * Cs(1) * P('|')) / replace_06
    local greek_07 = (P('"\'') * Cs(1)         ) / replace_07
    local greek_08 = (P('"`')  * Cs(1)         ) / replace_08
    local greek_09 = (P('"~')  * Cs(1)         ) / replace_09
    local greek_10 = (P("<'")  * Cs(1)         ) / replace_10
    local greek_11 = (P(">'")  * Cs(1)         ) / replace_11
    local greek_12 = (P("<`")  * Cs(1)         ) / replace_12
    local greek_13 = (P(">`")  * Cs(1)         ) / replace_13
    local greek_14 = (P("<~")  * Cs(1)         ) / replace_14
    local greek_15 = (P(">~")  * Cs(1)         ) / replace_15
    local greek_16 = (P("'")   * Cs(1) * P('|')) / replace_16
    local greek_17 = (P("`")   * Cs(1) * P('|')) / replace_17
    local greek_18 = (P("~")   * Cs(1) * P('|')) / replace_18
    local greek_19 = (P("'")   * Cs(1)         ) / replace_19
    local greek_20 = (P("`")   * Cs(1)         ) / replace_20
    local greek_21 = (P("~")   * Cs(1)         ) / replace_21
    local greek_22 = (P("<")   * Cs(1)         ) / replace_22
    local greek_23 = (P(">")   * Cs(1)         ) / replace_23
    local greek_24 = (Cs(1)    * P('|')        ) / replace_24
    local greek_25 = (P('"')   * Cs(1)         ) / replace_25
    local greek_26 = (Cs(1)                    ) / replace_26

    local skips =
        skips_01 + skips_02

    local greek =
        greek_01 + greek_02 + greek_03 + greek_04 + greek_05 +
        greek_06 + greek_07 + greek_08 + greek_09 + greek_10 +
        greek_11 + greek_12 + greek_13 + greek_14 + greek_15 +
        greek_16 + greek_17 + greek_18 + greek_19 + greek_20 +
        greek_21 + greek_22 + greek_23 + greek_24 + greek_25 +
        greek_26

    local spacing      = S(" \n\r\t")
    local startgreek   = P("\\startgreek")
    local stopgreek    = P("\\stopgreek")
    local localgreek   = P("\\localgreek")
    local lbrace       = P("{")
    local rbrace       = P("}")

    local documentparser = Cs((skips + greek + 1)^0)

    local contextgrammar = Cs ( P { "scan",
        ["scan"]     = (V("global") + V("local") + skips + 1)^0,
        ["global"]   = startgreek * ((skips + greek + 1)-stopgreek )^0 ,
        ["local"]    = localgreek * V("grouped"),
        ["grouped"]  = spacing^0 * lbrace * (V("grouped") + skips + (greek - rbrace))^0 * rbrace,
    } )

    converters['greek'] = {
        document = documentparser,
        context  = contextgrammar,
    }

    -- lpeg.print(parser): 254 lines

    function scripts.babel.convert(filename)
        if filename and filename ~= empty then
            local data = io.loaddata(filename) or ""
            if data ~= "" then
                local language  = environment.argument("language")  or ""
                if language ~= "" then
                    local converter = converters[language]
                    if converter then
                        local structure = environment.argument("structure") or "document"
                        converter = converter[structure]
                        if converter then
                            report("converting '%s' using language '%s' with structure '%s'", filename, language, structure)
                            data = converter:match(data)
                            local newfilename = filename .. ".utf"
                            io.savedata(newfilename, data)
                            report("converted data saved in '%s'", newfilename)
                        else
                            report("unknown structure '%s' language '%s'", structure, language)
                        end
                    else
                        report("no converter for language '%s'", language)
                    end
                else
                    report("provide language")
                end
            else
                report("no data in '%s'",filename)
            end
        end
    end

    --~ print(contextgrammar:match [[
    --~ oeps abg \localgreek{a}
    --~ \startgreek abg \stopgreek \oeps
    --~ oeps abg \localgreek{a{b}\oeps g}
    --~ ]])

end

if environment.argument("convert") then
    scripts.babel.convert(environment.files[1] or "")
elseif environment.argument("exporthelp") then
   application.export(environment.argument("exporthelp"),environment.files[1])
else
    application.help()
end