diff options
Diffstat (limited to 'tex/context/base/data-res.lua')
-rw-r--r-- | tex/context/base/data-res.lua | 184 |
1 files changed, 107 insertions, 77 deletions
diff --git a/tex/context/base/data-res.lua b/tex/context/base/data-res.lua index 83d30c994..d6de90ef0 100644 --- a/tex/context/base/data-res.lua +++ b/tex/context/base/data-res.lua @@ -872,9 +872,22 @@ local function collect_files(names) return noffiles > 0 and filelist or nil end -function resolvers.registerintrees(name) - if not find(name,"^%.") then - instance.foundintrees[name] = (instance.foundintrees[name] or 0) + 1 -- maybe only one +local fit = { } + +function resolvers.registerintrees(filename,format,filetype,usedmethod,foundname) + local foundintrees = instance.foundintrees + if usedmethod == "direct" and filename == foundname and fit[foundname] then + -- just an extra lookup after a test on presence + else + local t = { + filename = filename, + format = format ~= "" and format or nil, + filetype = filetype ~= "" and filetype or nil, + usedmethod = usedmethod, + foundname = foundname, + } + fit[foundname] = t + foundintrees[#foundintrees+1] = t end end @@ -898,12 +911,54 @@ local preparetreepattern = Cs((P(".")/"%%." + P("-")/"%%-" + P(1))^0 * Cc("$")) local collect_instance_files +local function find_analyze(filename,askedformat,allresults) + local filetype, wantedfiles, ext = '', { }, fileextname(filename) + -- too tricky as filename can be bla.1.2.3: + -- + -- if not suffixmap[ext] then + -- wantedfiles[#wantedfiles+1] = filename + -- end + wantedfiles[#wantedfiles+1] = filename + if askedformat == "" then + if ext == "" or not suffixmap[ext] then + local defaultsuffixes = resolvers.defaultsuffixes + for i=1,#defaultsuffixes do + local forcedname = filename .. '.' .. defaultsuffixes[i] + wantedfiles[#wantedfiles+1] = forcedname + filetype = resolvers.formatofsuffix(forcedname) + if trace_locating then + report_resolving("forcing filetype '%s'",filetype) + end + end + else + filetype = resolvers.formatofsuffix(filename) + if trace_locating then + report_resolving("using suffix based filetype '%s'",filetype) + end + end + else + if ext == "" or not suffixmap[ext] then + local format_suffixes = suffixes[askedformat] + if format_suffixes then + for i=1,#format_suffixes do + wantedfiles[#wantedfiles+1] = filename .. "." .. format_suffixes[i] + end + end + end + filetype = askedformat + if trace_locating then + report_resolving("using given filetype '%s'",filetype) + end + end + return filetype, wantedfiles +end + local function find_direct(filename,allresults) if not dangerous[askedformat] and isreadable(filename) then if trace_detail then report_resolving("file '%s' found directly",filename) end - return { filename } + return "direct", { filename } end end @@ -912,7 +967,10 @@ local function find_wildcard(filename,allresults) if trace_locating then report_resolving("checking wildcard '%s'", filename) end - return resolvers.findwildcardfiles(filename) -- we can use the local + local method, result = resolvers.findwildcardfiles(filename) + if result then + return "wildcard", result + end end end @@ -927,7 +985,7 @@ local function find_qualified(filename,allresults) -- this one will be split too if trace_detail then report_resolving("qualified file '%s' found", filename) end - return { filename } + return "qualified", { filename } end if trace_detail then report_resolving("locating qualified file '%s'", filename) @@ -943,12 +1001,12 @@ local function find_qualified(filename,allresults) -- this one will be split too if trace_locating then report_resolving("no suffix, forcing format filetype '%s'", s) end - return { forcedname } + return "qualified", { forcedname } end end end end - if suffix ~= "" then + if suffix and suffix ~= "" then -- try to find in tree (no suffix manipulation), here we search for the -- matching last part of the name local basename = filebasename(filename) @@ -982,7 +1040,7 @@ local function find_qualified(filename,allresults) -- this one will be split too end end if #result > 0 then - return result + return "qualified", result end end end @@ -997,53 +1055,11 @@ local function find_qualified(filename,allresults) -- this one will be split too -- end -- end -- if #result > 0 then - -- return result + -- return "qualified", result -- end end end -local function find_analyze(filename,askedformat,allresults) - local filetype, wantedfiles, ext = '', { }, fileextname(filename) - -- too tricky as filename can be bla.1.2.3: - -- - -- if not suffixmap[ext] then - -- wantedfiles[#wantedfiles+1] = filename - -- end - wantedfiles[#wantedfiles+1] = filename - if askedformat == "" then - if ext == "" or not suffixmap[ext] then - local defaultsuffixes = resolvers.defaultsuffixes - for i=1,#defaultsuffixes do - local forcedname = filename .. '.' .. defaultsuffixes[i] - wantedfiles[#wantedfiles+1] = forcedname - filetype = resolvers.formatofsuffix(forcedname) - if trace_locating then - report_resolving("forcing filetype '%s'",filetype) - end - end - else - filetype = resolvers.formatofsuffix(filename) - if trace_locating then - report_resolving("using suffix based filetype '%s'",filetype) - end - end - else - if ext == "" or not suffixmap[ext] then - local format_suffixes = suffixes[askedformat] - if format_suffixes then - for i=1,#format_suffixes do - wantedfiles[#wantedfiles+1] = filename .. "." .. format_suffixes[i] - end - end - end - filetype = askedformat - if trace_locating then - report_resolving("using given filetype '%s'",filetype) - end - end - return filetype, wantedfiles -end - local function check_subpath(fname) if isreadable(fname) then if trace_detail then @@ -1174,9 +1190,12 @@ local function find_intree(filename,filetype,wantedfiles,allresults) end -- todo recursive scanning if done and not allresults then - return #result > 0 and result + break end end + if #result > 0 then + return "intree", result + end end end @@ -1195,36 +1214,40 @@ local function find_onpath(filename,filetype,wantedfiles,allresults) end end end - return #result > 0 and result + if #result > 0 then + return "onpath", result + end end local function find_otherwise(filename,filetype,wantedfiles,allresults) -- other text files | any | whatever local filelist = collect_files(wantedfiles) local fl = filelist and filelist[1] if fl then - return { resolvers.resolve(fl[3]) } -- filename + return "otherwise", { resolvers.resolve(fl[3]) } -- filename end end +-- we could have a loop over the 6 functions but then we'd have to +-- always analyze + collect_instance_files = function(filename,askedformat,allresults) -- uses nested - local result, stamp, filetype, wantedfiles askedformat = askedformat or "" filename = collapsepath(filename) if allresults then -- no need for caching, only used for tracing local filetype, wantedfiles = find_analyze(filename,askedformat) local results = { - { method = "direct", list = find_direct (filename,stamp,true) }, - { method = "wildcard", list = find_wildcard (filename,true) }, - { method = "qualified", list = find_qualified(filename,true) }, - { method = "in tree", list = find_intree (filename,filetype,wantedfiles,true) }, - { method = "on path", list = find_onpath (filename,filetype,wantedfiles,true) }, - { method = "otherwise", list = find_otherwise(filename,filetype,wantedfiles,true) }, + { find_direct (filename,true) }, + { find_wildcard (filename,true) }, + { find_qualified(filename,true) }, + { find_intree (filename,filetype,wantedfiles,true) }, + { find_onpath (filename,filetype,wantedfiles,true) }, + { find_otherwise(filename,filetype,wantedfiles,true) }, } local result, status, done = { }, { }, { } for k, r in next, results do - local method, list = r.method, r.list - if list then + local method, list = r[1], r[2] + if method and list then for i=1,#list do local c = collapsepath(list[i]) if not done[c] then @@ -1240,6 +1263,7 @@ collect_instance_files = function(filename,askedformat,allresults) -- uses neste end return result, status else + local method, result, stamp, filetype, wantedfiles if instance.remember then stamp = format("%s--%s", filename, askedformat) result = stamp and instance.found[stamp] @@ -1250,21 +1274,27 @@ collect_instance_files = function(filename,askedformat,allresults) -- uses neste return result end end - result = find_direct (filename,stamp) or - find_wildcard (filename) or - find_qualified(filename) + method, result = find_direct(filename) if not result then - local filetype, wantedfiles = find_analyze(filename,askedformat) - result = find_intree (filename,filetype,wantedfiles) or - find_onpath (filename,filetype,wantedfiles) or - find_otherwise(filename,filetype,wantedfiles) - end - if result then - for k=1,#result do - local rk = collapsepath(result[k]) - result[k] = rk - resolvers.registerintrees(rk) -- for tracing used files + method, result = find_wildcard(filename) + if not result then + method, result = find_qualified(filename) + if not result then + filetype, wantedfiles = find_analyze(filename,askedformat) + method, result = find_intree(filename,filetype,wantedfiles) + if not result then + method, result = find_onpath(filename,filetype,wantedfiles) + if not result then + method, result = find_otherwise(filename,filetype,wantedfiles) + end + end + end end + end + if result and #result > 0 then + local foundname = collapsepath(result[1]) + resolvers.registerintrees(filename,askedformat,filetype,method,foundname) + result = { foundname } else result = { } -- maybe false end |