diff options
author | Hans Hagen <pragma@wxs.nl> | 2023-02-14 18:21:35 +0100 |
---|---|---|
committer | Context Git Mirror Bot <phg@phi-gamma.net> | 2023-02-14 18:21:35 +0100 |
commit | cd404631d7079a9af603d173eb015f026731aa01 (patch) | |
tree | 588c6c8e860bb26955999dfc8be67eb78cc7ff7f /tex/context/base/mkxl/data-vir.lmt | |
parent | d643b65f7ab840157f538914b07f8d3cddf94bec (diff) | |
download | context-cd404631d7079a9af603d173eb015f026731aa01.tar.gz |
2023-02-14 17:44:00
Diffstat (limited to 'tex/context/base/mkxl/data-vir.lmt')
-rw-r--r-- | tex/context/base/mkxl/data-vir.lmt | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/tex/context/base/mkxl/data-vir.lmt b/tex/context/base/mkxl/data-vir.lmt new file mode 100644 index 000000000..b78211fc9 --- /dev/null +++ b/tex/context/base/mkxl/data-vir.lmt @@ -0,0 +1,104 @@ +if not modules then modules = { } end modules ['data-vir'] = { + version = 1.001, + comment = "companion to luat-lib.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +local type = type +local formatters = string.formatters + +local trace_virtual = false +local report_virtual = logs.reporter("resolvers","virtual") + +trackers.register("resolvers.locating", function(v) trace_virtual = v end) +trackers.register("resolvers.virtual", function(v) trace_virtual = v end) + +local resolvers = resolvers +local savers = resolvers.savers +local cleaners = resolvers.cleaners + +local data = { } +local n = 0 -- hm, number can be query +local f_virtual_n = formatters["virtual://%s.%s"] +local f_virtual_y = formatters["virtual://%s-%s.%s"] + +function savers.virtual(specification,content,suffix) + n = n + 1 -- one number for all namespaces + local path = type(specification) == "table" and specification.path or specification + if type(path) ~= "string" or path == "" then + path = "virtualfile" + end + local filename = suffix and f_virtual_y(path,n,suffix) or f_virtual_n(path,n) + if trace_virtual then + report_virtual("saver: file %a saved",filename) + end + data[filename] = content + return filename +end + +function cleaners.virtual(filename) + data[filename] = nil +end + +local finders = resolvers.finders +local notfound = finders.notfound + +function finders.virtual(specification) + local original = specification.original + local d = data[original] + if d then + if trace_virtual then + report_virtual("finder: file %a found",original) + end + return original + else + if trace_virtual then + report_virtual("finder: unknown file %a",original) + end + return notfound() + end +end + +local openers = resolvers.openers +local notfound = openers.notfound +local textopener = openers.helpers.textopener + +function openers.virtual(specification) + local original = specification.original + local d = data[original] + if d then + if trace_virtual then + report_virtual("opener: file %a opened",original) + end + data[original] = nil -- when we comment this we can have error messages + -- With utf-8 we signal that no regime is to be applied! + -- characters.showstring(d) + return textopener("virtual",original,d,"utf-8") + else + if trace_virtual then + report_virtual("opener: file %a not found",original) + end + return notfound() + end +end + +local loaders = resolvers.loaders +local notfound = loaders.notfound + +function loaders.virtual(specification) + local original = specification.original + local d = data[original] + if d then + if trace_virtual then + report_virtual("loader: file %a loaded",original) + end + data[original] = nil + return true, d, #d + end + if trace_virtual then + report_virtual("loader: file %a not loaded",original) + end + return notfound() +end |