summaryrefslogtreecommitdiff
path: root/tex/context/base/regi-ini.lua
blob: 979a3999404ed46550a2c8654e4c0553d8de0613 (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
if not modules then modules = { } end modules ['regi-ini'] = {
    version   = 1.001,
    comment   = "companion to regi-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local utf = unicode.utf8
local char, utfchar, gsub = string.char, utf.char, string.gsub

--[[ldx--
<p>Regimes take care of converting the input characters into
<l n='utf'/> sequences. The conversion tables are loaded at
runtime.</p>
--ldx]]--

regimes          = regimes or { }
local regimes    = regimes

regimes.data     = regimes.data or { }
local data       = regimes.data

regimes.utf      = regimes.utf or { }

-- regimes.synonyms = regimes.synonyms or { }
-- local synonyms   = regimes.synonyms
--
-- if storage then
--     storage.register("regimes/synonyms", synonyms, "regimes.synonyms")
-- else
--     regimes.synonyms = { }
-- end

local synonyms = {

    ["windows-1250"]  = "cp1250",
    ["windows-1251"]  = "cp1251",
    ["windows-1252"]  = "cp1252",
    ["windows-1253"]  = "cp1253",
    ["windows-1254"]  = "cp1254",
    ["windows-1255"]  = "cp1255",
    ["windows-1256"]  = "cp1256",
    ["windows-1257"]  = "cp1257",
    ["windows-1258"]  = "cp1258",

    ["il1"]           = "8859-1",
    ["il2"]           = "8859-2",
    ["il3"]           = "8859-3",
    ["il4"]           = "8859-4",
    ["il5"]           = "8859-9",
    ["il6"]           = "8859-10",
    ["il7"]           = "8859-13",
    ["il8"]           = "8859-14",
    ["il9"]           = "8859-15",
    ["il10"]          = "8859-16",

    ["iso-8859-1"]    = "8859-1",
    ["iso-8859-2"]    = "8859-2",
    ["iso-8859-3"]    = "8859-3",
    ["iso-8859-4"]    = "8859-4",
    ["iso-8859-9"]    = "8859-9",
    ["iso-8859-10"]   = "8859-10",
    ["iso-8859-13"]   = "8859-13",
    ["iso-8859-14"]   = "8859-14",
    ["iso-8859-15"]   = "8859-15",
    ["iso-8859-16"]   = "8859-16",

    ["latin1"]        = "8859-1",
    ["latin2"]        = "8859-2",
    ["latin3"]        = "8859-3",
    ["latin4"]        = "8859-4",
    ["latin5"]        = "8859-9",
    ["latin6"]        = "8859-10",
    ["latin7"]        = "8859-13",
    ["latin8"]        = "8859-14",
    ["latin9"]        = "8859-15",
    ["latin10"]       = "8859-16",

    ["utf-8"]         = "utf",
    ["utf8"]          = "utf",

    ["windows"]       = "cp1252",

}

regimes.currentregime = "utf"

--[[ldx--
<p>We will hook regime handling code into the input methods.</p>
--ldx]]--

function regimes.number(n)
    if type(n) == "string" then return tonumber(n,16) else return n end
end

function regimes.setsynonym(synonym,target) -- more or less obsolete
    synonyms[synonym] = target
end

function regimes.truename(regime)
    context((regime and synonyms[synonym] or regime) or regimes.currentregime)
end

function regimes.load(regime)
    regime = synonyms[regime] or regime
    if not data[regime] then
        environment.loadluafile("regi-"..regime, 1.001)
        if data[regime] then
            regimes.utf[regime] = { }
            for k,v in next, data[regime] do
                regimes.utf[regime][char(k)] = utfchar(v)
            end
        end
    end
end

function regimes.translate(line,regime)
    regime = synonyms[regime] or regime
    if regime and line then
        local rur = regimes.utf[regime]
        if rur then
            return (gsub(line,"(.)",rur)) -- () redundant
        end
    end
    return line
end

local sequencers      = utilities.sequencers
local textfileactions = resolvers.openers.helpers.textfileactions

function regimes.process(s)
    return regimes.translate(s,regimes.currentregime)
end

function regimes.enable(regime)
    regime = synonyms[regime] or regime
    if data[regime] then
        regimes.currentregime = regime
        sequencers.enableaction(textfileactions,"regimes.process")
    else
        sequencers.disableaction(textfileactions,"regimes.process")
    end
end

function regimes.disable()
    regimes.currentregime = "utf"
    sequencers.disableaction(textfileactions,"regimes.process")
end

utilities.sequencers.prependaction(textfileactions,"system","regimes.process")
utilities.sequencers.disableaction(textfileactions,"regimes.process")