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

-- special functions that deal with io

local format, lower = string.format, string.lower
local unpack = unpack or table.unpack

local trace_locating = false trackers.register("resolvers.locating", function(v) trace_locating = v end)

local report_resolvers = logs.new("resolvers")

local resolvers = resolvers

local finders, openers, loaders = resolvers.finders, resolvers.openers, resolvers.loaders

local checkgarbage = utilities.garbagecollector and utilities.garbagecollector.check

function finders.generic(tag,filename,filetype)
    local foundname = resolvers.findfile(filename,filetype)
    if foundname and foundname ~= "" then
        if trace_locating then
            report_resolvers("%s finder: file '%s' found",tag,filename)
        end
        return foundname
    else
        if trace_locating then
            report_resolvers("%s finder: unknown file '%s'",tag,filename)
        end
        return unpack(finders.notfound)
    end
end

-- -- keep this one as reference as it's the first version
--
-- resolvers.filters = resolvers.filters or { }
--
-- local input_translator, utf_translator, user_translator = nil, nil, nil
--
-- function resolvers.filters.install(name,func)
--         if name == "input" then input_translator = func
--     elseif name == "utf"   then utf_translator   = func
--     elseif name == "user"  then user_translator  = func end
-- end
--
-- function openers.textopener(filename,file_handle,tag)
--     local u = unicode.utftype(file_handle)
--     local t = { }
--     if u > 0  then
--         if trace_locating then
--             report_resolvers("%s opener, file '%s' opened using method '%s'",tag,filename,unicode.utfname[u])
--         end
--         local l
--         local data = file_handle:read("*a")
--         if u > 2 then
--             l = unicode.utf32_to_utf8(data,u==4)
--         elseif u > 1 then
--             l = unicode.utf16_to_utf8(data,u==2)
--         else
--             l = string.splitlines(data)
--         end
--         file_handle:close()
--         t = {
--             utftype = u, -- may go away
--             lines = l,
--             current = 0, -- line number, not really needed
--             handle = nil,
--             noflines = #l,
--             close = function()
--                 if trace_locating then
--                     report_resolvers("%s closer, file '%s' closed",tag,filename)
--                 end
--                 logs.show_close(filename)
--                 t = nil
--             end,
--             reader = function(self)
--                 self = self or t
--                 local current, lines = self.current, self.lines
--                 if current >= #lines then
--                     return nil
--                 else
--                     current = current + 1
--                     self.current = current
--                     local line = lines[current]
--                     if not line then
--                         return nil
--                     elseif line == "" then
--                         return ""
--                     else
--                         if input_translator then
--                             line = input_translator(line)
--                         end
--                         if utf_translator then
--                             line = utf_translator(line)
--                         end
--                         if user_translator then
--                             line = user_translator(line)
--                         end
--                         return line
--                     end
--                 end
--             end
--         }
--     else
--         if trace_locating then
--             report_resolvers("%s opener, file '%s' opened",tag,filename)
--         end
--         -- todo: file;name -> freeze / eerste regel scannen -> freeze
--         --~ local data = lpegmatch(getlines,file_handle:read("*a"))
--         --~ local n = 0
--         t = {
--             reader = function() -- self
--                 local line = file_handle:read()
--                 --~ n = n + 1
--                 --~ local line = data[n]
--                 --~ print(line)
--                 if not line then
--                     return nil
--                 elseif line == "" then
--                     return ""
--                 else
--                     if input_translator then
--                         line = input_translator(line)
--                     end
--                     if utf_translator then
--                         line = utf_translator(line)
--                     end
--                     if user_translator then
--                         line = user_translator(line)
--                     end
--                     return line
--                 end
--             end,
--             close = function()
--                 if trace_locating then
--                     report_resolvers("%s closer, file '%s' closed",tag,filename)
--                 end
--                 logs.show_close(filename)
--                 file_handle:close()
--                 t = nil
--                 collectgarbage("step") -- saves some memory, maybe checkgarbage but no #
--             end,
--             handle = function()
--                 return file_handle
--             end,
--             noflines = function()
--                 t.noflines = io.noflines(file_handle)
--                 return t.noflines
--             end
--         }
--     end
--     return t
-- end


-- the main text reader --

local sequencers = utilities.sequencers

local fileprocessor = nil
local lineprocessor = nil

local textfileactions = sequencers.reset {
    arguments    = "str,filename",
    returnvalues = "str",
    results      = "str",
}

local textlineactions = sequencers.reset {
    arguments    = "str,filename,linenumber",
    returnvalues = "str",
    results      = "str",
}

openers.textfileactions = textfileactions
openers.textlineactions = textlineactions

sequencers.appendgroup(textfileactions,"system")
sequencers.appendgroup(textfileactions,"user")

sequencers.appendgroup(textlineactions,"system")
sequencers.appendgroup(textlineactions,"user")

function openers.textopener(filename,file_handle,tag)
    if trace_locating then
        report_resolvers("%s opener, file '%s' opened using method '%s'",tag,filename,unicode.utfname[u])
    end
    if textfileactions.dirty then
        fileprocessor = sequencers.compile(textfileactions)
    end
    local lines = io.loaddata(filename)
    local kind = unicode.filetype(lines)
    if kind == "utf-16-be" then
        lines = unicode.utf16_to_utf8_be(lines)
    elseif kind == "utf-16-le" then
        lines = unicode.utf16_to_utf8_le(lines)
    elseif kind == "utf-32-be" then
        lines = unicode.utf32_to_utf8_be(lines)
    elseif kind == "utf-32-le" then
        lines = unicode.utf32_to_utf8_le(lines)
    else -- utf8 or unknown
        lines = fileprocessor(lines,filename) or lines
        lines = string.splitlines(lines)
    end
    local t = {
        lines = lines,
        current = 0,
        handle = nil,
        noflines = #lines,
        close = function()
            if trace_locating then
                report_resolvers("%s closer, file '%s' closed",tag,filename)
            end
            logs.show_close(filename)
            t = nil
        end,
        reader = function(self)
            self = self or t
            local current, noflines = self.current, self.noflines
            if current >= noflines then
                return nil
            else
                current = current + 1
                self.current = current
                local line = lines[current]
                if not line then
                    return nil
                elseif line == "" then
                    return ""
                else
                    if textlineactions.dirty then
                        lineprocessor = sequencers.compile(textlineactions)
                    end
                    return lineprocessor(line,filename,current) or line
                end
            end
        end
    }
    return t
end

-- -- --

function openers.generic(tag,filename)
    if filename and filename ~= "" then
        local f = io.open(filename,"r")
        if f then
            logs.show_open(filename) -- todo
            if trace_locating then
                report_resolvers("%s opener, file '%s' opened",tag,filename)
            end
            return openers.textopener(filename,f,tag)
        end
    end
    if trace_locating then
        report_resolvers("%s opener, file '%s' not found",tag,filename)
    end
    return unpack(openers.notfound)
end

function loaders.generic(tag,filename)
    if filename and filename ~= "" then
        local f = io.open(filename,"rb")
        if f then
            logs.show_load(filename)
            if trace_locating then
                report_resolvers("%s loader, file '%s' loaded",tag,filename)
            end
            local s = f:read("*a")
            if checkgarbage then
                checkgarbage(#s)
            end
            f:close()
            if s then
                return true, s, #s
            end
        end
    end
    if trace_locating then
        report_resolvers("%s loader, file '%s' not found",tag,filename)
    end
    return unpack(loaders.notfound)
end

function finders.tex(filename,filetype)
    return finders.generic('tex',filename,filetype)
end

function openers.tex(filename)
    return openers.generic('tex',filename)
end

function loaders.tex(filename)
    return loaders.generic('tex',filename)
end

function resolvers.findtexfile(filename, filetype)
    return resolvers.methodhandler('finders',filename, filetype)
end

function resolvers.opentexfile(filename)
    return resolvers.methodhandler('openers',filename)
end

function resolvers.openfile(filename)
    local fullname = resolvers.findtexfile(filename)
    if fullname and (fullname ~= "") then
        return resolvers.opentexfile(fullname)
    else
        return nil
    end
end

function resolvers.loadtexfile(filename, filetype)
    -- todo: apply filters
    local ok, data, size = resolvers.loadbinfile(filename, filetype)
    return data or ""
end

resolvers.texdatablob = resolvers.loadtexfile