summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/data-tre.lua
blob: da3d1f14f19ed86bd71c727325e074174e569c84 (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
if not modules then modules = { } end modules ['data-tre'] = {
    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"
}

-- A tree search is rather dumb ... there is some basic caching of searched trees
-- but nothing is cached over runs ... it's also a wildcard one so we cannot use
-- the normal scanner.

-- tree://e:/temporary/mb-mp/**/drawing.jpg
-- tree://e:/temporary/mb-mp/**/Drawing.jpg
-- tree://t:./**/tufte.tex
-- tree://t:/./**/tufte.tex
-- tree://t:/**/tufte.tex
-- dirlist://e:/temporary/mb-mp/**/drawing.jpg
-- dirlist://e:/temporary/mb-mp/**/Drawing.jpg
-- dirlist://e:/temporary/mb-mp/**/just/some/place/drawing.jpg
-- dirlist://e:/temporary/mb-mp/**/images/drawing.jpg
-- dirlist://e:/temporary/mb-mp/**/images/drawing.jpg?option=fileonly
-- dirlist://///storage-2/resources/mb-mp/**/drawing.jpg
-- dirlist://e:/**/drawing.jpg

local find, gsub, lower = string.find, string.gsub, string.lower
local basename, dirname, joinname = file.basename, file.dirname, file   .join
local globdir, isdir, isfile = dir.glob, lfs.isdir, lfs.isfile
local P, lpegmatch = lpeg.P, lpeg.match

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

local report_trees   = logs.reporter("resolvers","trees")

local resolvers  = resolvers
local finders    = resolvers.finders
local openers    = resolvers.openers
local loaders    = resolvers.loaders
local locators   = resolvers.locators
local hashers    = resolvers.hashers
local generators = resolvers.generators

do

    local collectors = { }
    local found      = { }
    local notfound   = finders.notfound

    function finders.tree(specification) -- to be adapted to new formats
        local spec = specification.filename
        local okay = found[spec]
        if okay == nil then
            if spec ~= "" then
                local path = dirname(spec)
                local name = basename(spec)
                if path == "" then
                    path = "."
                end
                local names = collectors[path]
                if not names then
                    local pattern = find(path,"/%*+$") and path or (path .. "/*")
                    names = globdir(pattern)
                    collectors[path] = names
                end
                local pattern = "/" .. gsub(name,"([%.%-%+])", "%%%1") .. "$"
                for i=1,#names do
                    local fullname = names[i]
                    if find(fullname,pattern) then
                        found[spec] = fullname
                        return fullname
                    end
                end
                -- let's be nice:
                local pattern = lower(pattern)
                for i=1,#names do
                    local fullname = lower(names[i])
                    if find(fullname,pattern) then
                        if isfile(fullname) then
                            found[spec] = fullname
                            return fullname
                        else
                            -- no os name mapping
                            break
                        end
                    end
                end
            end
            okay = notfound() -- false
            found[spec] = okay
        end
        return okay
    end

end

do

    local resolveprefix = resolvers.resolve
    local appendhash    = resolvers.appendhash

    local function dolocate(specification)
        local name     = specification.filename
        local realname = resolveprefix(name) -- no shortcut
        if realname and realname ~= '' and isdir(realname) then
            if trace_locating then
                report_trees("locator %a found",realname)
            end
            appendhash('tree',name,false) -- don't cache
        elseif trace_locating then
            report_trees("locator %a not found",name)
        end
    end

    locators.tree    = dolocate
    locators.dirlist = dolocate
    locators.dirfile = dolocate

end


do

    local filegenerator = generators.file

    generators.dirlist = filegenerator
    generators.dirfile = filegenerator

end

do

    local filegenerator = generators.file
    local methodhandler = resolvers.methodhandler

    local function dohash(specification)
        local name = specification.filename
        if trace_locating then
            report_trees("analyzing %a",name)
        end
        methodhandler("hashers",name)
        filegenerator(specification)
    end

    hashers.tree    = dohash
    hashers.dirlist = dohash
    hashers.dirfile = dohash

end

-- This is a variation on tree lookups but this time we do cache in the given
-- root. We use a similar hasher as the resolvers because we have to deal with
-- for instance trees with 50K xml files plus a similar amount of resources to
-- deal and we don't want too much overhead.

local resolve  do

    local collectors  = { }
    local splitter    = lpeg.splitat("/**/")
    local stripper    = lpeg.replacer { [P("/") * P("*")^1 * P(-1)] = "" }

    local loadcontent = caches.loadcontent
    local savecontent = caches.savecontent

    local notfound    = finders.notfound

    local scanfiles   = resolvers.scanfiles
    local lookup      = resolvers.get_from_content

    table.setmetatableindex(collectors, function(t,k)
        local rootname = lpegmatch(stripper,k)
        local dataname = joinname(rootname,"dirlist")
        local content  = loadcontent(dataname,"files",dataname)
        if not content then
            -- path branch usecache onlyonce tolerant
            content = scanfiles(rootname,nil,nil,false,true) -- so we accept crap
            savecontent(dataname,"files",content,dataname)
        end
        t[k] = content
        return content
    end)

    local function checked(root,p,n)
        if p then
            if type(p) == "table" then
                for i=1,#p do
                    local fullname = joinname(root,p[i],n)
                    if isfile(fullname) then -- safeguard
                        return fullname
                    end
                end
            else
                local fullname = joinname(root,p,n)
                if isfile(fullname) then -- safeguard
                    return fullname
                end
            end
        end
        return notfound()
    end

    -- no funny characters in path but in filename permitted .. sigh

    resolve = function(specification) -- can be called directly too
        local filename = specification.filename
     -- inspect(specification)
        if filename ~= "" then
            local root, rest = lpegmatch(splitter,filename)
            if root and rest then
                local path, name = dirname(rest), basename(rest)
                if name ~= rest then
                    local content = collectors[root]
                    local p, n = lookup(content,name)
                    if not p then
                        return notfound()
                    end
                    local pattern = ".*/" .. path .. "$"
                    local istable = type(p) == "table"
                    if istable then
                        for i=1,#p do
                            local pi = p[i]
                            if pi == path or find(pi,pattern) then
                                local fullname = joinname(root,pi,n)
                                if isfile(fullname) then -- safeguard
                                    return fullname
                                end
                            end
                        end
                    elseif p == path or find(p,pattern) then
                        local fullname = joinname(root,p,n)
                        if isfile(fullname) then -- safeguard
                            return fullname
                        end
                    end
                    local queries = specification.queries
                    if queries and queries.option == "fileonly" then
                        return checked(root,p,n)
                    else
                        return notfound()
                    end
                end
            end
            local path    = dirname(filename)
            local name    = basename(filename)
            local root    = lpegmatch(stripper,path)
            local content = collectors[path]
            local p, n = lookup(content,name)
            if p then
                return checked(root,p,n)
            end
        end
        return notfound()
    end

    finders.dirlist = resolve

    function finders.dirfile(specification)
        local queries = specification.queries
        if queries then
            queries.option = "fileonly"
        else
            specification.queries = { option = "fileonly" }
        end
        return resolve(specification)
    end

end

do

    local fileopener = openers.file
    local fileloader = loaders.file

    openers.dirlist = fileopener
    loaders.dirlist = fileloader

    openers.dirfile = fileopener
    loaders.dirfile = fileloader

end

-- print(resolvers.findtexfile("tree://e:/temporary/mb-mp/**/VB_wmf_03_vw_01d_ant.jpg"))
-- print(resolvers.findtexfile("tree://t:/**/tufte.tex"))
-- print(resolvers.findtexfile("dirlist://e:/temporary/mb-mp/**/VB_wmf_03_vw_01d_ant.jpg"))