lang-imp-simpleascii.lua /size: 3117 b    last modification: 2024-01-16 10:22
1-- The data is taken from:
2--
3--   https://github.com/anyascii/anyascii/blob/master/table.tsv
4--
5-- by Hunter WB under the ISC License (2020-2023).
6--
7-- Updating:
8--
9-- -- copy table.tsv to lang-imp-simpleascii-data.tsv
10-- -- mtxrun --script lang-imp-simpleascii
11-- -- copy lang-imp-simpleascii-data.lgz over old file
12--
13-- Usage:
14--
15-- \usetransliteration[simpleascii]
16--
17-- \definetransliteration
18--     [simpleascii]
19--     [color=blue,
20--      vector={simple ascii}]
21--
22-- \settransliteration[simpleascii]
23--
24-- \starttext
25--
26-- \startchapter[title={深圳 ଗଜପତି Blöße}]
27--     深圳 ଗଜପତି Blöße\par
28--     深圳 ଗଜପତି Blöße\par
29--     深圳 ଗଜପତି Blöße\par
30--     深圳 ଗଜପତି Blöße\par
31-- \stopchapter
32--
33-- \stoptext
34
35local textfile = "lang-imp-simpleascii-data.tsv"  -- a copy of table.tsv
36local datafile = "lang-imp-simpleascii-data.lua"  -- for tracing
37local compfile = "lang-imp-simpleascii-data.lgz"  -- idem in distribution
38
39local verbose = false -- when true, saved uncompressed file for tracing
40local report  = logs.reporter("simpleascii")
41
42if not context and lfs.isfile(textfile) then
43
44    -- We save it in the local path so we need to move it explicitly into
45    -- the tree which prevents errors.
46
47    local data = io.loaddata(textfile)
48    if data and data ~= "" then
49        local mapping = { }
50        for k, v in string.gmatch(data,"(%S+)[\t]*([^\n\r]-)[\n\r]") do
51            if k ~= "" and v ~= "" and k ~= v then
52                mapping[k] = v
53            end
54        end
55        if verbose then
56            table.save(datafile,mapping)
57        else
58            mapping  = gzip.compress(table.fastserialize(mapping)) -- zlib.compress(d,9)
59            datafile = compfile
60            io.savedata(compfile,mapping)
61        end
62        report("data from %a saved in %a",textfile,datafile)
63    else
64        report("no data file %a",textfile)
65    end
66
67else
68
69    local mapping  = false
70
71    if not verbose then
72        mapping = io.loaddata(resolvers.findfile(compfile) or "")
73        if mapping then
74            mapping  = table.deserialize(gzip.decompress(mapping)) -- zlib.decompress(d)
75            if mapping then
76                datafile = compfile
77            else
78                report("data file %a is corrupt",compfile)
79            end
80        end
81    end
82    if not mapping then
83        mapping = table.load(resolvers.findfile(datafile) or "")
84    end
85
86    if mapping then
87
88        report("data file %a loaded",datafile)
89
90     -- for i = 0, 127 do
91     --     mapping[utfchar(i)] = utfchar(i) -- not needed
92     -- end
93
94        return {
95
96            name      = "simple ascii",
97            version   = "1.00",
98            comment   = "Unicode to ASCII transliteration",
99            author    = "Jairo A. del Rio & Hans Hagen",
100            copyright = "ConTeXt development team & whoever made this list",
101
102            transliterations = {
103                ["simple ascii"] = {
104                    mapping = mapping
105                },
106            }
107
108        }
109
110    else
111        report("no data file %a",datafile)
112    end
113
114end
115