data-pre.lua /size: 5088 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
data-pre
'
]
=
{
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
-- filename : only the basename, including suffix (file:)
10
-- pathname : the pathpart (path:)
11
-- locate : lookup in database (full: kpse: loc:)
12
-- home : home path
13
-- jobpath : job path
14
-- relative : relative path ./ ../ ../.. (rel:)
15
-- auto : relatove or lookup
16
-- toppath : topmost path in input stack
17
-- selfautodir : rather tex specific
18
-- selfautoloc : rather tex specific
19
-- selfautoparent : rather tex specific
20
-- environment : expansion of variable (env:)
21
--
22
-- nodename : computer name
23
-- machine : private, when set
24
-- sysname : operating system name
25
-- version : operating system version
26
-- release : operating system release
27 28
local
insert
,
remove
=
table
.
insert
,
table
.
remove
29 30
local
resolvers
=
resolvers
31
local
prefixes
=
resolvers
.
prefixes
32 33
local
cleanpath
=
resolvers
.
cleanpath
34
local
findgivenfile
=
resolvers
.
findgivenfile
35
local
expansion
=
resolvers
.
expansion
36
local
getenv
=
resolvers
.
getenv
-- we can probably also use resolvers.expansion
37 38
local
basename
=
file
.
basename
39
local
dirname
=
file
.
dirname
40
local
joinpath
=
file
.
join
41 42
local
isfile
=
lfs
.
isfile
43 44
prefixes
.
environment
=
function
(
str
)
45
return
cleanpath
(
expansion
(
str
)
)
46
end
47 48
local
function
relative
(
str
,
n
)
49
if
not
isfile
(
str
)
then
50
local
pstr
=
"
./
"
.
.
str
51
if
isfile
(
pstr
)
then
52
str
=
pstr
53
else
54
local
p
=
"
../
"
55
for
i
=
1
,
n
or
2
do
56
local
pstr
=
p
.
.
str
57
if
isfile
(
pstr
)
then
58
str
=
pstr
59
break
60
else
61
p
=
p
.
.
"
../
"
62
end
63
end
64
end
65
end
66
return
cleanpath
(
str
)
67
end
68 69
local
function
locate
(
str
)
70
local
fullname
=
findgivenfile
(
str
)
or
"
"
71
return
cleanpath
(
fullname
~
=
"
"
and
fullname
or
str
)
72
end
73 74
prefixes
.
relative
=
relative
75
prefixes
.
locate
=
locate
76 77
prefixes
.
auto
=
function
(
str
)
78
local
fullname
=
relative
(
str
)
79
if
not
isfile
(
fullname
)
then
80
fullname
=
locate
(
str
)
81
end
82
return
fullname
83
end
84 85
prefixes
.
filename
=
function
(
str
)
86
local
fullname
=
findgivenfile
(
str
)
or
"
"
87
return
cleanpath
(
basename
(
(
fullname
~
=
"
"
and
fullname
)
or
str
)
)
-- no cleanpath needed here
88
end
89 90
prefixes
.
pathname
=
function
(
str
)
91
local
fullname
=
findgivenfile
(
str
)
or
"
"
92
return
cleanpath
(
dirname
(
(
fullname
~
=
"
"
and
fullname
)
or
str
)
)
93
end
94 95
prefixes
.
selfautoloc
=
function
(
str
)
96
local
pth
=
getenv
(
'
SELFAUTOLOC
'
)
97
return
cleanpath
(
str
and
joinpath
(
pth
,
str
)
or
pth
)
98
end
99 100
prefixes
.
selfautoparent
=
function
(
str
)
101
local
pth
=
getenv
(
'
SELFAUTOPARENT
'
)
102
return
cleanpath
(
str
and
joinpath
(
pth
,
str
)
or
pth
)
103
end
104 105
prefixes
.
selfautodir
=
function
(
str
)
106
local
pth
=
getenv
(
'
SELFAUTODIR
'
)
107
return
cleanpath
(
str
and
joinpath
(
pth
,
str
)
or
pth
)
108
end
109 110
prefixes
.
home
=
function
(
str
)
111
local
pth
=
getenv
(
'
HOME
'
)
112
return
cleanpath
(
str
and
joinpath
(
pth
,
str
)
or
pth
)
113
end
114 115
prefixes
.
env
=
prefixes
.
environment
116
prefixes
.
rel
=
prefixes
.
relative
117
prefixes
.
loc
=
prefixes
.
locate
118
prefixes
.
kpse
=
prefixes
.
locate
119
prefixes
.
full
=
prefixes
.
locate
120
prefixes
.
file
=
prefixes
.
filename
121
prefixes
.
path
=
prefixes
.
pathname
122 123
-- This one assumes that inputstack is set (used in the tex loader). It is a momentary resolve
124
-- as the top of the input stack changes.
125 126
local
inputstack
=
{
}
127
local
stackpath
=
resolvers
.
stackpath
128 129
local
function
toppath
(
)
130
if
not
inputstack
then
-- more convenient to keep it here
131
return
"
.
"
132
end
133
local
pathname
=
dirname
(
inputstack
[
#
inputstack
]
or
"
"
)
134
if
pathname
=
=
"
"
then
135
return
"
.
"
136
else
137
return
pathname
138
end
139
end
140 141
-- The next variant is similar but bound to explicitly registered paths. Practice should
142
-- show if that gives the same results as the previous one. It is meant for a project
143
-- stucture.
144 145
local
function
jobpath
(
)
146
local
path
=
stackpath
(
)
147
if
not
path
or
path
=
=
"
"
then
148
return
"
.
"
149
else
150
return
path
151
end
152
end
153 154
local
function
pushinputname
(
name
)
155
insert
(
inputstack
,
name
)
156
end
157 158
local
function
popinputname
(
name
)
159
return
remove
(
inputstack
)
160
end
161 162
resolvers
.
toppath
=
toppath
163
resolvers
.
jobpath
=
jobpath
164
resolvers
.
pushinputname
=
pushinputname
165
resolvers
.
popinputname
=
popinputname
166 167
-- This hook sit into the resolver:
168 169
prefixes
.
toppath
=
function
(
str
)
return
cleanpath
(
joinpath
(
toppath
(
)
,
str
)
)
end
-- str can be nil or empty
170
prefixes
.
jobpath
=
function
(
str
)
return
cleanpath
(
joinpath
(
jobpath
(
)
,
str
)
)
end
-- str can be nil or empty
171 172
resolvers
.
setdynamic
(
"
toppath
"
)
173
resolvers
.
setdynamic
(
"
jobpath
"
)
174 175
-- for a while (obsolete):
176
--
177
-- prefixes.jobfile = prefixes.jobpath
178
-- resolvers.setdynamic("jobfile")
179