core-sys.lua /size: 3984 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['core-sys'] = {
2    version   = 1.001,
3    comment   = "companion to core-sys.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 lower, format, gsub = string.lower, string.format, string.gsub
10local suffixonly, basename, removesuffix = file.suffix, file.basename, file.removesuffix
11
12local environment  = environment
13local context      = context
14local implement    = interfaces.implement
15
16local report_files = logs.reporter("system","files")
17
18function environment.initializefilenames()
19
20    local arguments      = environment.arguments
21
22    local jobname        = arguments.jobname or tex.jobname
23    local fulljobname    = arguments.fulljobname or jobname
24    local inputfilename  = arguments.input or fulljobname
25    local outputfilename = arguments.result or removesuffix(jobname)
26
27    local inputfilename  = suffixonly(inputfilename) == "tex" and removesuffix(inputfilename) or inputfilename or ""
28
29    local filename       = fulljobname
30    local suffix         = suffixonly(filename)
31
32    local filename       = ctxrunner.resolve(filename) -- in case we're prepped
33
34    local jobfilename    = jobname or inputfilename or tex.jobname or ""
35    local inputfilename  = inputfilename or ""
36
37    local jobfilebase    = basename(jobfilename)
38    local inputfilebase  = basename(inputfilename)
39
40 -- jobfilename          = gsub(jobfilename,  "^./","")
41 -- inputfilename        = gsub(inputfilename,"^./","")
42
43    environment.jobfilefullname   = fulljobname
44    environment.jobfilename       = jobfilebase
45    environment.jobfilesuffix     = lower(suffixonly(jobfilebase))
46
47    environment.inputfilename     = inputfilename -- so here we keep e.g. ./ or explicit paths
48    environment.inputfilebarename = removesuffix(inputfilebase)
49    environment.inputfilesuffix   = lower(suffixonly(inputfilebase))
50
51    environment.outputfilename    = outputfilename or environment.inputfilebarename or ""
52
53    environment.filename          = filename
54    environment.suffix            = suffix
55
56 -- if tex then
57 --     tex.jobname = jobfilename
58 -- end
59
60    report_files("jobname %a, input %a, result %a",jobfilename,inputfilename,outputfilename)
61
62    function environment.initializefilenames() end
63end
64
65-- we could set a macro (but will that work when we're expanding? needs testing!)
66
67implement { name = "operatingsystem",     actions = function() context(os.platform)                     end }
68implement { name = "jobfilefullname",     actions = function() context(environment.jobfilefullname)     end }
69implement { name = "jobfilename",         actions = function() context(environment.jobfilename)         end }
70implement { name = "jobfilesuffix",       actions = function() context(environment.jobfilesuffix)       end }
71implement { name = "inputfilebarename",   actions = function() context(environment.inputfilebarename)   end }
72implement { name = "inputfilerealsuffix", actions = function() context(environment.inputfilerealsuffix) end }
73implement { name = "inputfilesuffix",     actions = function() context(environment.inputfilesuffix)     end }
74implement { name = "inputfilename",       actions = function() context(environment.inputfilename)       end }
75implement { name = "outputfilename",      actions = function() context(environment.outputfilename)      end }
76
77statistics.register("result saved in file", function()
78    -- suffix will be fetched from backend
79    local outputfilename = environment.outputfilename or environment.jobname or tex.jobname or "<unset>"
80    return lpdf and format("%s.%s, compresslevel %s, objectcompresslevel %s",outputfilename,"pdf",
81        lpdf.getcompression()
82    ) or "error"
83end)
84
85implement {
86    name      = "systemlog",
87    arguments = "3 strings",
88    actions   = function(whereto,category,text)
89        logs.system(whereto,"context",tex.jobname,category,text)
90    end,
91}
92