data-lua.lua /size: 4227 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['data-lua'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.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-- This is now a plug in into l-lua (as we also use the extra paths elsewhere).
10
11local package, lpeg = package, lpeg
12
13local loadfile = loadfile
14local addsuffix = file.addsuffix
15
16local P, S, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.Cs, lpeg.match
17
18local luasuffixes   = { 'tex', 'lua' }
19local libsuffixes   = { 'lib' }
20local luaformats    = { 'TEXINPUTS', 'LUAINPUTS' }
21local libformats    = { 'CLUAINPUTS' }
22local helpers       = package.helpers or { }
23local methods       = helpers.methods or { }
24
25local resolvers     = resolvers
26local resolveprefix = resolvers.resolve
27local expandedpaths = resolvers.expandedpathlistfromvariable
28local findfile      = resolvers.findfile
29
30helpers.report      = logs.reporter("resolvers","libraries")
31
32trackers.register("resolvers.libraries", function(v) helpers.trace = v end)
33trackers.register("resolvers.locating",  function(v) helpers.trace = v end)
34
35helpers.sequence = {
36    "already loaded",
37    "preload table",
38    "lua variable format",
39    "lib variable format",
40    "lua extra list",
41    "lib extra list",
42    "path specification",
43    "cpath specification",
44    "all in one fallback",
45    "not loaded",
46}
47
48local pattern = Cs(P("!")^0 / "" * (P("/") * P(-1) / "/" + P("/")^1 / "/" + 1)^0)
49
50function helpers.cleanpath(path) -- hm, don't we have a helper for this?
51    return resolveprefix(lpegmatch(pattern,path))
52end
53
54local loadedaslib   = helpers.loadedaslib
55local registerpath  = helpers.registerpath
56local lualibfile    = helpers.lualibfile
57
58local luaformatpaths
59local libformatpaths
60
61local function getluaformatpaths()
62    if not luaformatpaths then
63        luaformatpaths = { }
64        for i=1,#luaformats do
65            registerpath("lua format","lua",luaformatpaths,expandedpaths(luaformats[i]))
66        end
67    end
68    return luaformatpaths
69end
70
71local function getlibformatpaths()
72    if not libformatpaths then
73        libformatpaths = { }
74        for i=1,#libformats do
75            registerpath("lib format","lib",libformatpaths,expandedpaths(libformats[i]))
76        end
77    end
78    return libformatpaths
79end
80
81local function loadedbyformat(name,rawname,suffixes,islib,what)
82    local trace  = helpers.trace
83    local report = helpers.report
84    for i=1,#suffixes do -- so we use findfile and not a lookup loop
85        local format   = suffixes[i]
86        local resolved = findfile(name,format) or ""
87        if trace then
88            report("%s format, identifying %a using format %a",what,name,format)
89        end
90        if resolved ~= "" then
91            if trace then
92                report("%s format, %a found on %a",what,name,resolved)
93            end
94            if islib then
95                return loadedaslib(resolved,rawname)
96            else
97                return loadfile(resolved)
98            end
99        end
100    end
101end
102
103helpers.loadedbyformat = loadedbyformat
104
105-- print(lualibfile("bar"))
106-- print(lualibfile("foo.bar"))
107-- print(lualibfile("crap/foo...bar"))
108-- print(lualibfile("crap//foo.bar"))
109-- print(lualibfile("crap/../foo.bar"))
110-- print(lualibfile("crap/.././foo.bar"))
111
112-- alternatively we could split in path and base and temporary set the libpath to path
113
114-- we could build a list of relevant paths but for tracing it's better to have the
115-- whole lot (ok, we could skip the duplicates)
116
117methods["lua variable format"] = function(name)
118    if helpers.trace then
119        helpers.report("%s format, checking %s paths","lua",#getluaformatpaths()) -- call triggers building
120    end
121    return loadedbyformat(addsuffix(lualibfile(name),"lua"),name,luasuffixes,false,"lua")
122end
123
124methods["lib variable format"] = function(name)
125    if helpers.trace then
126        helpers.report("%s format, checking %s paths","lib",#getlibformatpaths()) -- call triggers building
127    end
128    return loadedbyformat(addsuffix(lualibfile(name),os.libsuffix),name,libsuffixes,true,"lib")
129end
130
131-- package.extraclibpath(environment.ownpath)
132
133resolvers.loadlualib = require -- hm
134