1if 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
9local tonumber = tonumber
10local format , find = string . format , string . find
11local isfile = lfs . isfile
12local is_qualified_path = file . is_qualified_path
13local hasscheme , urlescape = url . hasscheme , url . escape
14
15local trace_files = false trackers . register ( " resolvers.readfile " , function ( v ) trace_files = v end )
16local trace_details = false trackers . register ( " resolvers.readfile.details " , function ( v ) trace_details = v end )
17local report_files = logs . reporter ( " files " , " readfile " )
18
19resolvers . maxreadlevel = 2
20
21directives . register ( " resolvers.maxreadlevel " , function ( v )
22
23 resolvers . maxreadlevel = v = = false and 0 or tonumber ( v ) or 2
24end )
25
26local finders , loaders , openers = resolvers . finders , resolvers . loaders , resolvers . openers
27
28local found = { }
29
30local 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 " "
120end
121
122
123
124function resolvers . finders . original ( specification )
125 return specification . path
126end
127
128function finders . job ( specification ) return readfilename ( specification , false , false ) end
129function finders . loc ( specification ) return readfilename ( specification , resolvers . maxreadlevel , false ) end
130function finders . sys ( specification ) return readfilename ( specification , false , true ) end
131function finders . fix ( specification ) return readfilename ( specification , resolvers . maxreadlevel , false ) end
132function finders . set ( specification ) return readfilename ( specification , false , false ) end
133function finders . any ( specification ) return readfilename ( specification , resolvers . maxreadlevel , true ) end
134
135openers . job = openers . file loaders . job = loaders . file
136openers . loc = openers . file loaders . loc = loaders . file
137openers . sys = openers . file loaders . sys = loaders . file
138openers . fix = openers . file loaders . fix = loaders . file
139openers . set = openers . file loaders . set = loaders . file
140openers . any = openers . file loaders . any = loaders . file
141
142local function getreadfilename ( scheme , path , name )
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 )
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 " "
153end
154
155resolvers . getreadfilename = getreadfilename
156
157
158
159local implement = interfaces . implement
160
161implement {
162 name = " getreadfilename " ,
163 actions = { getreadfilename , context } ,
164 arguments = " 3 strings " ,
165}
166
167implement {
168 name = " locfilename " ,
169 actions = { getreadfilename , context } ,
170 arguments = { " 'loc' " , " '.' " , " string " } ,
171}
172
173implement {
174 name = " doifelselocfile " ,
175 actions = { getreadfilename , isfile , commands . doifelse } ,
176 arguments = { " 'loc' " , " '.' " , " string " } ,
177}
178 |