summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/lang-imp-simpleascii.lua
blob: 8fe664a276758860880edad671cf0fd55c7fbe30 (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
-- The data is taken from:
--
--   https://github.com/anyascii/anyascii/blob/master/table.tsv
--
-- by Hunter WB under the ISC License (2020-2023).
--
-- Updating:
--
-- -- copy table.tsv to lang-imp-simpleascii-data.tsv
-- -- mtxrun --script lang-imp-simpleascii
-- -- copy lang-imp-simpleascii-data.lgz over old file
--
-- Usage:
--
-- \usetransliteration[simpleascii]
--
-- \definetransliteration
--     [simpleascii]
--     [color=blue,
--      vector={simple ascii}]
--
-- \settransliteration[simpleascii]
--
-- \starttext
--
-- \startchapter[title={深圳 ଗଜପତି Blöße}]
--     深圳 ଗଜପତି Blöße\par
--     深圳 ଗଜପତି Blöße\par
--     深圳 ଗଜପତି Blöße\par
--     深圳 ଗଜପତି Blöße\par
-- \stopchapter
--
-- \stoptext

local textfile = "lang-imp-simpleascii-data.tsv"  -- a copy of table.tsv
local datafile = "lang-imp-simpleascii-data.lua"  -- for tracing
local compfile = "lang-imp-simpleascii-data.lgz"  -- idem in distribution

local verbose = false -- when true, saved uncompressed file for tracing
local report  = logs.reporter("simpleascii")

if not context and lfs.isfile(textfile) then

    -- We save it in the local path so we need to move it explicitly into
    -- the tree which prevents errors.

    local data = io.loaddata(textfile)
    if data and data ~= "" then
        local mapping = { }
        for k, v in string.gmatch(data,"(%S+)[\t]*([^\n\r]-)[\n\r]") do
            if k ~= "" and v ~= "" and k ~= v then
                mapping[k] = v
            end
        end
        if verbose then
            table.save(datafile,mapping)
        else
            mapping  = gzip.compress(table.fastserialize(mapping)) -- zlib.compress(d,9)
            datafile = compfile
            io.savedata(compfile,mapping)
        end
        report("data from %a saved in %a",textfile,datafile)
    else
        report("no data file %a",textfile)
    end

else

    local mapping  = false

    if not verbose then
        mapping = io.loaddata(resolvers.findfile(compfile) or "")
        if mapping then
            mapping  = table.deserialize(gzip.decompress(mapping)) -- zlib.decompress(d)
            if mapping then
                datafile = compfile
            else
                report("data file %a is corrupt",compfile)
            end
        end
    end
    if not mapping then
        mapping = table.load(resolvers.findfile(datafile) or "")
    end

    if mapping then

        report("data file %a loaded",datafile)

     -- for i = 0, 127 do
     --     mapping[utfchar(i)] = utfchar(i) -- not needed
     -- end

        return {

            name      = "simple ascii",
            version   = "1.00",
            comment   = "Unicode to ASCII transliteration",
            author    = "Jairo A. del Rio & Hans Hagen",
            copyright = "ConTeXt development team & whoever made this list",

            transliterations = {
                ["simple ascii"] = {
                    mapping = mapping
                },
            }

        }

    else
        report("no data file %a",datafile)
    end

end