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
|
if not modules then modules = { } end modules ['data-pre'] = {
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"
}
-- It could be interesting to hook the resolver in the file
-- opener so that unresolved prefixes travel around and we
-- get more abstraction.
-- As we use this beforehand we will move this up in the chain
-- of loading.
--~ print(resolvers.resolve("abc env:tmp file:cont-en.tex path:cont-en.tex full:cont-en.tex rel:zapf/one/p-chars.tex"))
local resolvers = resolvers
local prefixes = utilities.storage.allocate()
resolvers.prefixes = prefixes
local gsub = string.gsub
local cleanpath, findgivenfile, expansion = resolvers.cleanpath, resolvers.findgivenfile, resolvers.expansion
local getenv = resolvers.getenv -- we can probably also use resolvers.expansion
local P, Cs, lpegmatch = lpeg.P, lpeg.Cs, lpeg.match
-- getenv = function(...) return resolvers.getenv(...) end -- needs checking (definitions changes later on)
prefixes.environment = function(str)
return cleanpath(expansion(str))
end
prefixes.relative = function(str,n) -- lfs.isfile
if io.exists(str) then
-- nothing
elseif io.exists("./" .. str) then
str = "./" .. str
else
local p = "../"
for i=1,n or 2 do
if io.exists(p .. str) then
str = p .. str
break
else
p = p .. "../"
end
end
end
return cleanpath(str)
end
prefixes.auto = function(str)
local fullname = prefixes.relative(str)
if not lfs.isfile(fullname) then
fullname = prefixes.locate(str)
end
return fullname
end
prefixes.locate = function(str)
local fullname = findgivenfile(str) or ""
return cleanpath((fullname ~= "" and fullname) or str)
end
prefixes.filename = function(str)
local fullname = findgivenfile(str) or ""
return cleanpath(file.basename((fullname ~= "" and fullname) or str)) -- no cleanpath needed here
end
prefixes.pathname = function(str)
local fullname = findgivenfile(str) or ""
return cleanpath(file.dirname((fullname ~= "" and fullname) or str))
end
prefixes.selfautoloc = function(str)
return cleanpath(file.join(getenv('SELFAUTOLOC'),str))
end
prefixes.selfautoparent = function(str)
return cleanpath(file.join(getenv('SELFAUTOPARENT'),str))
end
prefixes.selfautodir = function(str)
return cleanpath(file.join(getenv('SELFAUTODIR'),str))
end
prefixes.home = function(str)
return cleanpath(file.join(getenv('HOME'),str))
end
prefixes.env = prefixes.environment
prefixes.rel = prefixes.relative
prefixes.loc = prefixes.locate
prefixes.kpse = prefixes.locate
prefixes.full = prefixes.locate
prefixes.file = prefixes.filename
prefixes.path = prefixes.pathname
function resolvers.allprefixes(separator)
local all = table.sortedkeys(prefixes)
if separator then
for i=1,#all do
all[i] = all[i] .. ":"
end
end
return all
end
local function _resolve_(method,target)
local action = prefixes[method]
if action then
return action(target)
else
return method .. ":" .. target
end
end
local resolved, abstract = { }, { }
function resolvers.resetresolve(str)
resolved, abstract = { }, { }
end
local function resolve(str) -- use schemes, this one is then for the commandline only
if type(str) == "table" then
local t = { }
for i=1,#str do
t[i] = resolve(str[i])
end
return t
else
local res = resolved[str]
if not res then
res = gsub(str,"([a-z][a-z]+):([^ \"\';,]*)",_resolve_) -- home:xx;selfautoparent:xx; etc (comma added)
resolved[str] = res
abstract[res] = str
end
return res
end
end
local function unresolve(str)
return abstract[str] or str
end
resolvers.resolve = resolve
resolvers.unresolve = unresolve
if os.uname then
for k, v in next, os.uname() do
if not prefixes[k] then
prefixes[k] = function() return v end
end
end
end
if os.type == "unix" then
local pattern
local function makepattern(t,k,v)
local colon = P(":")
local p
for k, v in table.sortedpairs(prefixes) do
if p then
p = P(k) + p
else
p = P(k)
end
end
pattern = Cs((p * colon + colon/";" + P(1))^0)
if t then
t[k] = v
end
end
makepattern()
getmetatable(prefixes).__newindex = makepattern
function resolvers.repath(str)
return lpegmatch(pattern,str)
end
else -- already the default:
function resolvers.repath(str)
return str
end
end
|