file-mod.lua /size: 8244 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
file-mod
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to file-mod.mkvi
"
,
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 module will be redone! For instance, the prefixes will move to data-*
10
-- as they arr sort of generic along with home:// etc/.
11 12
-- context is not defined yet! todo! (we need to load tupp-fil after cld)
13
-- todo: move startreadingfile to lua and push regime there
14 15
--[[ldx-- 16<p>It's more convenient to manipulate filenames (paths) in 17<l n='lua'/> than in <l n='tex'/>. These methods have counterparts 18at the <l n='tex'/> side.</p> 19--ldx]]
--
20 21
local
format
,
find
,
concat
,
tonumber
=
string
.
format
,
string
.
find
,
table
.
concat
,
tonumber
22
local
sortedhash
=
table
.
sortedhash
23
local
basename
=
file
.
basename
24 25
local
trace_modules
=
false
trackers
.
register
(
"
modules.loading
"
,
function
(
v
)
trace_modules
=
v
end
)
26
local
permit_unprefixed
=
false
directives
.
register
(
"
modules.permitunprefixed
"
,
function
(
v
)
permit_unprefixed
=
v
end
)
27 28
local
report
=
logs
.
reporter
(
"
modules
"
)
29 30
local
commands
=
commands
31
local
context
=
context
32
local
implement
=
interfaces
.
implement
33 34
local
findbyscheme
=
resolvers
.
finders
.
byscheme
-- use different one
35
local
iterator
=
utilities
.
parsers
.
iterator
36 37
-- modules can have a specific suffix or can specify one
38 39
local
prefixes
=
{
40
"
m
"
,
-- module, extends functionality
41
"
p
"
,
-- private code
42
"
s
"
,
-- styles
43
"
x
"
,
-- xml specific modules
44
-- "v", -- an old internal one for examples
45
"
t
"
,
-- third party extensions
46
}
47 48
-- the order might change and how about cld
49 50
local
suffixes
=
CONTEXTLMTXMODE
>
0
and
51
{
52
"
mklx
"
,
-- preprocessed mkiv lmtx files
53
"
mkxl
"
,
-- mkiv lmtx files
54
"
mkvi
"
,
-- preprocessed mkiv files
55
"
mkiv
"
,
-- mkiv files
56
"
tex
"
,
-- normally source code files
57
"
cld
"
,
-- context lua documents (often stand alone)
58
"
lua
"
,
-- lua files
59
}
60
or
61
{
62
"
mkvi
"
,
63
"
mkiv
"
,
64
"
tex
"
,
65
"
cld
"
,
66
"
lua
"
,
67
}
68 69
local
modstatus
=
{
}
70
local
missing
=
false
71 72
local
function
usemodule
(
name
,
hasscheme
)
73
local
foundname
74
if
hasscheme
then
75
-- no auto suffix as http will return a home page or error page
76
-- so we only add one if missing
77
local
fullname
=
file
.
addsuffix
(
name
,
"
tex
"
)
78
if
trace_modules
then
79
report
(
"
checking url %a
"
,
fullname
)
80
end
81
foundname
=
resolvers
.
findtexfile
(
fullname
)
or
"
"
82
elseif
file
.
suffix
(
name
)
~
=
"
"
then
83
if
trace_modules
then
84
report
(
"
checking file %a
"
,
name
)
85
end
86
foundname
=
findbyscheme
(
"
any
"
,
name
)
or
"
"
87
else
88
for
i
=
1
,
#
suffixes
do
89
local
fullname
=
file
.
addsuffix
(
name
,
suffixes
[
i
]
)
90
if
trace_modules
then
91
report
(
"
checking file %a
"
,
fullname
)
92
end
93
foundname
=
findbyscheme
(
"
any
"
,
fullname
)
or
"
"
94
if
foundname
~
=
"
"
then
95
break
96
end
97
end
98
end
99
if
foundname
~
=
"
"
then
100
if
trace_modules
then
101
report
(
"
loading file %a
"
,
foundname
)
102
end
103
context
.
startreadingfile
(
)
104
resolvers
.
jobs
.
usefile
(
foundname
,
true
)
-- once, notext
105
-- context.input(foundname)
106
context
.
stopreadingfile
(
)
107
return
true
108
else
109
return
false
110
end
111
end
112 113
function
environment
.
usemodules
(
prefix
,
askedname
,
truename
)
114
local
truename
=
truename
or
environment
.
truefilename
(
askedname
)
115
local
hasprefix
=
prefix
and
prefix
~
=
"
"
116
local
hashname
=
(
(
hasprefix
and
prefix
)
or
"
*
"
)
.
.
"
-
"
.
.
truename
117
local
status
=
modstatus
[
hashname
]
or
false
-- yet unset
118
if
status
=
=
0
then
119
-- not found
120
elseif
status
=
=
1
then
121
status
=
status
+
1
122
else
123
if
trace_modules
then
124
report
(
"
locating, prefix %a, askedname %a, truename %a
"
,
prefix
,
askedname
,
truename
)
125
end
126
local
hasscheme
=
url
.
hasscheme
(
truename
)
127
if
hasscheme
then
128
-- no prefix and suffix done
129
if
usemodule
(
truename
,
true
)
then
130
status
=
1
131
else
132
status
=
0
133
end
134
elseif
hasprefix
then
135
if
usemodule
(
prefix
.
.
"
-
"
.
.
truename
)
then
136
status
=
1
137
else
138
status
=
0
139
end
140
else
141
for
i
=
1
,
#
prefixes
do
142
-- todo: reconstruct name i.e. basename
143
local
thename
=
prefixes
[
i
]
.
.
"
-
"
.
.
truename
144
if
usemodule
(
thename
)
then
145
status
=
1
146
break
147
end
148
end
149
if
status
then
150
-- ok, don't change
151
elseif
find
(
truename
,
"
-
"
,
1
,
true
)
and
usemodule
(
truename
)
then
152
-- assume a user namespace
153
report
(
"
using user prefixed file %a
"
,
truename
)
154
status
=
1
155
elseif
permit_unprefixed
and
usemodule
(
truename
)
then
156
report
(
"
using unprefixed file %a
"
,
truename
)
157
status
=
1
158
else
159
status
=
0
160
end
161
end
162
end
163
if
status
=
=
0
then
164
missing
=
true
165
report
(
"
%a is not found
"
,
askedname
)
166
elseif
status
=
=
1
then
167
report
(
"
%a is loaded
"
,
trace_modules
and
truename
or
askedname
)
168
else
169
report
(
"
%a is already loaded
"
,
trace_modules
and
truename
or
askedname
)
170
end
171
modstatus
[
hashname
]
=
status
172
end
173 174
statistics
.
register
(
"
loaded tex modules
"
,
function
(
)
175
if
next
(
modstatus
)
then
176
local
t
,
f
,
nt
,
nf
=
{
}
,
{
}
,
0
,
0
177
for
k
,
v
in
sortedhash
(
modstatus
)
do
178
local
b
=
basename
(
k
)
179
if
v
=
=
0
then
180
nf
=
nf
+
1
181
f
[
nf
]
=
b
182
else
183
nt
=
nt
+
1
184
t
[
nt
]
=
b
185
end
186
end
187
if
nf
=
=
0
then
188
return
format
(
"
%s requested, all found (%s)
"
,
nt
,
concat
(
t
,
"
"
)
)
189
elseif
nt
=
=
0
then
190
return
format
(
"
%s requested, all missing (%s)
"
,
nf
,
concat
(
f
,
"
"
)
)
191
else
192
return
format
(
"
%s requested, %s found (%s), %s missing (%s)
"
,
nt
+
nf
,
nt
,
concat
(
t
,
"
"
)
,
nf
,
concat
(
f
,
"
"
)
)
193
end
194
else
195
return
nil
196
end
197
end
)
198 199
logs
.
registerfinalactions
(
function
(
)
200
logs
.
startfilelogging
(
report
,
"
used modules
"
)
201
for
k
,
v
in
sortedhash
(
modstatus
)
do
202
report
(
v
=
=
0
and
"
missing: %s
"
or
"
loaded : %s
"
,
basename
(
k
)
)
203
end
204
logs
.
stopfilelogging
(
)
205
if
missing
and
logs
.
loggingerrors
(
)
then
206
logs
.
starterrorlogging
(
report
,
"
missing modules
"
)
207
for
k
,
v
in
sortedhash
(
modstatus
)
do
208
if
v
=
=
0
then
209
report
(
"
%w%s
"
,
6
,
basename
(
k
)
)
210
end
211
end
212
logs
.
stoperrorlogging
(
)
213
end
214
end
)
215 216
-- moved from syst-lua.lua:
217 218
local
lpegmatch
=
lpeg
.
match
219
local
splitter
=
lpeg
.
tsplitter
(
lpeg
.
S
(
"
.
"
)
,
tonumber
)
220 221
function
environment
.
comparedversion
(
one
,
two
)
-- one >= two
222
if
not
two
or
two
=
=
"
"
then
223
one
,
two
=
environment
.
version
,
one
224
elseif
one
=
=
"
"
then
225
one
=
environment
.
version
226
end
227
one
=
lpegmatch
(
splitter
,
one
)
228
two
=
lpegmatch
(
splitter
,
two
)
229
one
=
(
one
[
1
]
or
0
)
*
10000
+
(
one
[
2
]
or
0
)
*
100
+
(
one
[
3
]
or
0
)
230
two
=
(
two
[
1
]
or
0
)
*
10000
+
(
two
[
2
]
or
0
)
*
100
+
(
two
[
3
]
or
0
)
231
if
one
<
two
then
232
return
-1
233
elseif
one
>
two
then
234
return
1
235
else
236
return
0
237
end
238
end
239 240
environment
.
comparedversion
=
comparedversion
241 242 243
function
environment
.
useluamodule
(
list
)
244
for
filename
in
iterator
(
list
)
do
245
environment
.
loadluafile
(
filename
)
246
end
247
end
248 249
implement
{
250
name
=
"
usemodules
"
,
251
actions
=
environment
.
usemodules
,
252
arguments
=
"
2 strings
"
,
253
}
254 255
implement
{
256
name
=
"
doifelseolderversion
"
,
257
actions
=
function
(
one
,
two
)
commands
.
doifelse
(
comparedversion
(
one
,
two
)
>
=
0
)
end
,
258
arguments
=
"
2 strings
"
259
}
260 261
implement
{
262
name
=
"
useluamodule
"
,
263
actions
=
environment
.
useluamodule
,
264
arguments
=
"
string
"
265
}
266 267
implement
{
268
name
=
"
loadluamodule
"
,
269
actions
=
function
(
name
)
dofile
(
resolvers
.
findctxfile
(
name
)
)
end
,
-- hack
270
arguments
=
"
string
"
271
}
272 273