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