file-res.lua /size: 6639 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
file-res
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to supp-fil.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
tonumber
=
tonumber
10
local
format
,
find
=
string
.
format
,
string
.
find
11
local
isfile
=
lfs
.
isfile
12
local
is_qualified_path
=
file
.
is_qualified_path
13
local
hasscheme
,
urlescape
=
url
.
hasscheme
,
url
.
escape
14 15
local
trace_files
=
false
trackers
.
register
(
"
resolvers.readfile
"
,
function
(
v
)
trace_files
=
v
end
)
16
local
trace_details
=
false
trackers
.
register
(
"
resolvers.readfile.details
"
,
function
(
v
)
trace_details
=
v
end
)
17
local
report_files
=
logs
.
reporter
(
"
files
"
,
"
readfile
"
)
18 19
resolvers
.
maxreadlevel
=
2
20 21
directives
.
register
(
"
resolvers.maxreadlevel
"
,
function
(
v
)
22
-- resolvers.maxreadlevel = (v == false and 0) or (v == true and 2) or tonumber(v) or 2
23
resolvers
.
maxreadlevel
=
v
=
=
false
and
0
or
tonumber
(
v
)
or
2
24
end
)
25 26
local
finders
,
loaders
,
openers
=
resolvers
.
finders
,
resolvers
.
loaders
,
resolvers
.
openers
27 28
local
found
=
{
}
-- can best be done in the resolver itself
29 30
local
function
readfilename
(
specification
,
backtrack
,
treetoo
)
31
if
trace_details
then
32
report_files
(
table
.
serialize
(
specification
,
"
specification
"
)
)
33
end
34
local
name
=
specification
.
filename
35
local
fnd
=
name
and
found
[
name
]
36
if
not
fnd
then
37
local
names
38
local
suffix
=
file
.
suffix
(
name
)
39
if
suffix
~
=
"
"
then
40
names
=
{
name
}
41
else
42
local
defaultsuffixes
=
resolvers
.
defaultsuffixes
43
names
=
{
}
44
for
i
=
1
,
#
defaultsuffixes
do
45
names
[
i
]
=
name
.
.
"
.
"
.
.
defaultsuffixes
[
i
]
46
end
47
if
trace_files
then
48
report_files
(
"
locating: %s, using default suffixes: %a
"
,
name
,
defaultsuffixes
)
49
end
50
end
51
for
i
=
1
,
#
names
do
52
local
fname
=
names
[
i
]
53
if
isfile
(
fname
)
then
54
if
trace_files
then
55
report_files
(
"
found local: %s
"
,
name
)
56
end
57
fnd
=
fname
58
break
59
end
60
end
61
if
not
fnd
and
backtrack
then
62
for
i
=
1
,
#
names
do
63
local
fname
=
names
[
i
]
64
for
i
=
1
,
backtrack
,
1
do
65
fname
=
"
../
"
.
.
fname
66
if
isfile
(
fname
)
then
67
if
trace_files
then
68
report_files
(
"
found by backtracking: %s
"
,
fname
)
69
end
70
fnd
=
fname
71
break
72
elseif
trace_files
then
73
report_files
(
"
not found by backtracking: %s
"
,
fname
)
74
end
75
end
76
if
fnd
then
77
break
78
end
79
end
80
end
81
if
not
fnd
then
82
local
paths
=
resolvers
.
getextrapaths
(
)
83
if
paths
then
84
for
i
=
1
,
#
paths
do
85
for
i
=
1
,
#
names
do
86
local
fname
=
paths
[
i
]
.
.
"
/
"
.
.
names
[
i
]
87
if
isfile
(
fname
)
then
88
if
trace_files
then
89
report_files
(
"
found on extra path: %s
"
,
fname
)
90
end
91
fnd
=
fname
92
break
93
end
94
end
95
if
fnd
then
96
break
97
end
98
end
99
end
100
end
101
if
not
fnd
and
treetoo
then
102
fnd
=
resolvers
.
findtexfile
(
name
)
or
"
"
103
if
trace_files
then
104
if
fnd
~
=
"
"
then
105
report_files
(
"
found by tree lookup: %s
"
,
fnd
)
106
else
107
report_files
(
"
not found by tree lookup: %s
"
,
name
)
108
end
109
end
110
end
111
found
[
name
]
=
fnd
112
elseif
trace_files
then
113
if
fnd
~
=
"
"
then
114
report_files
(
"
already found: %s
"
,
fnd
)
115
else
116
report_files
(
"
already not found: %s
"
,
name
)
117
end
118
end
119
return
fnd
or
"
"
120
end
121 122
-- resolvers.readfilename = readfilename -- bonus use getreadfilename instead
123 124
function
resolvers
.
finders
.
original
(
specification
)
-- handy, see memstreams
125
return
specification
.
path
126
end
127 128
function
finders
.
job
(
specification
)
return
readfilename
(
specification
,
false
,
false
)
end
-- current path, no backtracking
129
function
finders
.
loc
(
specification
)
return
readfilename
(
specification
,
resolvers
.
maxreadlevel
,
false
)
end
-- current path, backtracking
130
function
finders
.
sys
(
specification
)
return
readfilename
(
specification
,
false
,
true
)
end
-- current path, obeys tex search
131
function
finders
.
fix
(
specification
)
return
readfilename
(
specification
,
resolvers
.
maxreadlevel
,
false
)
end
-- specified path, backtracking
132
function
finders
.
set
(
specification
)
return
readfilename
(
specification
,
false
,
false
)
end
-- specified path, no backtracking
133
function
finders
.
any
(
specification
)
return
readfilename
(
specification
,
resolvers
.
maxreadlevel
,
true
)
end
-- loc job sys
134 135
openers
.
job
=
openers
.
file
loaders
.
job
=
loaders
.
file
-- default anyway
136
openers
.
loc
=
openers
.
file
loaders
.
loc
=
loaders
.
file
137
openers
.
sys
=
openers
.
file
loaders
.
sys
=
loaders
.
file
138
openers
.
fix
=
openers
.
file
loaders
.
fix
=
loaders
.
file
139
openers
.
set
=
openers
.
file
loaders
.
set
=
loaders
.
file
140
openers
.
any
=
openers
.
file
loaders
.
any
=
loaders
.
file
141 142
local
function
getreadfilename
(
scheme
,
path
,
name
)
-- better do a split and then pass table
143
local
fullname
144
if
hasscheme
(
name
)
or
is_qualified_path
(
name
)
then
145
fullname
=
name
146
else
147
if
not
find
(
name
,
"
%
"
,
1
,
true
)
then
148
name
=
urlescape
(
name
)
-- if no % in names
149
end
150
fullname
=
(
(
path
=
=
"
"
)
and
format
(
"
%s:///%s
"
,
scheme
,
name
)
)
or
format
(
"
%s:///%s/%s
"
,
scheme
,
path
,
name
)
151
end
152
return
resolvers
.
findtexfile
(
fullname
)
or
"
"
-- can be more direct
153
end
154 155
resolvers
.
getreadfilename
=
getreadfilename
156 157
-- a name belonging to the run but also honoring qualified
158 159
local
implement
=
interfaces
.
implement
160 161
implement
{
162
name
=
"
getreadfilename
"
,
163
actions
=
{
getreadfilename
,
context
}
,
164
arguments
=
"
3 strings
"
,
165
}
166 167
implement
{
168
name
=
"
locfilename
"
,
169
actions
=
{
getreadfilename
,
context
}
,
170
arguments
=
{
"
'loc'
"
,
"
'.'
"
,
"
string
"
}
,
171
}
172 173
implement
{
174
name
=
"
doifelselocfile
"
,
175
actions
=
{
getreadfilename
,
isfile
,
commands
.
doifelse
}
,
176
arguments
=
{
"
'loc'
"
,
"
'.'
"
,
"
string
"
}
,
177
}
178