data-lua.lua /size: 4227 b    last modification: 2020-07-01 14:35
1
if
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 11
local
package
,
lpeg
=
package
,
lpeg
12 13
local
loadfile
=
loadfile
14
local
addsuffix
=
file
.
addsuffix
15 16
local
P
,
S
,
Cs
,
lpegmatch
=
lpeg
.
P
,
lpeg
.
S
,
lpeg
.
Cs
,
lpeg
.
match
17 18
local
luasuffixes
=
{
'
tex
'
,
'
lua
'
}
19
local
libsuffixes
=
{
'
lib
'
}
20
local
luaformats
=
{
'
TEXINPUTS
'
,
'
LUAINPUTS
'
}
21
local
libformats
=
{
'
CLUAINPUTS
'
}
22
local
helpers
=
package
.
helpers
or
{
}
23
local
methods
=
helpers
.
methods
or
{
}
24 25
local
resolvers
=
resolvers
26
local
resolveprefix
=
resolvers
.
resolve
27
local
expandedpaths
=
resolvers
.
expandedpathlistfromvariable
28
local
findfile
=
resolvers
.
findfile
29 30
helpers
.
report
=
logs
.
reporter
(
"
resolvers
"
,
"
libraries
"
)
31 32
trackers
.
register
(
"
resolvers.libraries
"
,
function
(
v
)
helpers
.
trace
=
v
end
)
33
trackers
.
register
(
"
resolvers.locating
"
,
function
(
v
)
helpers
.
trace
=
v
end
)
34 35
helpers
.
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 48
local
pattern
=
Cs
(
P
(
"
!
"
)
^
0
/
"
"
*
(
P
(
"
/
"
)
*
P
(
-1
)
/
"
/
"
+
P
(
"
/
"
)
^
1
/
"
/
"
+
1
)
^
0
)
49 50
function
helpers
.
cleanpath
(
path
)
-- hm, don't we have a helper for this?
51
return
resolveprefix
(
lpegmatch
(
pattern
,
path
)
)
52
end
53 54
local
loadedaslib
=
helpers
.
loadedaslib
55
local
registerpath
=
helpers
.
registerpath
56
local
lualibfile
=
helpers
.
lualibfile
57 58
local
luaformatpaths
59
local
libformatpaths
60 61
local
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
69
end
70 71
local
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
79
end
80 81
local
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
101
end
102 103
helpers
.
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 117
methods
[
"
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
"
)
122
end
123 124
methods
[
"
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
"
)
129
end
130 131
-- package.extraclibpath(environment.ownpath)
132 133
resolvers
.
loadlualib
=
require
-- hm
134