data-vir.lua /size: 3040 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
data-vir
'
]
=
{
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
local
type
=
type
10
local
formatters
=
string
.
formatters
11 12
local
trace_virtual
=
false
13
local
report_virtual
=
logs
.
reporter
(
"
resolvers
"
,
"
virtual
"
)
14 15
trackers
.
register
(
"
resolvers.locating
"
,
function
(
v
)
trace_virtual
=
v
end
)
16
trackers
.
register
(
"
resolvers.virtual
"
,
function
(
v
)
trace_virtual
=
v
end
)
17 18
local
resolvers
=
resolvers
19
local
savers
=
resolvers
.
savers
20 21
local
data
=
{
}
22
local
n
=
0
-- hm, number can be query
23
local
f_virtual_n
=
formatters
[
"
virtual://%s.%s
"
]
24
local
f_virtual_y
=
formatters
[
"
virtual://%s-%s.%s
"
]
25 26
function
savers
.
virtual
(
specification
,
content
,
suffix
)
27
n
=
n
+
1
-- one number for all namespaces
28
local
path
=
type
(
specification
)
=
=
"
table
"
and
specification
.
path
or
specification
29
if
type
(
path
)
~
=
"
string
"
or
path
=
=
"
"
then
30
path
=
"
virtualfile
"
31
end
32
local
filename
=
suffix
and
f_virtual_y
(
path
,
n
,
suffix
)
or
f_virtual_n
(
path
,
n
)
33
if
trace_virtual
then
34
report_virtual
(
"
saver: file %a saved
"
,
filename
)
35
end
36
data
[
filename
]
=
content
37
return
filename
38
end
39 40
local
finders
=
resolvers
.
finders
41
local
notfound
=
finders
.
notfound
42 43
function
finders
.
virtual
(
specification
)
44
local
original
=
specification
.
original
45
local
d
=
data
[
original
]
46
if
d
then
47
if
trace_virtual
then
48
report_virtual
(
"
finder: file %a found
"
,
original
)
49
end
50
return
original
51
else
52
if
trace_virtual
then
53
report_virtual
(
"
finder: unknown file %a
"
,
original
)
54
end
55
return
notfound
(
)
56
end
57
end
58 59
local
openers
=
resolvers
.
openers
60
local
notfound
=
openers
.
notfound
61
local
textopener
=
openers
.
helpers
.
textopener
62 63
function
openers
.
virtual
(
specification
)
64
local
original
=
specification
.
original
65
local
d
=
data
[
original
]
66
if
d
then
67
if
trace_virtual
then
68
report_virtual
(
"
opener: file %a opened
"
,
original
)
69
end
70
data
[
original
]
=
nil
-- when we comment this we can have error messages
71
-- With utf-8 we signal that no regime is to be applied!
72
-- characters.showstring(d)
73
return
textopener
(
"
virtual
"
,
original
,
d
,
"
utf-8
"
)
74
else
75
if
trace_virtual
then
76
report_virtual
(
"
opener: file %a not found
"
,
original
)
77
end
78
return
notfound
(
)
79
end
80
end
81 82
local
loaders
=
resolvers
.
loaders
83
local
notfound
=
loaders
.
notfound
84 85
function
loaders
.
virtual
(
specification
)
86
local
original
=
specification
.
original
87
local
d
=
data
[
original
]
88
if
d
then
89
if
trace_virtual
then
90
report_virtual
(
"
loader: file %a loaded
"
,
original
)
91
end
92
data
[
original
]
=
nil
93
return
true
,
d
,
#
d
94
end
95
if
trace_virtual
then
96
report_virtual
(
"
loader: file %a not loaded
"
,
original
)
97
end
98
return
notfound
(
)
99
end
100