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

function finders.generic(tag,filename,filetype)
    local foundname = resolvers.find_file(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

--~ local lpegmatch = lpeg.match
--~ local getlines = lpeg.Ct(lpeg.patterns.textline)

local input_translator, utf_translator, user_translator = nil, nil, nil

function resolvers.install_text_filter(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.text_opener(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
        if u > 2 then
            l = unicode.utf32_to_utf8(file_handle:read("*a"),u==4)
        else
            l = unicode.utf16_to_utf8(file_handle:read("*a"),u==2)
        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
            end,
            handle = function()
                return file_handle
            end,
            noflines = function()
                t.noflines = io.noflines(file_handle)
                return t.noflines
            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.text_opener(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 garbagecollector and garbagecollector.check then garbagecollector.check(#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.texdatablob(filename, filetype)
    local ok, data, size = resolvers.loadbinfile(filename, filetype)
    return data or ""
end

resolvers.loadtexfile = resolvers.texdatablob