summaryrefslogtreecommitdiff
path: root/tex/context/base/data-tex.lua
blob: 393c787d6fd51a7f57a572e1c5b47ab1bd829558 (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
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, savers = resolvers.finders, resolvers.openers, resolvers.loaders, resolvers.savers

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

-- 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(tag,filename,file_handle)
    if textfileactions.dirty then
        fileprocessor = sequencers.compile(textfileactions)
    end
    local lines
    if not file_handle then
        lines = io.loaddata(filename)
    elseif type(file_handle) == "string" then
        lines = file_handle
    elseif type(file_handle) == "table" then
        lines = file_handle
    elseif file_handle then
        lines = file_handle:read("*a")
        file_handle:close()
    end
    if type(lines) == "string" then
        local kind = unicode.filetype(lines)
        if trace_locating then
            report_resolvers("%s opener, file '%s' opened using method '%s'",tag,filename,kind)
        end
        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
    elseif trace_locating then
        report_resolvers("%s opener, file '%s' opened",tag,filename)
    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

local data, n, template = { }, 0, "virtual://virtualfile:%s"

-- todo: report statistics

function savers.virtual(content)
    n = n + 1
    local filename = format(template,n)
    if trace_locating then
        report_resolvers("%s finder: virtual file '%s' saved",tag,filename)
    end
    data[filename] = content
    return filename
end

function finders.virtual(filename,filetype,specification)
    local path = specification and specification.path
    local name = path ~= "" and path or filename
    local d = data[name]
    if d then
        if trace_locating then
            report_resolvers("virtual finder: file '%s' found",filename)
        end
        return filename
    else
        if trace_locating then
            report_resolvers("virtual finder: unknown file '%s'",filename)
        end
        return unpack(finders.notfound)
    end
end

function openers.virtual(filename,filetype,specification) -- duplicate ... todo: specification
    local path = specification and specification.path
    local name = path ~= "" and path or filename
    local d = data[name]
    if d then
        if trace_locating then
            report_resolvers("virtual opener, file '%s' opened",filename)
        end
        data[filename] = nil
        return openers.textopener("virtual",filename,d)
    else
        if trace_locating then
            report_resolvers("virtual opener, file '%s' not found",filename)
        end
        return unpack(openers.notfound)
    end
end

function loaders.virtual(filename,filetype,specification)
    local path = specification and specification.path
    local name = path ~= "" and path or filename
    local d = data[name]
    if d then
        if trace_locating then
            report_resolvers("virtual loader, file '%s' loaded",filename)
        end
        data[filename] = nil
        return true, d, #d
    end
    if trace_locating then
        report_resolvers("virtual loader, file '%s' not loaded",filename)
    end
    return unpack(loaders.notfound)
end

-- could be a finder (beware: the generic finders take a tag!)

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

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(tag,filename,f)
        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

-- -- 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(tag,filename,file_handle)
--     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